Thread: error: expected expression before ‘{’ token

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    error: expected expression before ‘{’ token

    get error on c program..
    Code:
    void cal_fft(x,y,l,mode)
         float *x[256],*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 = {
    455,699,831.5,357.7,-156.7,-238.8,-243.5,-412.3,-455.7,-347.7,-436.2,-597.1,-516.3,-488.6,-601.1,
    -304.2,344.2,685.1,843.8,1014.5,722.2,60.5,-385.9,-822.1,-1358.5,-1381,-864.9,-435.4,-42.2,525.6,
    724,582.5,670.4,766,506.1,432.8,567.8,261.1,-271,-632.5,-1229.6,-1800.1,-1677.5,-1329.8,-873.2,
    82.6,671.5,859.6,1173.8,666.5,-59.3,-506.5,-1125.1,-1478.1,-1315.5,-1007.6,-678.5,-140.3,318.5,
    503,729.5,754.2,422.2,191.9,-63.2,-346.2,-297,-6.3,657.1,1712,2352.2,2548.8,2644.3,2288.2,1723.3,
    1619.6,1776.1,2122,3101,3739.4,4246.8,4780.5,4628.5,4651.2,4518.3,3444.9,-41.2,-7537.9,-11870.6,-9759.9,
    -8684,-7629.3,-1460.7,3680.4,4703,7312.7,8529.8,3245.5,-1701.8,-2031.6,-2455.7,-4492.4,-4684.7,
    -3794.4,-4865.1,-5996.2,-4903.6,-4551.7,-5013.1,-1892.3,3361.7,5620.1,6537.8,7446.2,4774.1,197.3,
    -2400.5,-5046.5,-8156,-7590.2,-4451.3,-2433.4,-157.2,2490.4,2986.4,2425,3183.2,3229.6,2024,1947.7,
    2225.8,697.1,-888,-1956.2,-4110.4,-5690.2,-5344.3,-4965.9,-3516.8,-337.3,1699.7,2876.6,3640.4,
    2367.7,118.7,-1780.8,-3350.2,-4233.3,-3462.1,-2093.9,-614,862.7,1544.2,1581.2,1572.5,1027.2,249.9,
    -261.2,-814.8,-1182.2,-912,-266.6,868.4,2467.6,3807.2,4182.5,3961.8,3271.8,2405,1657.9,1448.1,1578.7,
    2175.9,2994.8,3653.2,4328,4616.3,4509.9,4513.1,3988.7,2708.3,-1204.8,-7215.8,-9026.1,-7237,-6753.7,
    -5006.4,-379.3,2228,3011.5,4804.9,4647.9,1273.9,-856.5,-827.9,-1434,-2396.9,-2198.4,-2052.1,-2773.7,
    -2883.5,-2293.6,-2303.4,-2033.6,-242.7,1535.7,2228.9,2786.6,2756.3,1407.3,49.8,-719.3,-1766.1,-2503.3,
    -2056.6,-1330.8,-830.3,-62.7,551.1,537.4,545.2,837.4,720.1,499.5,615.6,501.4,112.9,-128.6,-388.4,
    -835.9,-1084.5,-1204.6,-1189.6,-678.3,-22.5,422.5,763.6,746.3,433.1,82.4,-272.4,-532.6,-562.6,
    -426.4,-214.7,0,132.2,157.1,158,134.3,86.4,49.7,0.1,-53,-82.1,-61.5,		};
      y=0;
      
      /* ====== radix-2 fft ====== */
      
      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;
      }
      
      /* ====== bit reversal ====== */
      
      
      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;
      }
    }
    please help..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Initialise the array x before calling the function, then pass x to the function. Incidentally, if x (outside of the function) is an array of pointers to float, trying to initialise it with an array of double (or float) will still not work.
    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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Initialise the array x before calling the function, then pass x to the function. Incidentally, if x (outside of the function) is an array of pointers to float, trying to initialise it with an array of double (or float) will still not work.
    I've change with ..
    Code:
    void cal_fft(float *x, float *y,int l,int 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;
    float x = {
    455,699,831.5,357.7,-156.7,-238.8,-243.5,-412.3,-455.7,-347.7,-436.2,-597.1,-516.3,-488.6,-601.1,
    -304.2,344.2,685.1,843.8,1014.5,722.2,60.5,-385.9,-822.1,-1358.5,-1381,-864.9,-435.4,-42.2,525.6,
    724,582.5,670.4,766,506.1,432.8,567.8,261.1,-271,-632.5,-1229.6,-1800.1,-1677.5,-1329.8,-873.2,
    82.6,671.5,859.6,1173.8,666.5,-59.3,-506.5,-1125.1,-1478.1,-1315.5,-1007.6,-678.5,-140.3,318.5,
    503,729.5,754.2,422.2,191.9,-63.2,-346.2,-297,-6.3,657.1,1712,2352.2,2548.8,2644.3,2288.2,1723.3,
    1619.6,1776.1,2122,3101,3739.4,4246.8,4780.5,4628.5,4651.2,4518.3,3444.9,-41.2,-7537.9,-11870.6,-9759.9,
    -8684,-7629.3,-1460.7,3680.4,4703,7312.7,8529.8,3245.5,-1701.8,-2031.6,-2455.7,-4492.4,-4684.7,
    -3794.4,-4865.1,-5996.2,-4903.6,-4551.7,-5013.1,-1892.3,3361.7,5620.1,6537.8,7446.2,4774.1,197.3,
    -2400.5,-5046.5,-8156,-7590.2,-4451.3,-2433.4,-157.2,2490.4,2986.4,2425,3183.2,3229.6,2024,1947.7,
    2225.8,697.1,-888,-1956.2,-4110.4,-5690.2,-5344.3,-4965.9,-3516.8,-337.3,1699.7,2876.6,3640.4,
    2367.7,118.7,-1780.8,-3350.2,-4233.3,-3462.1,-2093.9,-614,862.7,1544.2,1581.2,1572.5,1027.2,249.9,
    -261.2,-814.8,-1182.2,-912,-266.6,868.4,2467.6,3807.2,4182.5,3961.8,3271.8,2405,1657.9,1448.1,1578.7,
    2175.9,2994.8,3653.2,4328,4616.3,4509.9,4513.1,3988.7,2708.3,-1204.8,-7215.8,-9026.1,-7237,-6753.7,
    -5006.4,-379.3,2228,3011.5,4804.9,4647.9,1273.9,-856.5,-827.9,-1434,-2396.9,-2198.4,-2052.1,-2773.7,
    -2883.5,-2293.6,-2303.4,-2033.6,-242.7,1535.7,2228.9,2786.6,2756.3,1407.3,49.8,-719.3,-1766.1,-2503.3,
    -2056.6,-1330.8,-830.3,-62.7,551.1,537.4,545.2,837.4,720.1,499.5,615.6,501.4,112.9,-128.6,-388.4,
    -835.9,-1084.5,-1204.6,-1189.6,-678.3,-22.5,422.5,763.6,746.3,433.1,82.4,-272.4,-532.6,-562.6,
    -426.4,-214.7,0,132.2,157.1,158,134.3,86.4,49.7,0.1,-53,-82.1,-61.5
    };
    float  yx=0;
      
      /* ====== radix-2 fft ====== */
      
      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;
      }
      
      /* ====== bit reversal ====== */
      
      
      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;
      }
    }
    I got alot warning such as
    Code:
    fourier-asli.c:87: warning: excess elements in scalar initializer
    fourier-asli.c:87: warning: (near initialization for ‘x’)
    It's so many warning on code float x. I think if warning it's no problem. But the output is none, I think I've some error because so many warning I can't scroll again..

    help !!
    Last edited by nendariani; 09-14-2008 at 10:52 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you declare "float x = " you are declaring a new variable that has nothing to do with the array of floats that were passed in to your function (which is therefore no longer accessible to you). It is not possible to use initializer notation to set values of a passed-in array.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    x and y are pointers. Are you sure the --x, --y is what you want? You are changing an address which is a bad idea.
    As tabstop said this kind of initialization won't work. But let me ask you this. Why would you want to initialize a string like that inside the function. It is something fixed. Which means that the array x will be intialized every time with the same values. Which also means that x must have a specific size. Which means that x is actually something standard, something constant.
    So what you should do is this. You should declare a const float x[] and initialize it as you did. Then just copy if you want the values of x to another float xx[] (not constant) and pass it to the function and do your job.

    My point is this:
    1) You won't have to rewrite the whole initialization. The other way is to write x[0] = 43242, x[1] = 3213432 etc etc. But it not that nice as a code. And you don't really get a better performance
    2) If you have something constant is better to declare it with the ease of the initialization C offers you. Then you can do whatever you want with that data.
    3) A good idea is to give as a paramter floax x[number_of_elements_initialized] since you want to initialize an array of a specific number of elements, thus the compiler will warn you of any possible error. Giving float *x as a parameter means that any array of float can be passed

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Quote Originally Posted by tabstop View Post
    If you declare "float x = " you are declaring a new variable that has nothing to do with the array of floats that were passed in to your function (which is therefore no longer accessible to you). It is not possible to use initializer notation to set values of a passed-in array.
    It is not possible to use initializer notation to set values of a passed-in array
    Did you mean like
    Code:
    x[256] = { ... }
    I've try but same with topic error: expected expression before ‘{’ token

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    3) A good idea is to give as a paramter floax x[number_of_elements_initialized] since you want to initialize an array of a specific number of elements, thus the compiler will warn you of any possible error. Giving float *x as a parameter means that any array of float can be passed
    Actually, the number of elements in brackets is ignored when part of a parameter. An array is converted to a pointer to its first element when passed as an argument, so it really is just a pointer.

    Quote Originally Posted by nendariani
    I've try but same with topic error: expected expression before ‘{’ token
    The initializer notation is the { ... } part. By the time x is in the function, it has been initialised, so you simply cannot initialise it any more. You can only assign to its elements.
    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

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    I've tried
    Code:
     float x[256] = ...
    and successful
    thank you all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Parsing and Tokens (strtok)
    By readerwhiz in forum C Programming
    Replies: 6
    Last Post: 04-22-2002, 09:57 AM