Thread: Difference between Int *p and int *p = &a

1. Difference between Int *p and int *p = &a

Hello,

Some way the pointers are used confuses me.

When use as below

Code:
```int a = 5;
int *p = &a;

printf("address of p = %d\n", &p);
printf("Address pointed by p = %d\n", p);
printf("Address of a = %d\n", &a);
printf("Value pointed by p = 5d\n", *p);```
Code:
```OUTPUT: address of p = 1987328632                                                         Address pointed by p = 1987328628
Value pointed by p = 5```
here p is pointed to the memory address of a

But what does this mean
Code:
`int *z = 7;`
To print this

Code:
`printf("%d\n", z);`
1. Is z pointer here?? If yes, then why
Code:
`printf("%d\n", z)`
Code:
`printf("%d\n", *z)`
2. Is z pointed to 7 or the address of 7 ?
3. What is the address of z ?
4. Why can't I use
Code:
`int *p = &45`
??

2. Originally Posted by Athul
But what does this mean
Code:
`int *z = 7;`
It means that you are initialising the pointer z with the value of 7. This is usually a mistake, but sometimes it is well documented for the particular system that you're coding for such that either the address 7 has some designated meaning, or the compiler knows to transform the special value to a valid address.

Originally Posted by Athul
1. Is z pointer here?
Yes. You declared z to be a pointer, so it is a pointer.

Originally Posted by Athul
If yes, then why
Code:
`printf("%d\n", z)`
Code:
`printf("%d\n", *z)`
If you want to print the pointer z, you should write:
Code:
`printf("%p\n", (void*)z);`
If you want to print what z points to, then the second example that you showed is correct, except that because you initialised z to 7, it probably doesn't point to something valid so this would also be an error.

Originally Posted by Athul
2. Is z pointed to the address of 7 or 7 ?
The value of z is 7, i.e., if it were a valid address, it would be the address 7. Again, because z probably contains an invalid address, it doesn't actually point to anything valid.

Originally Posted by Athul
3. What is the address of z ?
The address of z is &z

Originally Posted by Athul
4. Why can't I use
Code:
`int *p = &45`
??
You cannot take the address of an integer literal as it might not even have an address.

It means that you are initialising the pointer z with the value of 7. This is usually a mistake, but sometimes it is well documented for the particular system that you're coding for such that either the address 7 has some designated meaning, or the compiler knows to transform the special value to a valid address.
Does this mean,

Code:
`Int *p = 7`
here 7 is treated as address, not as value??

If Yes then I guess below question is not valid.

If No

1. Is z pointer here?? If yes, then whyCode:
[COLOR=white !important]?[/COLOR]
 1 printf("%d\n", z)

[COLOR=white !important]?[/COLOR]
 1 printf("%d\n", *z)

What I meant by this is,

For example

Code:
```int a = 5;
int *p = &5;```
Here p is a pointer, then to print value pointed by p,

Code:
`printf("%d", *p);`
In the case of
Code:
`int *z = 7;`
If z is also pointer, then as per my first example what comes first would be following to print value pointed by pointer z

Code:
`printf("%d", *z);`
but that wouldn't be correct, the correct code is

Code:
`printf("%d", *z);`
I don't understand why??

4. Originally Posted by Athul
here 7 is treated as address, not as value??
Both! p is a variable which is initialised by a value. However, because p is specifically a pointer, this value is treated as an address.

Originally Posted by Athul
For example
Code:
```int a = 5;
int *p = &5;```
As you noted in post #1, this will not compile. As I noted in post #2, this is because you cannot take the address of an integer literal. You probably intended this example:
Code:
```int a = 5;
int *p = &a;```
Originally Posted by Athul
Here p is a pointer, then to print value pointed by p,
Code:
`printf("%d", *p);`
Yes, that is correct.

Originally Posted by Athul
In the case of
Code:
`int *z = 7;`
If z is also pointer, then as per my first example what comes first would be following to print value pointed by pointer z
Code:
`printf("%d", *z);`
but that wouldn't be correct
z is also a pointer, and hence you are wrong to say that "that wouldn't be correct". The correct code to print what z points to is indeed:
Code:
`printf("%d", *z);`
This is because *z is what the pointer z points to. The issue that you are facing is not that the syntax of printing what z points to is wrong, it is that z doesn't actually point to something valid.

Originally Posted by Athul
the correct code is
Code:
`printf("%d", *z);`
I don't understand why??
It looks like you copied and pasted without changing your code snippet. You probably wanted to say that the correct code is:
Code:
`printf("%d", z);`
But this is wrong. It prints z, not what z points to (and does so with the wrong format specifier).

If you're still having trouble with this concept, then I have two suggestions:
• Keep in mind that if z is the pointer, *z is what the pointer points no. As long as the pointer z is valid and not a null pointer, this is true. If the pointer z is invalid or a null pointer, then *z is wrong. Absolutely wrong. Completely forbidden. Never ever dereference a pointer that you don't know for sure to be valid and not a null pointer.
• Never initialise a pointer with an integer literal other than 0 (which means setting the pointer to be a null pointer), e.g., never write:
Code:
`int *z = 7;`
Forget that you have seen this, and just never do it. It is wrong. Wrong. WRONG. The resulting pointer will be invalid. (One day you might do it anyway and it might be the right thing to do, but by then this won't be an issue, so just treat it as absolutely wrong and hence forbidden for now.)

5. So to sum up

When do as follows

Code:
`int *p = 5;`
here value 5 is treated as an address

Code:
```printf("%d\n", p);

//OUTPUT : 5```
But when do as follows
Code:
`printf("%d\n", *p);`
It crashes program because p is not pointing to something,

Right