Pointer to first element in array! (C)

My guess (you only show two lines) is that this code appears outside a function. This is a statement:

pt = myArray[0];

Statements must go in functions. Also, if myArray has type unsigned short[], then you want to do one of these instead:

pt = myArray;
pt = &myArray[0]; // same thing

& is the reference operator. It returns the memory address of the variable it precedes. Pointers store memory addresses. If you want to "store something in a pointer" you dereference it with the * operator. When you do that the computer will look into the memory address your pointer contains, which is suitable for storing your value.

char *pc; // pointer to a type char, in this context * means pointer declaration
char letter = 'a'; // a variable and its value

pc = &letter; // get address of letter
// you MUST be sure your pointer "pc" is valid

*pc = 'B'; // change the value at address contained in "pc"

printf("%c\n", letter); // surprise, "letter" is no longer 'a' but 'B'

When you use myArray[0] you don't get an address but a value, that's why people used &myArray[0].


Yeah, you really should include a bit more code so we can see the context.

I don't quite get the error messages, but your code is not correct.

Try:

pt = &myArray[0];

Or:

pt = myArray + 0;

Or just:

pt = myArray;

Instead.

Tags:

C

Arrays

Pointers