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

Pages: 12
Jan 10, 2012 at 9:35am
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 Jan 11, 2012 at 11:27am
Jan 10, 2012 at 9:47am
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

Jan 10, 2012 at 9:50am
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.
Jan 10, 2012 at 9:50am
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).
Jan 10, 2012 at 1:25pm
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;
}
Jan 10, 2012 at 1:50pm
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.
Jan 10, 2012 at 1:51pm
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.
Jan 10, 2012 at 2:05pm
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 Jan 10, 2012 at 2:05pm
Jan 10, 2012 at 3:22pm
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;
}
Jan 10, 2012 at 3:43pm
make026 wrote:
*p = &a[0][0]; //Why is this line giving an error?

Because you dereference a null pointer.
Jan 10, 2012 at 4:15pm
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;
}
Jan 10, 2012 at 4:42pm
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 Jan 10, 2012 at 4:43pm
Jan 10, 2012 at 4:45pm
Except that in that case p is not null but uninitialized (garbage).
Jan 10, 2012 at 4:47pm
closed account (1vRz3TCk)
p is a null pointer
Isn't p an uninitialized pointer-to-a-pointer-to-an-int?
Jan 10, 2012 at 4:53pm
You are right. It's uninitialized. Dereferencing an uninitialized pointer is not defined and might still crash the program.
Last edited on Jan 10, 2012 at 4:54pm
Jan 10, 2012 at 5:26pm
@Ivan Sidarau
P.S.:
int **p2 = NULL; // not a double pointer, it is pointer to pointer
double *d = NULL; // double pointer


I lol'd
Jan 10, 2012 at 5:45pm
1
2
int **p; //Isn't it called a double pointer?
int ***p1; //Isn't it called a triple pointer? 
Jan 10, 2012 at 5:47pm
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))
Jan 10, 2012 at 6:54pm
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 Jan 11, 2012 at 11:09am
Jan 11, 2012 at 11:17pm
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 Jan 11, 2012 at 11:22pm
Pages: 12