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

Pages: 12
p is initialized on line 6. It is initialized to NULL, which is 0. It does not point to another pointer yet.
Does the compiler allocate a memory address for p on line 6?

What is the action offically called for a pointer to a pointer on line 11?
Does the compiler allocate a memory address for p on line 11?
closed account (1vRz3TCk)
make026 wrote:
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
#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;
}

Does the compiler allocate a memory address for p on line 6?

What is the action offically called for a pointer to a pointer on line 11?
Does the compiler allocate a memory address for p on line 11?


At line 6, an object called p, with the type pointer-to-pointer-to-int, is created and the value is initialised to NULL.

Line 11, you are assigning a value to the pointer. The value you are assigning is the addess of the first element of the array a.

If you think of pointers like a card in a card index in a library. You go to the card index find the card you want. The information on the card tells you where to find the book in the library.

So int * ptr; would be like getting a cards and giving it the title ptr. This card may already have some information filled it from the last time it was used, so it would not be good to use it as it is. This is uninitialised.

int * ptr =NULL; would be the same as a move only your erase any information on the card. It still will not help you find the book but at least you know that it will not. This is initialising to a NULL pointer constant.

int * ptr = &an_int;, So here would be like, you have a book called an_int in the library. You get a card. call it ptr and fill in the location information. Again, this is initialisation. This location information is written on the card before it is put into use.

ptr = &an_int2;, This is assignment, You are wringing new information on a card that is in use.



Thank all of you!
Topic archived. No new replies allowed.
Pages: 12