pointer to next element of an array

Lokesh

I am struggling with a problem in C. The problem is how to get pointer to an element in array if I know the pointer to the previous element in the array?

Suppose I have a string

s = "Hello World"

and I have a pointer to 'o' in the string

char *p = strchr(s, 'o')

How do I get pointer to o given I just know p?

Sergey Kalinichenko

If you know that the pointer to an array element is not pointing to the last element of the array, the expression p+1 will point to the next element of the array. This rule works regardless of the type of the array element, as long as the base type of the pointer is the same as the element type of the array: the compiler makes all the necessary adjustments when performing the addition.

Therefore, in your strchr example printing *(p+1) will print a space.

Demo.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java Arrays - Do each element in array hold pointer to the next element

Pointer to the Address of Element of an Array

Replace element of a pointer array

How to move to the next pointer in a smart pointer array

Get the next element of an array

Get next array element

next pointer element in linked list structure

Ctypes pointer on element in ctypes array

C pointer to last element of array

Access array element with pointer arithmetics

Compare array element previous and next

Is there a way to show the next element in an array?

Check for next 'false' element in array

Grabbing previous and next element in an array

Find the next greater element in an array

After using realloc the next pointer in array is lost

Replacing the element with next largest element in array

Add/Subtract/etc element by the next element in an array

php array chunk with next element of an array

What is the best way to copy a buffer and return a pointer to the next available element?

comparing one array element with the next element to find what is the biggest element

making a pointer that stores an element of a char array

Why sizes of an array and a pointer to a first element are different?

Pointer to one before first element of array

adding and subtracting an element from array pointer

Difference between pointer to a new element and new array?

Calling free on a pointer to an element of an array declared dynamically

How to specify member pointer to an array element?

Static array initialization with pointer to extern struct element?