#include <stdio.h>
int main(void)
{
int* ptr = NULL;
*ptr = 10;
printf("%d", *ptr);
return 0;
}
I am very new to C programming and sorry in advance if it comes off ignorant. I am trying to put a value, acceessing pointer variable which is assigned to NULL but it does not work.
My guess is that this is because *ptr is supposed to point some array, or variable's address, cannot contain the value itself without pointing anything.
Is my understaning correct?
CodePudding user response:
My guess is that this is because *ptr is supposed to point some array, or variable's address, cannot contain the value itself without pointing anything.
That is correct; *ptr = 10;
will not change where ptr
points, so you must set it to point to usable memory first.
int *ptr = NULL;
initializes ptr
to a null pointer value, which is a value that essentially means “nowhere.” It is some value that is unequal to the address of any object or function. (Most commonly, C implementations use a “zero address” for a null pointer.)
You can set ptr
to point to usable memory in several ways:
int x;
ptr = &x; // This sets ptr to point to x.
int a[17];
ptr = &a[3]; // This sets ptr to point to a[3].
ptr = &a[0]; // This sets ptr to point to a[0].
ptr = a; // This also sets ptr to point to a[0].
In the last example, the array a
is automatically converted to a pointer to a[0]
.
You can also allocate memory dynamically:
ptr = malloc(sizeof *ptr);
if (ptr == NULL)
{
fprintf(stderr, "Error, unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
Calling malloc
asks the system to reserve memory. If it succeeds, the address of the memory is returned. If it fails, a null pointer is returned, and you should always handle that possibility by testing after calling malloc
. You can #include <stdio.h>
to declare fprintf
and #include <stdlib>
to declare exit
and EXIT_FAILURE
.
After any of the above, you can execute *ptr = 10;
to store 10 in the place where ptr
points.
When you use allocated memory, you usually want to free it when you are done with it. You can do that by calling free(ptr)
.
malloc(sizeof *ptr)
allocates enough memory for one object of the type ptr
points to. You can allocate memory for N
objects with ptr = malloc(N * sizeof *ptr)
. After that, if the call succeeds, you can store values in ptr[0]
, ptr[1]
, … ptr[N-1]
.
CodePudding user response:
Yes your understanding is correct, you cannot do what you code tried to do
int main(void)
{
int* ptr = NULL; <<== creates a pointer that points noweher (null)
*ptr = 10; <<== store 10 where that pointer points
printf("%d", *ptr);
return 0;
}
In the second line you try to store 10 where 'ptr' points. But 'ptr' points nowehere, the result is whats called Undefined Behavior. Typically your program will halt right there
You can do this instead
int main(void)
{
int* ptr = NULL; <<<=== create pointer to nothing
int val = 0; << == create int with value 0
ptr = &val; <<<== set ptr to point at val
*ptr = 10; <<< ====overwrite val with 10
printf("%d", *ptr);
return 0;
}
the line
*ptr = 10;
does the same as though you did
val = 10;
You can also do
int main(void)
{
int* ptr = NULL; <<<=== create pointer to nothing
ptr = malloc(sizeof(int); <<<== set ptr to point at dynamically allocated int
if(ptr==NULL) return -1; <<<=== make sure it worked
*ptr = 10; <<< === now set that dynamically allocated int to 10
printf("%d", *ptr);
return 0;
}
Note that unlike the previous example there is no other way to refer to that int
. (You could use 'val' and '*ptr' interchangeably before)