Adi_1310
Adi_1310

Reputation: 3

Variable addressing

int main()
{

    int a = 10;

    int *p;   // int *p = a; gives me an error: invalid conversion from int to int *.
              // Tell me why?

    *p = a;   // while this runs

    cout << &a <<"  "<<p;
}

Secondly &a and p gives 2 different values. According to me Address of a and the value stored in pointer p should be the same?

Upvotes: 0

Views: 112

Answers (4)

atomicinf
atomicinf

Reputation: 3736

int *p = a, interpreted literally, takes the value stored in a and tries to interpret it as a memory address to store in p. While computationally legal, C++ won't allow this without an explicit typecast, because this is normally not what you want to do.

The reason why the statement int *p = a is different from *p = a is simple: the first statement, shorthand for the following

int *p;
p = a;

is initializing the pointer, so it expects a memory address on the RHS, while the second statement is assigning a value to the location pointed to by p, so expects (in this case) an integer on the RHS.

If you want to initialize p to point to a, you can use int * p = &a or p = &a instead, where & is the address-of operator. NEVER try to dereference uninitialized pointers! You will end up touching memory in an essentially arbitrary location, which could cause a segmentation fault (resulting in crash) or start overwriting other data in your program (resulting in obscure and non-reproducible bugs).

When you run your example code, p and &a have different values precisely because p was never assigned to point to the address of a. Some short background on why you might get any nonzero value in p at all: local variables are assigned from a special region of memory known as the stack, which also stores information about function calls and return addresses. Each process has their own stack. Crucially, though, unused regions of the stack aren't really zeroed out or otherwise cleaned up before use (except maybe in debug builds, which tend to assign really obvious values like 0xCCCCCCCC or 0xBAADF00D to uninitialized pointers). If your compiler doesn't automatically set default values for you (and release builds generally won't have such automatic initialization, for efficiency's sake), what you are seeing in p is what happened to be located in the memory assigned to p before your program set up its stack frame.

Upvotes: 2

rashok
rashok

Reputation: 13474

int *p = a; - This means you are declaring a variable and assinging values to it. Variable name is p and its type is int * value you are assiging is a (10) which will be assigned to p. int *p = a; is equivalent to

int *p; 
p = a;

We cant assing a int value to int *.

*p = a; - This you are assing int to the *p not p. so this is fine. Before doing this dont forget to allocate memory for p other it may leads to crash(undefined behaviour) because p might have some garbage values.

I hope you are trying to assing address of a to p. In that you case you can do like below.

int a;
int *p = &a;

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361722

int *p = a; initializes a pointer p with a which isn't a pointer (hence the error), while *p=a; assigns a to the memory pointed to by p, syntactically speaking. Also, the former is an initialization, while the latter is assignment.

Note that in your case, *p=a invokes undefined behavior, as p doesn't point to program's legal memory, i.e you have not allocated memory for p.

Upvotes: 2

iblue
iblue

Reputation: 30434

You store a at the address that p points to (*p). If you want to store the address of a (&a) in p, you must use

p = &a;

Using int *p = a, gives an error, because p is a int*, while a is an int. Think of it as int* p = a.

Upvotes: 1

Related Questions