Hi,
I have some questions about type check warning. I am reading the tutorial notes from Click Here

CASE 1

int int1 = 1036; /* some data to point to */
int int2 = 8;
int *int_ptr1 = &int1; /* get addresses of data */
int *int_ptr2 = &int2;
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;

it said "Type check warning: int_ptr2 is not an int"and "int1 becomes 8"

CASE 2

int int1 = 1036; /* some data to point to */
int int2 = 8;
int *int_ptr1 = &int1; /* get addresses of data */
int *int_ptr2 = &int2;
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;

it said "Type check warning: *int_ptr2 is not an int *" and "Changes int_ptr1 – doesn’t change int1"

Can anyone explain?

In both cases, line 5 is the problem.

Looking at Case 1, line 5:
*int_ptr1 = int_ptr2;
int_ptr1 and int_ptr2 are both pointers to int. The Asterisk character (*) is the dereference operator which is used to dereference the pointer. In other words it will give you the value stored at the memory address pointed at by the pointer.
In the above line of code, you are attempting to set the int value pointed to by int_ptr1 to the address of the pointer stored in int_ptr2. So the compiler is warning you that the two types do not match. The types of the arguments either side of the assignment (=) operator do not match, one side is an int and the other side is an int pointer.

In Case 2, line 5; it's exactly the same problem, but the other way around.
int_ptr1 = *int_ptr2
Again, the types of the two arguments either side of the assignment operator do not match. But this time one side is an int pointer and the other side is an int.

This is why you are getting warnings/errors and this is why you are not seeing the results you are expecting!

Depending on what you intend to do, you could do this:
int_ptr1 = int_ptr2;
This sets int_ptr1 to point to whatever memory address int_ptr2 is pointing to (So both pointers point to the same memory address)

Or this:
*int_ptr1 = *int_ptr2;
This sets the value pointed to by int_ptr1 to the value pointed to by int_ptr2. Both pointers point to different memory addresses, but both addresses hold the same value.

I hope I've explained this clearly enough!

This two line iswrong:

*int_ptr1 = int_ptr2;

When you see an asterisk in front of a pointer read it as "the contents of int_ptr1" So that line is
saying

"the contents of int_ptr1 become int_ptr2."

But the contents of an int pointer is an int. You should either do:

int_ptr1 = int_ptr2; // pointer1 = pointer2

or

*int_ptr1 = *int_ptr2; // contents of pointer1 = contents of pointer2

So both objects on either side of the = should generally be of the same time.

I must have been drunk when I wrote my previous post. Here is what I should have said:

This line is wrong:

*int_ptr1 = int_ptr2; // different sorts of things on either side of =

When you see an asterisk in front of a pointer read it as "the contents of int_ptr1" So that line is
saying

"the contents of int_ptr1 become int_ptr2."

But the contents of an int pointer is an int. You should either do:

int_ptr1 = int_ptr2; // pointer1 = pointer2

or

*int_ptr1 = *int_ptr2; // contents of pointer1 = contents of pointer2

So both objects on either side of the = should generally be of the same type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.