Reputation: 59
Trying to figure out the difference between pointer initialization and pointer assignment.
C language
int *p=0;
int *p;
*p=0;
I don't know what is the difference between the two methods. Same?
Upvotes: 5
Views: 4423
Reputation: 148
Quick Answer
They are different because in your first example int *p=0;
, you are declaring p as a pointer, and then setting it to point to address 0x0. This one line example is the equivalent to these two lines of code int *p; p=0;
In your second example int *p; *p=0;
you declare p on the first line, but it is not initialized... so p is pointing somewhere undefined. On the second line you are going to the address at that undefined location and trying to set the value at that address to 0.
You can see the difference between this example, and the equivilent 2-line example before because one is using p=0;
and the other is *p=0;
Example
So before you try to access *p, you want to define where p is pointing. Let's assume you know the address to the integer that you want p to point to is 0x76543210. Let's take a look at the values in memory before the code has run:
// Dummy Memory Dump
// Offset: 0 4 8 C
// (0x76543200): 00000000 00000000 00000000 00000000
// (0x76543210): 00000000 00000000 00000000 00000000
// (0x76543220): 00000000 00000000 00000000 00000000
I changed your *p=0;
to *p=5;
in this example so that we can see the change in memory. So now let's say we run these lines of code:
int *p; // Declare a pointer p
p = (int*) 0x76543210; // Set p to point at the address 0x76543210 cast to (int*)
*p = 5; // Go to the address that p is pointing to, and set the value to 5
Now if we go back and look at what our memory, we should see that we set 5 to the address that p was pointing to:
// Dummy Memory Dump
// Offset: 0 4 8 C
// (0x76543200): 00000000 00000000 00000000 00000000
// (0x76543210): 00000005 00000000 00000000 00000000
// (0x76543220): 00000000 00000000 00000000 00000000
Explanation
Typically, declaration is broken into these components:
[type] [name];
And optionally the in-line initialization can be broken like this:
[type] [name] = [value];
In this example:
int *p=5;
This can be confusing because the * in the declaration is attached to the name. The * in the declaration just means that p is a pointer. But the name of the pointer is still just p.
So now that we know the components, we can say that
int *p=5;
is equivalent to
int *p;
p=5;
Now to add to the confusion, there is a difference between the * when declaring a pointer and when you access the address of a pointer. When you use * during access, it uses the value in p as the address that will be accessed. In this example:
int *p; // This declares a pointer p with no value
*p=5; // This line wants to go to the address that p is pointing to and set it to 5
// At this point, p is undefined because p was just declared and hasn't been set
// So it is trying to go to an unknown location to set the value 5.
Upvotes: 1
Reputation: 817
int *p = 0;
This creates a integer pointer which will point to address zero.so it is a null pointer.A null pointer just means the pointer isn't pointing to anything, or in some languages means it is unknown what it is pointing at. But because it is a null pointer, you know this, the code knows this, so no problem.
An uninitialized pointer variable is usually called a dangling pointer simply because you do not know where it points. Performing
int *p;
*p=0;
has unpredictable effects.
Upvotes: 0
Reputation: 310970
There is no difference between pointer initialization and pointer assignment.except that you may place a pointer declaration and its initialization in the global name space while an assignment as an expression statement may be used only inside a function.
So in general these two code snippets have the same effect.
int *p = 0;
and
int *p;
p = 0;
You are mistakely considering an object assignment pointed to by a pointer as a pointer itself assignment.
In this statement
*p=0;
the pointer is derefernced and the object pointed to by the pointer is assigned with the value 0.
int *p;
*p=0;
As the pointer itself was not initialized then it has some indetermined value and the expression statement with the assignment expression has undefined behaviour.
Consider the following code snippet
int x;
int *p;
p = &x; // pointer assigment
*p = 0; // object assignment pointed to by the pointer
Using a typedef for a pointer type will help to escape the confusion between pointer assignment and assignment of an object pointed to by a pointer
For example
typedef int * Pointer;
Pointer p;
p = 0;
So in a declaration the sign '*'
is used to declare a pointer.
In an expression the sign '*'
means the unary operator that denotes indirection. The result of applying this operator to a pointer is an lvalue designating the object.
Upvotes: -1
Reputation: 213799
The main reason why every new C programmer struggles with pointers is the similar-looking syntax between pointer declaration and pointer access/de-referencing.
int *p;
is a declaration of a pointer to integer. *p=0;
is de-referencing the pointer, accessing the location it points at and attempting to write the value 0 there. For this to be ok, the pointer must be set to point at a valid memory location first.int *p = 0;
is a declaration of a pointer to integer, with an initializer value. This sets where the pointer itself points at. It is not de-referencing.Assigning/initializing the value 0
to the pointer itself is a special case, since this translates to a "null pointer constant". Basically a pointer pointing at a well-defined nowhere. It is preferred to use the macro NULL
from stddef.h instead, so that we don't mix it up with the integer value 0
.
Because in the case of *p=0;
on a line of its own, the 0
is just that, a plain integer value.
Also see Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer.
Upvotes: 4
Reputation: 11921
Both are different. Here
int *p=0;
the integer pointer p
is assigned with 0
and this is same as
int *p; /* first p is declared : uninitialized */
p = 0; /* then assigned with 0 */
Though I would not prefer p = 0
. This
int *p = NULL;
is better. And here
int *p;
*p=0; /* p doesn't have any valid address, de-referencing it causes UB */
you are trying to assign *p
with 0
which is wrong as p
doesn't have any valid address in this case. It invokes undefined behavior.
Upvotes: 1
Reputation: 24052
Within a function,
int *p = 0;
is equivalent to:
int *p;
p = 0;
I.e. the variable itself is initialized, with the declarator portion of the declaration being ignored. As opposed to:
int *p;
*p = 0;
which results in undefined behavior since the target of an undefined pointer is being assigned to.
Upvotes: 7
Reputation: 12732
int *p=0;
This line will declare the int *
pointer and make it to point 0
. Also make sure you make p
to point valid memory before dereferencing.
int *p;
*p=0;
Is undefined behavior. As p
is not pointing to anywhere.
Upvotes: 3