There is a small program.

#include <stdio.h>

void f(char **p);

int main()
{
char *argv[]={"ab", "cd", "ef", "gh"};
f(argv);
}
void f(char **p)
{
char *t;
t=(p+=sizeof(int))[-1];
printf("%s\n", t);
}

Assume the size of int is 4, I was asked to give the running result of the following code segment. In specific, I don't understand how to analyze

t=(p+=sizeof(int))[-1];

Dani AI

Generated

Short answer: and are on the right track. The tricky expression first advances the pointer by a numeric amount (four on the platform you were given), then indexes one position before that new pointer, which lands on the final string in the four-element array.

Why that happens (stepwise, without repeating the original line): pointer arithmetic in C moves in units of the pointed-to type, not bytes. Adding 4 to a pointer-to-pointer-to-char moves it forward by four pointer-sized elements (four char slots), so starting at the first element it ends up one past the last element of a four-entry array. Array subscripting is just pointer arithmetic (a[b] == (a + b)), so using an index of -1 on that one-past-last pointer yields the last valid element. See the pointer-arithmetic rules for details: cppreference — additive operators and a longer discussion at C FAQ — pointers and arrays.

Important cautions and clarity:

  • p += N modifies p and yields the new pointer value; that result is then used by the index operation. The order is what makes the “one-past-last then -1” trick work.
  • It is legal to form a pointer that is one past the last array element, but dereferencing that one-past pointer is undefined. Accessing an element by moving back into the array (as done here) is okay; moving beyond one-past-last or dereferencing an out-of-range pointer is undefined behavior.
  • Using sizeof(int) in pointer arithmetic is obfuscating and not portable (it only equals 4 on some platforms). For byte-level moves use a char* cast, and for clarity compute offsets in element counts rather than mixing sizes.

This pattern is a compact exam-style trick. For maintainable code, prefer clear indexing (explicit indices or named variables for counts) rather than relying on side effects of compound assignments and negative subscripts.

Recommended Answers

All 3 Replies

The program prints the last element of the array of chars that you give it.

t=(p+=sizeof(int))[-1];

Is what assigns t to the last element of the array of arrays of chars.

I'm not sure if that's exactly what you're asking, but if not, I'd like some more clarification so I could help.

Thanks.

since sizeof(int) = 4, so this line of code boils down to

t=(p+=4)[-1];

p should points to that char array, then the "(p+=4)" and "[-1]" got me lost.

The program prints the last element of the array of chars that you give it.

t=(p+=sizeof(int))[-1];

Is what assigns t to the last element of the array of arrays of chars.

I'm not sure if that's exactly what you're asking, but if not, I'd like some more clarification so I could help.

p+=4 causes p, which was originally a pointer to the first element of the array ("ab") to point 4 spots further (past the end of the array) so when it is indexed with -1 it points to the element before directly itself which would be the last element in the original array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.