问题
I wrote following code I am solving a puzzle I when compiled this part of code
#include <stdio.h>
int main ()
{
int a[10],b[10],c[10];
int i,j,k,l;
a[10]={"21","33","12","19","15","17","11","12","34","10"};
b[10]={"10","15","9","13","16","21","15","32","29","7"};
c[10]={"11","8","3","6","1","4","6","20","19","3"};
l=sizeof(a)/sizeof(a[0]);
for (i=0;i<=l;i++)
{
}
}
gives me error
array.c: In function ‘main’:
array.c:7:8: error: expected expression before ‘{’ token
array.c:8:8: error: expected expression before ‘{’ token
array.c:9:8: error: expected expression before ‘{’ token
Why is the error coming here?
回答1:
There's several problems in your code:
- You should initialize your arrays in the same line you declare them
- You must initialize them with array of numbers, not with array of c-strings:
- You actually try to set value to 11'th element of the array.
Correct line of code will be:
int a[10] = {21,33,12,19,15,17,11,12,34,10};
回答2:
You're setting the eleventh element of the array to an array.
Try this:
int a[10] = {21,33,12,19,15,17,11,12,34,10};
回答3:
Type mismatch: You are setting character literal strings to an
int
s. This is a no-no.You are accessing one past the end of the array -- a classic off-by-one error.
回答4:
this is int array don' put qoutes for numbers
c[10]={11,8,3,6,1,4,6,20,19,3};
来源:https://stackoverflow.com/questions/10683147/expected-expression-before-token-c-program