Ben C.
Ben C.

Reputation: 31

How do you get the pointer of something you dereferenced?

The pointer points to a struct which contains an array. How do I get the memory location of each element of the array in the pointer?

ptr->array[a]

Upvotes: 1

Views: 67

Answers (3)

Jonathan Wood
Jonathan Wood

Reputation: 67195

You can use the address-of operator (&) to get the address of a variable. (As in &ptr->array[a].)

However, since ptr->array is already an address, you can also use the syntax ptr->array + a.

Upvotes: 0

David Haim
David Haim

Reputation: 26476

and also ptr->array+a, becuase of pointer arithmetics. but use @Vaughn Cato way for readability.

Upvotes: 0

Vaughn Cato
Vaughn Cato

Reputation: 64308

Using &ptr->array[a] will give you a pointer to element a within ptr->array.

Upvotes: 2

Related Questions