Difference between pointer[0] and *pointer in C?

0x0 in c
pointers in c
null pointer in c
difference between array and pointer in c
array of pointers in c
dangling pointer
difference between null and null in c
c does null equal 0

So I have the following array fib_sequence in C passed to a function as *fib_sequence[].

My function segfaults when I access an element by doing:

*fib_sequence[i]

However it works when I do:

fib_sequence[0][i]

Am I going insane or are these not equivalent?

For reference here is the entire function, it failed when assigned to index 1 only.

Segfault function

void fib(int *fib_sequence[], int count) {
    *fib_sequence = malloc(sizeof(int[count]));

    for (int i = 0; i < count; i++) {
        if (i == 0) {
            *fib_sequence[i] = 0;

        } else if (i == 1) {
            *fib_sequence[i] = 1;

        } else if (i >= 2) {
            *fib_sequence[i] = *fib_sequence[i-2] + *fib_sequence[i-1];
        }
    }
}

Working Function

void fib(int *fib_sequence[], int count) {
    *fib_sequence = malloc(sizeof(int[count]));

    for (int i = 0; i < count; i++) {
        if (i == 0) {
            fib_sequence[0][i] = 0;

        } else if (i == 1) {
            fib_sequence[0][i] = 1;

        } else if (i >= 2) {
            fib_sequence[0][i] = fib_sequence[0][i-2] + fib_sequence[0][i-1];
        }
    }
}

They are not equivalent, because postfix operators have a higher precedence than unary. This means that *fib_sequence[i] actually means *(fib_sequence[i]). Then by the equivalence of *(E) and (E)[0] that you understand correctly, that expression means (fib_sequence[i])[0], from which we can drop the unnecessary parentheses to get fib_sequence[i][0].

Remember postfix versus unary/prefix: *E, ++E, &E and others are all unary operators. E(...), E[], E->memb, E.memb are postfix.

All unary and postfix can be clumped together as one. When postfix is combined with postfix, it's clear: they go in one direction, from the root expression on the left, toward the right: E[i]->foo.memb(arg)[blah]. The precedence is all the same and the associativity can obviously only be left to right.

When unaries are combined, same thing in the opposite direction: sizeof (T) &*++E. The precedence is all the same, and the associativity is right-to-left. All of these are higher than the various binary operators.

If we put these two together, we hardly have to think:

sizeof (T) &*++E[i]->foo.memb(arg)[blah]

Once we scan past the unary operators to find the E, the precedence situation is simply this:

sizeof (T) &*++ ( E[i]->foo.memb(arg)[blah] )
                ^---------------------------^

the postfix cruft all has higher precedence than the unary cruft.

Postfixes have the highest precedence, followed by unaries, then everything else.

Difference between pointer to an array and array of pointers , The above declaration is the pointer to an array of five integers. We use int i = 0​;. // Points to the whole array b. a = &b;. for (i = 0; i < 5; i++). printf ( "%d\n" , *(*a  In this article, I will try to illustrate the differences between pointers and references. Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be dereferenced with * operator to access the memory location it points to.

You want to be aware that unary * and [] has different "precedence", and your expression

*foo[1]

is actually parsed as

*(foo[1])

You need to parenthesize your "preference" so it functions correctly:

(*foo)[1]

On a side note: In some cases *p is not equivalent to p[0], though it's always equivalent to *(p + 0) (note the pointer arithmetic here).

Therefore, you may find p[0] refuses to compile when p is a function pointer, because it cannot participate in pointer arithmetics. For data pointers, *p and p[0] doesn't really make any difference, however.

Difference between pointer[0] and *pointer in C?, They are not equivalent, because postfix operators have a higher precedence than unary. This means that *fib_sequence[i] actually means  Type &pointer; Pointer=variable name; The main differences between pointers and reference parameters are − References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can.

*pointer and pointer[0] are exactly the same in C. But that means that *fib_sequence[i] is the same as fib_sequence[i][0], which is NOT the same as fib_sequence[0][i] (unless i happens to be 0). Suffix operators in C are all higher precedence than prefix operators.

What's the difference between pointers and non-pointers in C?, Non-pointers are objects that, well, hold the actual type. int i; is a single What is the correct method of a pointer assignment in C? i = 0; /* Set the value of i. Difference between pointer and array in C? Pointers are used for storing address of dynamically allocated arrays and for arrays which are passed as arguments to functions. In other contexts, arrays and pointer are two different things, see the following programs to justify this statement.

Note that int *fib[] is parsed as int* (fib)[]; that is, an unbounded array of int*. You probably meant int (*fib)[]: a pointer to an unbounded array of integers. Once you make that change, your first example no longer compiles (as it should), but (*fib_sequence)[i] and fib_sequence[0][i] both work (also as they should).

However, *fib_sequence = malloc(sizeof(int[count])) is now an error, because you can't assign an array value. That means that you would have to move malloc outside of the function, or give up the int (*fib)[] syntax and use int** fib instead.

What is difference between null pointer and void pointer in C?, char *ptr = '\0'; A void pointer is one which does not have any data type associated with it, i.e. it can be assigned a value of any type. Also  Furthermore, the other difference lies between the implementation of the array and pointer where the array are implemented when the fixed size of the memory is allocated. On the contrary, the pointers can be used for allocating the memory dynamically.

C - Pointers, Some C programming tasks are performed more easily with pointers, and other The only difference between pointers of different data types is the data type of the value using the pointer */ printf("Value of *ip variable: %d\n", *ip ); return 0; }. In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations.

C/Pointers, 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int G = 0; /* a global variable, stored in A simple application of pointers is to get around C's limit on having only one The result is an integer value, equal to the numerical difference between the  char *cPtr // pointer to a character int *iPtr; // pointer to an integer float *fPtr; // pointer to a float double *dPtr; // pointer to a double . Difference between array and pointer in C: Array and pointer are different from each other. Below I am mentioning some points which describe the difference between array and pointer in C language. 1.

Pointers in C Programming with Examples, What is a Pointer? data_type is the pointer's base type of C's variable types and indicates the type of the #include <stdio.h> int main() { int *p = NULL; //null pointer printf(“The value inside variable p is:\n%x”,p); return 0; }. this is the Pointer which is a non-const pointer and points either a const variable or non-const variable. Explanation with examples is present in Video). 2nd) int const *P; ---) Watch in Video.

Comments
  • Small note: In some cases *p is not equivalent to p[0], though it's always equivalent to *(p + 0) (note the pointer arithmetic here).
  • Try (*fibSequence)[0].
  • @iBug: "In some cases *p is not equivalent to p[0]" in which cases, please?
  • @alk see the end of my answer below
  • You're correct however someone answered before you so I marked them as correct.
  • @Ge0rges Yep I know. That's actually a meme on this site :)