English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Is there any difference doing:

int (*cake)[10] = NULL;
int *sushi = new int[10];

When I increment by one:

sushi++;
cake++;

I get sushi's address added by 4 and cake's added by 28. Any idea why?

2006-06-12 11:55:36 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

Sure. You're doing two completely different things on those lines.

int (*cake)[10] = NULL;

means cake is an array, on the stack, of ten pointers to integers.

int *sushi = new int[10];

means sushi is a pointer to an array of ten integers on the heap.

The size of a pointer to an array is the size of any other pointer (4), the size of a ten pointer array is ten times the size of a pointer (40). You're probably looking at the address in hex... thus why you think you're seeing an increment by 28 when you're really seeing an increment by 0x28.

2006-06-12 12:19:51 · answer #1 · answered by Ryan 4 · 2 0

fedest.com, questions and answers