Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   I'm getting an ": error: expected expression before ‘{’ token" (http://www.go4expert.com/forums/im-getting-error-expected-expression-t5793/)

billy1 11Aug2007 00:59

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.

DaWei 11Aug2007 03:09

Re: I'm getting an ": error: expected expression before ‘{’ token"
 
Well, in the first place, it isn't smaht to show a line number if you don't show enough code for us to count lines, or at least highlight the line as YOU know it. We're not sitting in your chair looking at your screen.

Secondly, the construct, MAT[] = {1,1,2,1,0,0,0,0}; is not a workable runtime operation. It is only good at compile time, when the compiler does you a favor by converting it into an initialization sequence (which becomes a permanent part of the program prior to its conversion to actual executable code).

In C/C++ arrays are not an atomic thing that can be assigned to in one swell foop. They are a collection of individual items that must be treated individually. Your code is NOT ansi C code, despite your claims to the contrary.

billy1 11Aug2007 14:41

Re: I'm getting an ": error: expected expression before ‘{’ token"
 
Thank you for you quick answer!
But I still have some questions. Defenitly, I have a conceptual problem which is not described in my books.
All what I did is 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.

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 as you suggested, but I get the same error en each line.

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

Thanks for your help!!

billy1 11Aug2007 14:51

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

Originally Posted by billy1
Thank you for you quick answer!
But I still have some questions. Defenitly, I have a conceptual problem which is not described in my books.
All what I did is 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.

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 , In my boooks this a correct way to declare an array.. */
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 as you suggested, 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!!

:confused:

DaWei 11Aug2007 18:44

Re: I'm getting an ": error: expected expression before ‘{’ token"
 
Okay, listen up. You can't use that type of assignment at run time, only at compile time. The compiler can do that for you, just once, when you compile. The microprocessor cannot do that for you at run time, when it determines what the value of "x" is.

Let me give you another example. You may put, at the top of your program, a statement like
Code:

char myText [] = "This is my text";
That only works because the compiler writers are doing you a favor. You cannot assign a bunch of characters to an array in C/C++. You have to assign them one at a time. In the foregoing example, the compiler runs all the code necessary to assign the characters, individually. It then stores the result permanently, as a series of characters, in the executable file. When the program is launched that pattern is put into memory BEFORE the executive calls your "main" function. It is then there for your program to work with, as it runs. It is impossible for the compiler to know what "x" is before the program runs. It cannot, therefore, put "This is my text" and "That was my text" in the memory location which has been named "myText".

I'm sorry if this is confusing to you. You will either have to take my word for it, or think about it, or back your *** off into assembly language and try to put two different values in the same variable at the same time. It ainna gonna woik, Bubba.

Nognir 27Jun2008 18:50

Re: I'm getting an ": error: expected expression before ‘{’ token"
 
Hey guys, I've got a similar problem here. I'm trying to make a program to calculate a polyonym but I'm getting an error. Here's the code:
Code:

#include <stdio.h>
#include <stdlib.h>
#define epsilon = 0.0001;

int p_grade, p_2nd, p_1st, p_zero, i, *p;
float x1,x2,x_0, err, x_mid;

float force (float x, int y)
{
        int i, j=1;
        for (i=1; i < y; i++) j*=x;
        return j;
}

float f (int x)                        /*Ορισμός συνάρτησης πολυωνύμου*/
{
        int i, j=0;
        for (i=0; i<p_grade; i++)
        j += p[i]*force(x, p_grade);
        return j;
}                                                                /*Τέλος ορισμού*/
float root (float a, float b)
{
        float f1, f_mid, x_mid, err;
        f1 = f(a);
        if (f(f1*(f(b)))>0) err = 1.0;
                else
                        {
                                err = 0;
                                x_mid = (a+b)/2;
                                while (abs(a - x_mid) > epsilon)
                                        {
                                                f_mid = f(x_mid);
                                                if ((f1 * f_mid) <= 0) b = x_mid;
                                                        else
                                                        {
                                                                a = x_mid; f1 = f_mid;
                                                        }
                                                x_mid = (a+b)/2;
                                        }
                        }        return x_mid;
}
void main()
{
        printf("ΒΑΘΜΟΣ ΠΟΛΥΩΝΥΜΟΥ?:");  scanf("%d", &p_grade);        /* Εισαγωγή των δεδομένων της άσκησης*/
        p = (int *) malloc(p_grade * sizeof (int));
        if (p == NULL)
            {
           
            }
        else
            {
           
            }
        for (i = p_grade; i >= 0; i--) {printf("ΣΥΝΤΕΛΕΣΤΗΣ ΟΡΟΥ i?:"); scanf("%d", &p[i]);}

        printf("Η ΤΙΜΗ Χ1 είναι?:");        scanf("%f", &x1);
        printf("Η ΤΙΜΗ Χ2 είναι?:");        scanf("%f", &x2);        /*Τέλος της εισαγωγής*/
        free(p);
}

I get that output:
Code:

gcc -O3 ask3.c
ask3.c: In function ‘root’:
ask3.c:31: error: expected expression before ‘=’ token
ask3.c: In function ‘main’:
ask3.c:44: warning: return type of ‘main’ is not ‘int’

Any help here? I'd be grateful

open_mind_core 29Jun2008 12:30

Re: I'm getting an ": error: expected expression before ‘{’ token"
 
Your epsilon definition is wrong.

it should be
#define epsilon 0.0001,
not
#define epsilon = 0.0001;


All times are GMT +5.5. The time now is 15:17.