varunsinghal65
varunsinghal65

Reputation: 556

Error in initializing members of a structure

Following is a program for initializing members of a structure in c

struct stack
{
    int *a;
    int top;
    int size;
}s;

void init()
{

    const int size =10; /*s.size=10;*/  /*const int s.size=10*/
    s.a=(int*)malloc(size*sizeof(int));
    s.top=0;    
}

int main()
{
    init();
    printf("Initialization done !\n");
    return 0;   
}

Q1 : In init method instead of const int size=10 when I wrote s.size=10 , I got an error "size was not declared in scope " , but I have already declared size in stack struct .I was able to initialize top in the same manner Then why the error ?

Q2 : In init method I am getting correct output with const int size=10 . I am confused , in this statement how are we able to access size member of the struct stack without using a struct variable , shouldn't it be const int s.size=10?

Upvotes: 1

Views: 261

Answers (4)

user3629249
user3629249

Reputation: 16540

Note: the following method of defining/declaring a struct is depreciated
struct stack
{
    int *a;
    int top;
    int size;
}s;

The preferred method is:
// declare a struct type, named stack
struct stack
{
    int *a;
    int top;
    int size;
};

struct stack s;  // declare an instance of the 'struct stack' type

// certain parameters to the compile command can force 
// the requirement that all functions (other than main) 
// have a prototype so:
void init ( void );

void init()
{

    s.size =10;
    // get memory allocation for 10 integers
    if( NULL == (s.a=(int*)malloc(s.size*sizeof(int)) ) )
    { // then, malloc failed
        perror( "malloc failed" );
        exit( EXIT_FAILURE );
    }

    s.top=0;    
} // end function: init

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

I think, your confusion is because you've used the same variable name size twice,

  1. as a structure member variable
  2. as a local variable in void init().

Please be noted, these two are seperate variables.

The size member variable in struct stack is a member of the structure. You need to access a member variable by either . or -> operator [Yes, even if the structure is global].

OTOH, the int size in void init() is a normal variable of type int.

Without having a variable of type struct stack, there does not exist a size, which belongs to struct stack. Likewise, there's no way you can access size, which is a member variable in struct stack directly [without using a structure variable of type struct stack] anywhere.

To summarize

Answer 1:

The error is not for replacing const int size=10 with s.size = 10. It's rather from the next line,

s.a= malloc(size*sizeof(int));
              ^
              |

Where, no size variable is present when const int size=10 is removed.

Answer 2

const int size=10 declares and defines a new variable called size. It's not the same as the s.size [member of struct stack]. That's why using

s.a= malloc(size*sizeof(int));
              ^
              |

is valid, as a variable named size is in scope.

Upvotes: 0

sharon
sharon

Reputation: 734

Yes as size is a structure variable, you have to access with the structure variable and initialize it.

if you initialize size =10 it will take as a new variable. because init function will be stored in a separate stack and the scope of variable size will be inside the init function only. Then while allocating the memory you should allocate for the s.size variable.

s.a = malloc(s.size * sizeof(int));

Upvotes: 2

Yu Hao
Yu Hao

Reputation: 122493

s.size=10 has no problem. The problem is when you allocate memory for s.a, there's no variable named size, you should change it to:

s.a = malloc(s.size * sizeof(int));

You seem to be confused about the variable size and the member size in the structure struct stack s, they are not related except having the same name.

Upvotes: 2

Related Questions