Advertisement
sapitando

LCC-Win32 32bit - Variadic functions

Sep 9th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. /* Autor : Tiago Portela
  2.    Email : sapitando@gmail.com
  3.    Sobre : Compilado com LCC-Win32 32bit . Tentando entender a disposição na memória dos argumentos da função variádica,
  4.            cada compilador(e sua própria versão) implementa a sua maneira própria.
  5.    Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <stdarg.h>
  11. #include <conio.h>
  12.  
  13.  
  14. void Myprintf(const char* format,...)
  15. {
  16.     va_list args;
  17.     args = (va_list)(&format + 1); // va_start(args, format);
  18.     void *p = (void*)args;
  19.  
  20.     printf("%p -> %p = %s\n", (void*)(&format), format, format);
  21.     printf("%p -> %p = %s\n", (void*)(&format + 1), (void*)*(&format + 1), *(&format + 1));
  22.     printf("%p -> %p = %s\n", (void*)(&format + 2), (void*)*(&format + 2), *(&format + 2));
  23.     printf("%p = %Lf\n", (void*)(&format + 3), *(long double*)(&format + 3));
  24.     printf("%p = %lf\n", (void*)(&format + 6), *(double*)(&format + 6));
  25.     printf("%p = %u\n", (void*)(&format + 8), *(int*)(&format + 8));
  26.     printf("%p = %p = %u\n", (void*)(&format + 9), *(void**)(&format + 9), **(int**)(&format + 9) );
  27.     printf("\n\n");
  28.  
  29.     printf("%p = %u\n", (void*)((void**)args + 7), *(unsigned int*)(args + 7 * sizeof(uintptr_t)));
  30.     printf("%p = %p = %u\n", (void*)((void**)args + 8), *((void**)args + 8), **((unsigned int**)args + 8));
  31.     printf("\n\n");
  32.  
  33.     vfprintf(stdout, format, (va_list)p);
  34.     va_end(args);
  35.     printf("\n");
  36. }
  37.  
  38.  
  39. int main(void){
  40.     long double ldCount = 99.50;
  41.     double lfCount = 250.125;
  42.     unsigned int uiCount = 199;
  43.     Myprintf("AAAAAAAAAA %s %s %Lf %lf %u %p", "BBBBBBBBBB", "CCCCCCCCCC", ldCount, lfCount, uiCount, &uiCount);
  44.     getch();
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement