Beruflich Dokumente
Kultur Dokumente
Allocation
Mugurel Ionu Andreica
Spring 2012
int x;
int *p, char* q;
p = q = &x;
*p // refers to the int which starts at the address &x
*q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)
Pointers - example
*pp1 = p1;
printf("%d, %d, %d\n", pp1, *pp1, **pp1);
pp1 = &p1;
printf("%d, %d, %d\n", pp1, *pp1, **pp1);
x = 3;
printf("%d, %d, %d\n", pp1, *pp1, **pp1);
**pp1 = 5;
printf("%d\n", x);
int main() {
x = 7;
ppp1 = &pp1;
printf("%d, %d, %d, %d\n", ppp1, *ppp1, **ppp1, ***ppp1);
p1 = new int;
*p1 = 10;
printf("%d, %d\n", p1, *p1);
p1 = &x;
printf("%d, %d\n", p1, *p1);
pp1 = new int*;
*pp1 = new int;
**pp1 = 11;
printf("%d, %d, %d\n", pp1, *pp1, **pp1);
***ppp1 = 90;
printf("%d\n", x);
ppp1 = new int**;
*ppp1 = new int*;
**ppp1 = new int;
***ppp1 = 9;
printf("%d, %d, %d, %d\n", ppp1, *ppp1, **ppp1, ***ppp1);
printf("%d\n", x);
v = new int[100];
v[x = 17] = 8;
printf("%d\n", v[x]);
A = new int*[600];
A[345] = new int[512];
A[345][100] = 16;
printf("%d, %d\n", A[345][100], A[345][101]);
sp = new struct mystruct;
sp->x = 17;
sp->b[3] = 14;
sp->d = 9.3;
printf("%d, %d\n", sp->x, (*sp).x);
spp = &sp;
printf("%d, %d\n", (*spp)->x, (**spp).x);
sppp = &spp;
(***sppp).x = 9;
printf("%d, %d, %d, %d\n", (***sppp).x, (**sppp)->x, sp->x,
(*spp)->x);
delete sp;
printf("%d, %d, %d, %d\n", (***sppp).x, (**sppp)->x, sp->x,
(*spp)->x);
e = e1;
while (e != NULL) {
printf("e->x=%d\n", e->x);
e = e->next;
}
return 0;
}