3

If I have the following function...

void function(double *array)
{
    double a = array[3]; // Method 1
    double b = *(array + 3); // Method 2
}

Assume the array has 5 elements (I do know the length of the array ahead of time). The code compiles fine and runs ok. 'a' and 'b' do contain the expected value.

In what instances would I use method 2 as opposed to method 1?

DanielS
  • 75
  • 1
  • 1
  • 7
  • 2
    Related: [Is there a difference between using brackets and pointer arithmetic](http://stackoverflow.com/questions/21887169/is-there-a-difference-between-using-ai-and-a-i) – Shafik Yaghmour Jul 28 '14 at 16:39
  • `[]` is syntax sugar for arrays (or treating pointers like arrays). `*` dereferences a pointer, and `+` taking a pointer and an integral value returns a pointer offset by that amount. This is merely a case of the rules of c++. – payo Jul 28 '14 at 16:40
  • I sometimes use method 2 without the `*` when I will store the result in a pointer. – Neil Kirk Jul 28 '14 at 16:44
  • 2
    Note that since the two are by definition equivalent, `3[array]` is also valid and equivalent to `array[3]`. – avakar Jul 28 '14 at 17:01
  • Almost all such functions in C would either expect a second argument, indication the length of the array (ie: `void function(double* array, size_t length)`, or a custom struct that contains a pointer to an array, and the length, ie: `typedef struct myStruct_t { double* array; size_t length; } myStruct_t`, and then you'd create a function like `void function(myStruct_t* data)`. – Cloud Jul 28 '14 at 17:29
  • @avakar: Nice and concise point! Out of curiosity, have you ever seen code like `3[array]` in the wild? Someone who hasn't seen that before would probably be quite confused. On related note, I once got hit with something like `int i = ...; if ("fuzzbucket"[i] == ...) ...` during an interview. That seemed rather obscure but the interviewer claimed it would be simpler to do that than access the characters in the string literal through a `char const` array. – Void - Othman Jul 28 '14 at 17:36
  • Doesn't really matter which one you use, personally I always use the longer notation. Do note though that some compilers will use varying arithmetic for multi-dimensional arrays (like in `[X][Y]` XY vs YX priority) so if you use either the brackets or offset dereference notation you will be fine, but using them interchangeably could cause issues. – rsethc Jul 28 '14 at 18:10

2 Answers2

5

E1[E2] is equivalent in C to (*((E1) + (E2))) by definition of [] operator.

Prefer the first notation as it is shorter and more readable.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

both do the same. that's like asking which method is better for accessing struct member allocated on heap :

#include <stdio.h>
#include <stdlib.h>

struct s { int a;};
int main(void)
{
    struct s* x = malloc(sizeof(struct s));
    x->a = 2;  //method 1 OR
    printf("%d\n",x->a);
    (*x).a = 3;  //method 2
    printf("%d\n",(*x).a);
    free(x);
    return 0;
}

so use [] and -> operators. it's good to know how they really work (dereferencing a pointer and adding offset to get to right address).

macfij
  • 3,093
  • 1
  • 19
  • 24