expected expression before ‘{’ token c program

▼魔方 西西 提交于 2019-12-11 04:47:12

问题


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:

  1. You should initialize your arrays in the same line you declare them
  2. You must initialize them with array of numbers, not with array of c-strings:
  3. 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 ints. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!