443,837 Members | 1,842 Online
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

# warning C4047:

 P: n/a Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include void swap(int *x, int *y); int main() { int x[2]={1,6}, y[2]={1,10}; printf("Before the function swap, x = %d and y = %d\n\n", x, y); swap(&x, &y); printf("After the function swap, x = %d and y = %d\n\n", x, y); return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } Nov 13 '05 #1
10 Replies

 P: n/a io****************@web.de (isxyos) wrote: Hello, It's just a warning, but can anybody explain to me what this warning is:warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *'#include void swap(int *x, int *y);int main(){ int x[2]={1,6}, y[2]={1,10}; Make this: int x = 6, y = 10; printf("Before the function swap, x = %d and y = %d\n\n", x, y); swap(&x, &y); printf("After the function swap, x = %d and y = %d\n\n", x, y); return 0;} Or, if x and y had to be arrays, change x, y to *x, *y in the printf()s, and change swap( &x, &y ) to swap( x, y ); Alas, with the identical values you supplied for x[0] and y[0], you won't notice if you'd invoced swap() at all... void swap(int *x, int *y){ int temp = *x; *x = *y; *y = temp;} -- 6 * 9 = 42 (base 13) Nov 13 '05 #2

 P: n/a isxyos wrote: Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include void swap(int *x, int *y); int main() { int x[2]={1,6}, y[2]={1,10}; printf("Before the function swap, x = %d and y = %d\n\n", x, y); swap(&x, &y); printf("After the function swap, x = %d and y = %d\n\n", x, y); return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } Don't know what the error message is exactly supposed to mean, are you sure you got it for the program you posted (I can't see neither any 'void *' nor an 'unsigned int' in the whole program...)? But the problem is obvious: you're declaring x and y to be arrays of two integers each but in the following you consistently treat them as if they were simple integers. So either change your program so that they are both declared to be simple integers or append every- where else if you mean the first or the second element of the array. Regards, Jens -- _ _____ _____ | ||_ _||_ _| Je***********@physik.fu-berlin.de _ | | | | | | | |_| | | | | | http://www.physik.fu-berlin.de/~toerring \___/ens|_|homs|_|oerring Nov 13 '05 #3

 P: n/a isxyos wrote: Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include void swap(int *x, int *y); int main() { int x[2]={1,6}, y[2]={1,10}; printf("Before the function swap, x = %d and y = %d\n\n", x, y); x and y arent' integers. What do you think is supposed to be printed ? swap(&x, &y); swap(x, y); In this context, x and y are converted to pointers to their respective first elements. printf("After the function swap, x = %d and y = %d\n\n", x, y); return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } #include void swap(int *x, int *y); int main(void) { int x[2]={1,2}, y[2]={3,4}; printf("Before the function swap, x[0] = %d and y[0] = %d\n", x[0], y[0]); printf("Before the function swap, x[1] = %d and y[1] = %d\n\n", x[1], y[1]); swap(x, y); swap(x + 1, y + 1); printf("After the function swap, x[0] = %d and y[0] = %d\n", x[0], y[0]); printf("After the function swap, x[1] = %d and y[1] = %d\n\n", x[1], y[1]); return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } -- pete Nov 13 '05 #4

 P: n/a io****************@web.de (isxyos) wrote: Hello, It's just a warning, but can anybody explain to me what this warning is:warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *'#include void swap(int *x, int *y);int main(){ int x[2]={1,6}, y[2]={1,10}; Make this: int x = 6, y = 10; printf("Before the function swap, x = %d and y = %d\n\n", x, y); swap(&x, &y); printf("After the function swap, x = %d and y = %d\n\n", x, y); return 0;} Or, if x and y had to be arrays, change x, y to *x, *y in the printf()s, and change swap( &x, &y ) to swap( x, y ); Alas, with the identical values you supplied for x[0] and y[0], you won't notice if you'd invoced swap() at all... void swap(int *x, int *y){ int temp = *x; *x = *y; *y = temp;} -- 6 * 9 = 42 (base 13) Nov 13 '05 #5

 P: n/a isxyos wrote: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' Doesn't your compiler give you line numbers? -- Tom Zych This email address will expire at some point to thwart spammers. Permanent address: echo 'g******@cbobk.pbz' | rot13 Nov 13 '05 #6

 P: n/a isxyos wrote: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' Doesn't your compiler give you line numbers? -- Tom Zych This email address will expire at some point to thwart spammers. Permanent address: echo 'g******@cbobk.pbz' | rot13 Nov 13 '05 #7

 P: n/a Tom Zych wrote in message news:<3F***************@pobox.com>... isxyos wrote: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' Doesn't your compiler give you line numbers? Here is what my compiler says: dresnick(531)\$ gcc -Wall -ansi -pedantic foo.c -o foo foo.c: In function `main': foo.c:6: warning: int format, pointer arg (arg 2) foo.c:6: warning: int format, pointer arg (arg 3) foo.c:7: warning: passing arg 1 of `swap' from incompatible pointer type foo.c:7: warning: passing arg 2 of `swap' from incompatible pointer type foo.c:8: warning: int format, pointer arg (arg 2) foo.c:8: warning: int format, pointer arg (arg 3) The program as written doesn't make much sense. If the purpose is to swap in place arrays of integers, then the sizes of the arrays need to be provided to the swap function (and they'd better be the same size). If it is to just swap the first elements of arrays of integers, perhaps it would be more clearly invoked as: swap(&x[0], &y[0]); (though swap(x,y); should also be fine). Or are the integer arrays red herrings? In any case, the other complaint is that you are passing a pointer to the %d specifier, when you mean to be dereferencing the pointer and passing the int... -David Nov 13 '05 #8

 P: n/a Tom Zych wrote in message news:<3F***************@pobox.com>... isxyos wrote: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' Doesn't your compiler give you line numbers? Here is what my compiler says: dresnick(531)\$ gcc -Wall -ansi -pedantic foo.c -o foo foo.c: In function `main': foo.c:6: warning: int format, pointer arg (arg 2) foo.c:6: warning: int format, pointer arg (arg 3) foo.c:7: warning: passing arg 1 of `swap' from incompatible pointer type foo.c:7: warning: passing arg 2 of `swap' from incompatible pointer type foo.c:8: warning: int format, pointer arg (arg 2) foo.c:8: warning: int format, pointer arg (arg 3) The program as written doesn't make much sense. If the purpose is to swap in place arrays of integers, then the sizes of the arrays need to be provided to the swap function (and they'd better be the same size). If it is to just swap the first elements of arrays of integers, perhaps it would be more clearly invoked as: swap(&x[0], &y[0]); (though swap(x,y); should also be fine). Or are the integer arrays red herrings? In any case, the other complaint is that you are passing a pointer to the %d specifier, when you mean to be dereferencing the pointer and passing the int... -David Nov 13 '05 #9

 P: n/a isxyos wrote: Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include void swap(int *x, int *y); int main() { int x[2]={1,6}, y[2]={1,10}; printf("Before the function swap, x = %d and y = %d\n\n", x, y); x and y are pointers to ints (well, arrays that decay to pointers to ints), not ints. Use: printf("Before the function swap, x = %p and y = %p\n\n", (void *)x, (void *)y); swap(&x, &y); Try this: swap(x, y); Of course, this won't work the way you expect - the function will affect only the first element in the array. printf("After the function swap, x = %d and y = %d\n\n", x, y); See above about %p. Also, this won't change, as x and y have fixed locations in memory (unless you recurse). return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } -- Lead me not into temptation... I can find it myself. Nov 13 '05 #10

 P: n/a isxyos wrote: Hello, It's just a warning, but can anybody explain to me what this warning is: warning C4047: '=' : 'unsigned int ' differs in levels of indirection from 'void *' #include void swap(int *x, int *y); int main() { int x[2]={1,6}, y[2]={1,10}; printf("Before the function swap, x = %d and y = %d\n\n", x, y); x and y are pointers to ints (well, arrays that decay to pointers to ints), not ints. Use: printf("Before the function swap, x = %p and y = %p\n\n", (void *)x, (void *)y); swap(&x, &y); Try this: swap(x, y); Of course, this won't work the way you expect - the function will affect only the first element in the array. printf("After the function swap, x = %d and y = %d\n\n", x, y); See above about %p. Also, this won't change, as x and y have fixed locations in memory (unless you recurse). return 0; } void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } -- Lead me not into temptation... I can find it myself. Nov 13 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.