pointer to array element or direct element

closed account (3bfGNwbp)
Which way is more efficient; using a pointer to an array element or directly giving the array element? I ask this because ppl say my code is bad so I want to fix that and prove to them I can code well.

Well, I'm made a brainfuck interpreter and we were told to use a pointer to the first array element. We would then read each character from the file and use a switch statement to find the appropriate operation to preform. One of the operations ('>') would move the pointer one array element over. arrayPtr++. I never knew why we were required to use a pointer, and that's why I'm wondering if it's more efficient or something.
Last edited on
closed account (o3hC5Di1)
Hi there,

As I understand it, the variable identifier is in itself a pointer, namely to the first element of the array.
The subscript operator, [], dereferences the pointer by counting further memory slots according to the data type.

For instance, with an array of type int, the array name would refer to the address of the first element of the array. Then when you request array[1], the pointer will go look for the value 4 bytes further than the first value.

So out of this perspective it would seem silly to me to use another pointer - but perhaps one of the experts here can give a more in depth answer.

Hope that helps.

All the best,
NwN
closed account (3bfGNwbp)
So, in that case, a pointer is completely useless. I wonder why we were required to use one...
Last edited on
Topic archived. No new replies allowed.