Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure member assignment causing syntax error when not inside a function

Tags:

c

I want to assign a particular value to a (user defined) global variable in C programming language. When I am doing this from within any other function or main it is fine. But when I am doing it from global space (outside of any function) it is giving the following compilation error:

[expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token]

Following is a code snippet which causing issue:

#include <stdio.h>
#define MAX_SIZE 5

typedef struct
{
    int val[MAX_SIZE];
    int top;
}stack_t;

stack_t s;
s.top = -1;  // <== Initialization from here is causing compilation error

main()
{
    //s.top = -1; < === Initialization from here is fine  
    printf("s.top =%d\n", s.top);
    return 0;
}

But same kind of assignment for integer variables is not giving only warning

#include <stdio.h>

int i,j,k,l;
k=10;
main()
{
        printf("i= %d, j=%d k=%d l=%d\n", i, j, k, l);
        return 0;
}

Can anyone please tell the reason for this?

like image 381
sudhir Avatar asked Dec 27 '14 05:12

sudhir


People also ask

Can you return a structure from a function in C?

A structure can be returned from a function using the return keyword. Structures can be passed into functions either by reference or by value. An array of structures can also be passed to a function.

How do you initialize a struct value?

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign ( = ).

Can we initialize variable in structure?

The direct answer is because the structure definition declares a type and not a variable that can be initialized. Your example is: struct s { int i=10; }; This does not declare any variable - it defines a type.


1 Answers

The error from the assignment of s.top is not surprising. It is not an initialization but an assignment, and those are different concepts in C. You can't have assignments outside of functions.

The interesting part here is that it looks like you can do an assignment of the integer variable k. But that is an illusion, since in that case it's not an assignment but an initialization!

The line

k = 10;

is interpreted not as an assignment but as a variable definition. A hint to this is that GCC gives the warnings "data definition has no type or storage class" and "type defaults to 'int' in declaration of 'k'". So the line could be written as

int k = 10;

As Matt wrote in a comment below, leaving out the data type like this seems to be a GCC extension, and not something that the standard allows.

(By the way, do turn on warnings in your compiler, and pay attention to them!)

But wait, wasn't there already a definition of k on the line above, and you can't have more than one definition, can you?

First, remember that C makes a distinction between definitions and declarations. A definition of a variable is when you "create" the variable, and optionally give it an initial value. A declaration is when you just tell the compiler that the variable exists, and its name and data type, but the definition is elsewhere. You can have both a definition and one or more declarations of the same variable. For example:

int xxx = 10; // This is the definition of xxx
int xxx; // This is a declaration of xxx
int xxx; // Another delaration of xxx

But sometimes the compiler can't determine if what it sees is a declaration or a definition, and then it is interpreted as a "tentative definition", or in other words "perhaps a definition, perhaps just a declaration, we'll decide later". For example:

int yyy;

Is that a definition of the variable yyy (without an initial value), or is it just a declaration, where the definition will be found elsewhere? The compiler doesn't know, so it waits to decide, and calls this a tentative definition.

When the compiler sees your first declaration of k (along with the other variables i, j and l) it is interpreted as a tentative definition, and when the later definition is found, that tentative definition of k will be decided to be a declaration, not a definition.

like image 100
Thomas Padron-McCarthy Avatar answered Oct 05 '22 23:10

Thomas Padron-McCarthy