Home > OS >  assign struct variable inside main()
assign struct variable inside main()

Time:02-19

I am a beginner in C programming. I did a simple structure program. I tried to assign the value of one structure variable to another structure variable. If I define inside main(), it doesn't give an error.

This works fine.

#include<stdio.h>
#include <string.h>

struct student{
        char name[50];
        int age;
        int roll;
        float marks;
};

int main(){
        struct student s1 = {"Nick",21,3,12.41};
        struct student s2, s3;

        strcpy(s1.name,s2.name);
        s2.age = s1.age;
        s2.roll = s1.roll;
        s2.marks = s1.marks;

        s3 = s2;

        printf ( "\n%s %d %d %f", s1.name, s1.age, s1.roll, s1.marks ) ;
        printf ( "\n%s %d %d %f", s2.name, s2.age, s2.roll, s2.marks ) ;
        printf ( "\n%s %d %d %f", s3.name, s3.age, s3.roll, s3.marks) ;

}

But if I assign it outside main(), it throws error. what is the difference?

#include<stdio.h>
#include <string.h>
// USE ASSIGNMENT OPERATOR "=" TO ASSIGN VALUES OF STRUCT
struct student{
        char name[50];
        int age;
        int roll;
        float marks;
};

#why throwing an error?

struct student s1 = {"Nick",21,3,12.41};
struct student s2, s3;

strcpy(s1.name,s2.name);
s2.age = s1.age;
s2.roll = s1.roll;
s2.marks = s1.marks;

s3 = s2; 

int main(){

        printf ( "\n%s %d %d %f", s1.name, s1.age, s1.roll, s1.marks ) ;
        printf ( "\n%s %d %d %f", s2.name, s2.age, s2.roll, s2.marks ) ;
        printf ( "\n%s %d %d %f", s3.name, s3.age, s3.roll, s3.marks) ;

}

Is there any rule that struct variable has to be defined inside main()?

CodePudding user response:

You can't execute code outside functions in C save for initialization which is always done with compile-time constants. This has nothing to do with structs as such, but all kinds of variables.

struct student s1 = {"Nick", 21, 3, 12.41}; outside a function works since it's initialization. struct student s1; ... s1.age = 21; does not work because that's run-time assignment.

However, declaring variables outside functions is generally considered bad practice, so you shouldn't be doing that to begin with.

Also since the struct has no pointer members and similar, there is no need to assign each member individually as you do between s1 and s2. s2=s1; works fine in this case - the only case where you can copy arrays/strings without using functions like strcpy is when they are struct members.

CodePudding user response:

In C/C , you can't run codes out of function scope. But you can hack it by variable initialization. (Totally not recommended)

Variable initialization will be exec before main function.

#include <stdio.h>
#include <string.h>
// USE ASSIGNMENT OPERATOR "=" TO ASSIGN VALUES OF STRUCT
struct student
{
    char name[50];
    int age;
    int roll;
    float marks;
};

struct student s1 = {"Nick", 21, 3, 12.41};
struct student s2, s3;

int variable_init()
{
    strcpy(s1.name, s2.name);
    s2.age = s1.age;
    s2.roll = s1.roll;
    s2.marks = s1.marks;
    s3 = s2;
    return 0;
}

static int _useless_variable = variable_init();

int main()
{

    printf("\n%s %d %d %f", s1.name, s1.age, s1.roll, s1.marks);
    printf("\n%s %d %d %f", s2.name, s2.age, s2.roll, s2.marks);
    printf("\n%s %d %d %f", s3.name, s3.age, s3.roll, s3.marks);
}
  • Related