1

Already declared is an array such as:

char ma[10][20];

The address of a specific element is gotten using:

p = &ma[1][18];

Which element will p point to after p++; ?

0

3 Answers 3

2

Adding 1 to a address of member of array, get the address of the next member. since p is the address of ma[1][18], which is member of the array ma[1], p+1 is the address of ma[1][19]. (And of course, p++; is like p=p+1;)

Edit: I assumed, of course, that p is char*. If it's something else, the answer can be other.

Sign up to request clarification or add additional context in comments.

3 Comments

if p is not char * (or a qualified variant of char * or void *), the program is invalid, so we can safely assume it is char *.
Yes, but some compilers will compile it anyway. (Tested on gcc)
the fact that gcc compiles it doesn't make it valid, but at least the compiler has to issue a diagnostic if the type is not one of the types cited above.
1

p++ yields &ma[1][19]

Here is the explanation:

char ma[10][20];
char *p = &ma[1][18];

p value is &ma[1][18] which is equal to *(ma + 1) + 18.

So p++ value which is equal to p + 1 is equal to (*(ma + 1) + 18) + 1 equal to *(ma + 1) + 19 which is equal to &ma[1][19].

Comments

0

You don't specify the type of p; assuming it's char *, then p++ will advance it to ma[1][19] (1 char).

Here are a couple of variations:

char (*p)[20] = &ma[1];

In this case, p is a pointer to a 20-element array of char, initialized to point to ma[1]; executing p++ will advance p to point to ma[2] (20 chars).

char (*p)[10][20] = &ma;

In this case, p is a pointer to a 10-element array of 20-element arrays of char, initialized to point to ma; executing p++ will advance p to the next element immediately following ma (200 chars).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.