[C++] One Star Symbol And A pointer to A pointer(Hard Questions)

Pages: 12
Hello all!
Please tell me why...

Here are my questions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>

int main(){
	int **p = NULL;

	printf("%d\n", *p); //Why is this line giving an error???
	//What kind of error is it?
	//What does "*p" do ?
	//What can "*p" do ?
	//What is "*p" ?
	//When does the compiler allocate a memory address to it?

	//I usually use the codes below:
	int a = 60;
	int *p1 = NULL;//a pointer
	int **p2 = NULL;//a double pointer

	p1 = &a; //point to an integer varible a
	p2 = &p1; //point to a pointer p1
     
    system("pause");
    return 0;
}
Last edited on
you can't use dereferencing operator on NULL

**p = NULL;
what do you expect to see on *p ?
you are trying to use memory that does not exist.

P.S.:
int **p2 = NULL; // not a double pointer, it is pointer to pointer
double *d = NULL; // double pointer

Just think "What is [**]p?". The answer would very clearly be: **p is an integer, *p is a pointer to an integer and p is a pointer to a pointer [to an integer]. All your questions should now solve themselves.
1
2
3
	int **p = NULL;

	printf("%d\n", *p); //Why is this line giving an error??? 

p is a pointer to a pointer to an int, it's just like p2 that appears later on. In this case it's initialised to null.

Whe you pass *p to printf, you're dereferencing p. That is, you're getting the value that p points to. In your case, you've already told it that it doesn't point anywhere, then you try to dereference it. This is an error, and your memeory address space is mapped such that this interrupts the program (as it's such a common error to dereference null).
What is the difference between "NULL of int" and "NULL of int **"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>

int main(){
	int a = NULL; //?
	int **p = NULL; //?
	int *p1 = NULL;


	p = &p1;
	p1 = &a;

	printf("p = %d\n", p);
	printf("*p = %d\n", *p); //This line isn't giving an error.
	printf("**p = %d\n", **p);
	printf("&p1 = %d\n", &p1);
	printf("p1 = %d\n", p1);
	printf("*p1 = %d\n", *p1);
     
    system("pause");
    return 0;
}
NULL means 0. That is the case and even for some old c compilers NULL should mean 0.

That is that for variables as int it's obvious what it means (just a value equal to zero) but for pointers it has some special meaning since it means: Not pointing to anything.

You can safely use 0 instead but it's more clear sometimes to use NULL.
NULL is a alias of (void*)0, there is no difference in the value.
The thing you have to understand is that NULL cannot be dereferenced.
But at line 10 you give a value to p, the adress of p1.
So at line 14 it will print the value in the variable p1 which is the adress of a.
aquaz wrote:
NULL is a alias of (void*)0, there is no difference in the value.

NULL is never (void*)0 in C++. That would break a lot of code.
int *p = (void*)0; // doesn't compile!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main(){
	int a[2][2] = {{4, 5}, {6, 7}};
	int **p = NULL;
	int *p1 = NULL;

	p1 = &a[0][0];
	*p = &a[0][0]; //Why is this line giving an error?

	printf("p1 = %d\n", p1);
	printf("*p1 = %d\n", *p1);
     
    system("pause");
    return 0;
}
make026 wrote:
*p = &a[0][0]; //Why is this line giving an error?

Because you dereference a null pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>

int main(){
	int a[2][2] = {{4, 5}, {6, 7}};
	int **p;
	int *p1 = NULL;

	p1 = &a[0][0];
	*p = &a[0][0]; //Why is this line giving an error?

    system("pause");
    return 0;
}
p is a null pointer, so when you do *p you are dereferencing a null pointer. Dereferencing a null pointer is not defined so the program might crash.
Last edited on
Except that in that case p is not null but uninitialized (garbage).
closed account (1vRz3TCk)
p is a null pointer
Isn't p an uninitialized pointer-to-a-pointer-to-an-int?
You are right. It's uninitialized. Dereferencing an uninitialized pointer is not defined and might still crash the program.
Last edited on
@Ivan Sidarau
P.S.:
int **p2 = NULL; // not a double pointer, it is pointer to pointer
double *d = NULL; // double pointer


I lol'd
1
2
int **p; //Isn't it called a double pointer?
int ***p1; //Isn't it called a triple pointer? 
No, p is a pointer to a (pointer to an int), and
p1 is a pointer to a (pointer to a (pointer to an int))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main(){
int a[2][2] = {{4, 5}, {6, 7}}; //This array is initialized.
int **p = NULL; //But... For a pointer... Is this pointer really initialized on this line?
int *p1 = NULL;

p1 = &a[0][0];

p = (int **)&a[0][0]; //???This line...
//Does the compiler really initialize the pointer or allocate memory address for it?
*p = &a[0][0]; //This line isn't giving an error.

printf("*p = %d\n", *p);

*p = (int *)6;

printf("p = %d\n", p);
printf("*p = %d\n", *p);

     
system("pause");
return 0;
}

Last edited on
Dereferencing an uninitialized pointer is not defined and might still crash the program.
But there is a new question...
When does p really be initialized?
Last edited on
Pages: 12