warning C4047:

I

isxyos

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 <stdio.h>
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;
}
 
J

Jens.Toerring

isxyos said:
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 <stdio.h>
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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
P

pete

isxyos said:
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 <stdio.h>
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 <stdio.h>

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;
}
 
I

Irrwahn Grausewitz

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 <stdio.h>
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...
 
D

David Resnick

Tom Zych said:
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
 
B

bd

isxyos said:
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 <stdio.h>
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top