Reputation: 75
I have an external variable char myArr[3]
and I am trying to assign something to it. For example, inside my function, I have, myArr[3] = {1,2,3}
. The compiler tells me that I have "unexpected token: =", but as soon as I declare my variable locally, (myArr[3] = {1,2,3}
), the error disappears. I tried masking the external variable inside my function with no results.
char myArr[3];
void my func(){
myArr = {1,2,3}
}
Upvotes: 0
Views: 93
Reputation: 10613
When you do
char myArr[3] = { 1, 2, 3}
You are creating an array of 3 characters, and setting them to the values 1, 2 and 3 respectively.
When you do
myArr[3] = { 1, 2, 3 }
you are trying to set myArr[3]
(which is the fourth character in an array of three characters by the way; you are going "out of bounds") to { 1, 2, 3 }
which doesn't make sense. In C you cannot set arrays like that: it's a syntax error.
If you want to set your array to the values 1, 2 and 3 try this:
myArr[0] = 1;
myArr[1] = 2;
myArr[2] = 3;
Upvotes: 3