Incrementing pointers of pointers

Refresh

February 2019

Views

2.3k time

1

I'm trying to figure out how i would go about incrementing a pointer of pointers to point at a value i want. This is essentially an array of pointers to cstrings and i want to increment the pointer to point at xx pointer. For insistence in my current application if i want to point at the fifth pointer i can do (pointer is defined as char **pointer)

*pointer++;
*pointer++;
*pointer++;
*pointer++;
*pointer++;

and i it would now set the fifth pointer to *pointer[0] which i want. I am just looking for a way to clean this up into hopefully a 1 liner. I've tried different ways to go about this, but i have yet to find a working solution. My last attempt was

*pointer += (sizeof(pointer) * 5);

but it did not work. I thought that if i get a size of a pointer on the system (4 bytes) and multiply it by the number of pointers i wish to skip that it would do the same thing as spamming that *pointer++ calls. After i increment my pointer i use the following to printf the rest of the cstrings until the end of the pointer array is met.

while(*pointer[0])
{
   printf("data: %s\n", *pointer);
   pointer++;
}

Overall i need help turning 5 *pointer++; calls into a 1 liner.

2 answers

3

If you want to increment a pointer by 5, simply do it:

pointer += 5;

This is fine in C, it's called pointer arithmetic: the compiler will figure out the size of the pointed object and will add the right number of addressable units. If you multiply 5 by the size of a pointer the result won't what you expect, if the size of pointers is 4 on your platform then the result will be an increment by 20 instead of 5.

Increment operator doesn't do anything magic with pointers, you're just adding 1 to it.

Furthermore, are you sure *pointer++ really does what you think? Increment has highest precedence than dereference operator. This two statements have the same effect:

*pointer++;

pointer++;

In both statements pointer is incremented, in the first one you also dereference the pointer (the old value, since the increment is postfix) and take what it points to, without doing anything with it.

If you use them inside an expression they will give you two different things: if pointer has type char ** (supposing it's an array of strings), *pointer++ gives you the first string as a pointer to its first character and then increments pointer, while the result of pointer++ is still a char ** and simply returns pointer itself and then increments pointer. Beware that in both cases you're losing the first string.

I saw the linux tag: you don't have to think about the platform here, this is portable.

1

If you have a pointer pointer of type TYPE* and you add N, then the pointer will be advanced by N * sizeof (*pointer) or equivalent N * sizeof(TYPE) bytes. Since you have not mentioned if the memory is allocated dynamically, after these pointer advancing would cause undefined behavior when you try to free() the memory. Meaning it is number of allocated bytes or the current address pointing too.