user2688772
user2688772

Reputation: 347

Addressing in C

#include<stdio.h>
#include<stdlib.h>   
int main(int argc, char *argv[])
{
    int x = 2;
    int *pointer2 = &x;
    int number = &x;
    printf("%x\n",number );
    printf("%x\n",number+1 );
    printf("%x\n",pointer2 );
    printf("%x\n",pointer2+1);

    return EXIT_SUCCESS;
}

I get a warning from gcc that the "initialization makes integer from pointer without a cast [enabled by default]" for the variable number but when i compile i get the following ouput:

5da24540
5da24541
5da24540
5da24544

Clearly the above thing doesnt happen as we can see on the first two lines of output that the address is only incremented by 1 whereas by 4 in second case.Also please explain that & returns the address which can be assigned to other variables like int's or it is a pointer in it self which can be assigned only to pointer type.

Upvotes: 2

Views: 339

Answers (5)

John Bode
John Bode

Reputation: 123458

You get the warning because it's not guaranteed that the int type can hold a pointer value (for example, you're on a system where int is 16 bits but pointer values are 32 bits); if you add a cast, you tell the compiler that you know what you're doing (even if you don't):

int number = (int) &x;

This tells the compiler that yes, you really want to take the address of x and treat it as an ordinary integer. Just be aware that the value that gets stored to number may be truncated.

Nor is it guaranteed that all pointer types have the same size and representation; IOW, a pointer to int may be a different size than a pointer to char, which may be a different size from a pointer to a struct. On most modern desktop and server architectures they are the same, but there are still some oddball architectures out there where they're not.

As for why you got different results on the additions...

Adding 1 to a pointer gives the address of the next object of the pointed-to type; IOW, if you have a pointer to int, adding 1 will give you the address of the next available int, which is sizeof (int) bytes from the current address. If you have a pointer to a 10-element array of int, adding 1 will give you the address of the next 10-element array of int, which is 10 * sizeof (int) bytes from the current address.

This is how array indexing works. The expression a[i] is interpreted as *(a + i); that is, compute the address of the i'th element following a, and dereference the result.

Upvotes: 0

Max Truxa
Max Truxa

Reputation: 3478

The warning is because of this assignment:

int number = &x;

You are assigning an int* to an int, which is nothing you should do. Either (1) assign the value of the x to number, or (2) change number's type to int* (link pointer2) and assign the address of x.

Or (3) if you really want to assign the address of x to number use a typecast.

// (1)
int number = x; // Assign value of x.
// (2)
int* number = &x; // Assign address of x.
// (3)
int number = (int)&x; // Assign address of x and cast to int.

Your second question has to do with pointer arithmetic.

Your number variable is an int, which means if you increment it (+ 1) it is literally incremented by 1.

Your pointer2 variable is an int*, which means if you increment it it is incremented by the size of an int* (4 byte on 32 bit systems, 8 byte on 64 bit systems).

This is done, so that you can iterate over pointer arrays by simply incrementing the pointer.

There are two reasons the compiler emits a warning:

1) The type mismatch is not handled implicitly by the compiler, because the standard says so. And this in return is since converting from a pointer type to another type has a rare use case. Most of the time assignments like this are made accidentally and the author simply messed up the level of indirection (the number of *).

2) Another reason why the standard prohibits this, is that int is 4 bytes (in most implementations) and int* can be either 4 or 8 bytes depending on the platform compiled for.

Upvotes: 1

mcleod_ideafix
mcleod_ideafix

Reputation: 11428

The compiler doesn't convert number from int to pointer. The warning says "make integer from pointer", not "make pointer from integer", so number is still an int. It's the pointer (the address of x) which has been "converted" to a plain integer.

Upvotes: 0

JeffRSon
JeffRSon

Reputation: 11176

number is an int, initialized with the address of x (makes int from address "without cast" - the warning tells you, that this is probably not what you want) - if you increment it, it will be incremented by one.

pointer2 is a pointer to int (initialized with the same address of x), incrementing it increments by size of int (this behaviour is called "pointer arithmetics").

Upvotes: 7

Ashwin
Ashwin

Reputation: 1013

Typecast the 'number' assignment and try like this,

int x = 2;
int *pointer2 = &x;
int number = (int)&x;

This will work.

Upvotes: -1

Related Questions