int myPrintf(const char *format, ...)
{
extern int print(char **out, int *varg);
register int *varg = (int *) (&format);
return print(0, varg);
}
google : What does … mean in an argument list in C ?
-> http://stackoverflow.com/questions/1124265/what-does-mean-in-an-argument-list-in-c
I came across the following function signature and I wondered if this (the ellipsis, or "..."
) is some kind of polymorphism?
#include <fcntl.h>
int fcntl(int fd, int cmd, ... );
It's a variable argument list.
C: Variable Argument List Access
Here, I explained how we can utilize the operator ellipsis (…) to pass variable number of arguments to a function. I have utilised there the concept of pointers to access the variable arguments. The standard C Library provides support to access these arguments. Use for this support
All you need is to know the last argument before the ellipsis operator(At least one argument is must to use variable arguments), let’s call it larg
suppose
fun(type var1,type var2,...)
is a function, then larg corresponds to var2
Now we need to declare the list using va_list
i.e.,
va_list al
now initialize it using va_start
va_start(al,larg);
Now to access each argument, we must know the expected type of the argument
type var_name = va_arg(al,type);
When we have accessed all the variable arguments, we need to clean up
va_end(al);
Using standard library, we can easily access the variable arguments
Take a simple example
#include <stdarg.h> int print(int num_count,...) /*num_count contains the number of integers passed*/ { int i; va_list al; va_start(al,num_count); for(i=1;i<=num_count;i++){ int val=va_arg(al,int); printf("%d ",val); } } int print_str(const char *str,...) /*str has the number of strings passed*/ { int i; va_list al; int num_count=atoi(str); va_start(al,str); int *num=&num_count; for(i=1;i<=num_count;i++){ char *str=va_arg(al,char *); printf("%s ",str); } } int main() { print(3,2,3,4); print_str("3","Hi","All","!!!"); }
OUTPUT
2 3 4 Hi All !!!
http://linuxprograms.wordpress.com/2008/03/07/c-variable-argument-list-access/
이거해서 아래처럼 즉각즉각 디버깅에 사용할 수 있도록 하자
AfxMessageBox( func("%d = %d\n",10,20));
#define _ADDRESSOF(v) ( &reinterpret_cast<const char &>(v) )
#define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
-> va_start(al, num_count) 하면 int임
-> char* ap = (char*)주소 + 4임
-> 맨처음 arg는 건드리지 않고 i+1부터 건드린다는 얘기지
#define _crt_va_end(ap) ( ap = (va_list)0 ) //이건 그냥 NULL을 ap
에다가 넣고 그냥 이제 메모리 접근 못하게 막는거고
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
#define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
_INTSIZEOF(n) 검증
->
char,int,float,char* -> 4 , 0100
double -> 8 , 1000
_crt_va_arg(ap,t) 검증
->
#define _crt_va_arg(ap,t) ( *(int *)주소) //이러면 int
#define _crt_va_arg(ap,t) ( *(char *)주소) //이러면 char
#define _crt_va_arg(ap,t) ( *(float *)주소) //이러면 float
#define _crt_va_arg(ap,t) ( *(char** *)주소) //이러면 char*임
_crt_va_arg(ap,t) 실행하면 주소가 +4가 되버리더라..왜냐면 ap += 4를
했으니깐...일단 주소는 무조건 증가하고..값은 그 이전꺼 보여주는거임
->printf로 확인 결과 원래 4였는데 이게 8,c,0이 되버림...맞음
->f는 double로 8바이트 읽어야 되는 듯?
http://showmiso.tistory.com/183 참고함
#include <stdio.h>
int main(void)
{
int a= 0xffffffff ;
int b= 0;
char* c = reinterpret_cast<char*>(&a);
printf("a의 주소 = %x\n",&a);
printf("c = %x\n",c);
printf("&c = %x\n", &c);
getchar();
getchar();
return 0;
}
결과
a의 주소 = 28ff44
c = 28ff44
&c = 28ff3c
이걸 cpp로 하면 실행이 되고 c로 하면 안됨
나도 저거 #include <stdarg.h> 헤더 추가해서 했는데 저 reinterpret_cast가 cpp 문법인가봄. 검색해도 그리 나옴
굳이 저리 캐스트 안해도 c로도 그냥 포인터에 상수 넣고 상수형 변수에 주소값 넣을 수 있는걸로 아는데
어셈블리로 짜도 그리 될테고...별거 아닌 내용 같음...
reinterpret_cast<const char &>
dynamic_cast
const_cast
static_cast
http://showmiso.tistory.com/183
->
출처
http://blog.naver.com/sorkelf/40120457656
일반적으로 허용하지 않는 위험한 형변환을 할 때 사용한다
단지 안의 있는 비트열만 보고 원하는 형으로 강제 변환을 할 때 사용한다
포인터를 정수로 변환하는 작업을 할 때 많이 사용한다
'컴퓨터 과학 & 영상처리 관련 > C / C++' 카테고리의 다른 글
C의 기이한 배열 접근법 (0) | 2013.01.04 |
---|---|
변수 없이 CString의 Format() 사용하기 (0) | 2012.12.16 |
#define에서 #x 와 같이 문자열 사용하는 법 (printf도 포함) (0) | 2012.11.16 |
함수 문법에서 신기한거 (0) | 2012.11.15 |
STL map 자료구조 (0) | 2012.10.11 |