Thread: I'm getting an ": error: expected expression before ‘{’ token"

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    I'm getting an ": error: expected expression before ‘{’ token"

    Hi all,
    This is my simple ansi C code:
    Code:
       int         x, MAT[9]={};
       x = nodeinfo.nodenumber;
       switch(x) {   
            case '0':MAT[] = {1,1,2,1,0,0,0,0};       
                    break;
            case '1':MAT[] = {1,1,2,2,3,1,0,0};
                    break;
            case '2':MAT[] = {1,2,2,1,0,0,0,0};
                    break;
            default:printf("no node available\n");
         }
    But I'm getting this compiling error:

    xx.c:12: error: expected expression before ‘{’ token
    xx.c:13: error: expected expression before ‘{’ token


    Can you please let me know what is wrong?

    thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't assign values to an array dynamically - you can initialize an array when it's created, but once it's been set, you need to use other methods (such as a loop that sets each element or copy from somewhere with memcpy()).

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Thank you Mats! you are right!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I noticed that you're using '0' and '1' etc for the switch's case values. If x is storing a character (such as the return value of getchar()), that's what you want. But if x is storing an ordinary number, you want to use 0 and 1 etc for the case values.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Hi,
    Sorry, but I'm afraid that I need your help again...

    Definitly, I have a conceptual problem which is not described in my books.
    All what I did is included in my C books (or at least that's what I underestand so far..)

    What I wanna do is just a switch or if loop wich -according with a variable I get (x)- then I set some values in an empty defined array.
    Code:
    1 int x;
    2
    3 x = number; /*here "number" is an int which I get from another function*/
    4
    5 switch(x) {
    6   case 0: int MAT[]={1,2,3,4,5}; /*Initializing the content of an array when it is declared */
    7     break;
    8   case 1: int MAT[]={3,4,5,6,7};
    9     break;
    10 default: printf("none selected\n");
    11 }
    

    Here I'm getting an error in lines 6 and 8 saying:

    error: expected expression before '{' token
    error: expected expression before '{' token

    I've also tryied setting element by element , but I get the same error.

    I tried also using "if-else if" , but the same error message in each line!!

    Thanks for your help!!
    Last edited by billy1; 08-11-2007 at 03:20 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. This is C, so there is no declaring variables in the middle of the code.

    2. Even if you use braces, it doesn't help
    Code:
        case 0: {
            int MAT[]={1,2,3,4,5};
            break;
        }
    because the array goes out of scope as soon as the case breaks.

    To initialise an array at some outer scope (as per your first example), then you need to write it out the long way, say
    MAT[0] = 1;
    MAT[1] = 2;
    MAT[2] = 3;
    etc

    Or declare another array and copy it
    Code:
        case 0: {
            int MAT0[]={1,2,3,4,5};
            for ( i = 0 ; i < 5 ; i++ ) MAT[i] = MAT0[i];
            break;
        }
    Or if your cases are always 0, 1, 2 etc, consider a 2D array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Quote Originally Posted by matsp View Post
    You can't assign values to an array dynamically - you can initialize an array when it's created, but once it's been set, you need to use other methods (such as a loop that sets each element or copy from somewhere with memcpy()).

    --
    Mats
    I'm still confuse your explain..
    I get error on c program..

    Code:
    void cal_fft(x,y,l,mode)
         float *x[],*y;
         int l,mode;
    {
      int np,lmx,lo,lix,lm,li,j1,j2,nv2,npm1,i,j,k;
      float scl,arg,c,s,t1,t2;
      
    //  --x;
    //  --y;
      x[256] = { /*i got error error: expected expression before ‘{’ token
    */
    455,
    699,
    831.5,
    357.7, ..etc, because so many }: 
    
      y=0;
    
      np=(int)pow(2.0,(float)l);
      lmx=np;
      scl=(float)(6.283185303/(float)np);
      for(lo=1;lo<=l;++lo){
        lix=lmx;
        lmx=lmx/2.0;
        arg=0.0;
        for(lm=1;lm<=lmx;++lm){
          c=cos(arg);
          s=mode*sin(arg);
          arg=arg+scl;
          for (li=lix; lix<0 ? li>=np : li<=np; li+=lix){
    	j1=li-lix+lm;
    	j2=j1+lmx;
    //	t1=x[j1]-x[j2];
    	t2=y[j1]-y[j2];
    //	x[j1]=x[j1]+x[j2];
    	y[j1]=y[j1]+y[j2];
    //	x[j2]=c*t1+s*t2;
    	y[j2]=c*t2-s*t1;
          }
        }
        scl=2.0*scl;
      }
    
      j=1;
      nv2=(int)(np/2.0);
      npm1=np-1;
      for(i=1;i<=npm1;++i){
        if(i>=j)
          goto L30;
    //    t1=x[j];
        t2=y[j];
    //    x[j]=x[i];
        y[j]=y[i];
    //    x[i]=t1;
        y[i]=t2;
      L30:
        k=nv2;
        
      L40:
        if(k>=j)
          goto L50;
        
        j-=k;
        k=(int)(k/2.0);
        goto L40;
      L50:
        j=j+k;
      }
    }
    can you explain it why that error, with syntax

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    nendariani, this thread is dead, and your post adds nothing of importance to it.

    Ask your question in a new thread. Oh, and while you are at it, post what you are trying to do, and how does it not work.

    *thread closed*

    EDIT:
    Okay, I see that you actually stated how it does not work, but in the code itself. Nonetheless, ask it more clearly in a new thread... and did you actually write the code? It looks ancient with the old-style of parameter declaration.
    Last edited by laserlight; 09-14-2008 at 08:39 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: expected expression before ‘{’ token
    By nendariani in forum C Programming
    Replies: 7
    Last Post: 09-14-2008, 11:27 AM
  2. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM