-1

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()?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 4
    You cannot have code outside functions. Variable definitions, possibly with initialization, are ok (though frowned upon because *global variables* often cause more trouble than they're worth); assignments are not ok. – pmg Feb 18 '22 at 09:07
  • Whatever you need to do, has to be done inside the main function. Otherwise, you need to declare function outside the main, that will do whatever you want them to do and of course, you need to call them in the main function. – wajaap Feb 18 '22 at 09:10
  • The initialization of `struct student s1` outside of `main` as a global variable is fine and compiles but instructions like `strcpy` have to be in the scope of a function like `main` – Odysseus Feb 18 '22 at 09:14
  • @Odysseus Actually, Global variable is mostly evil.. – K.R.Park Feb 18 '22 at 09:19
  • @K.R.Park It might indeed be not advisable to use global variables most of the time but it's still valid `C` – Odysseus Feb 18 '22 at 09:24
  • 3
    Your parameters to `strcpy`are in the wrong order. It should be `strcpy(destination, source);` – mmixLinus Feb 18 '22 at 09:38
  • 1
    C and C++ are very different languages. Please don't tag both unless you're asking about their differences. – molbdnilo Feb 18 '22 at 09:40
  • 1
    This is not specific to structs. You would encounter the same problem with `int x; x = 1; int main() {}`. – molbdnilo Feb 18 '22 at 09:42
  • @EricPostpischil Don't remove the C++ tag once there are C++ answers posted. This should indeed never have been tagged C++, but since someone posted a C++ answer we shouldn't turn that answer off-topic by removing the tag. See C and C++ tag wikis for details. – Lundin Feb 18 '22 at 11:15
  • @Lundin:Answerers should not go by tags alone. OP stated they are a beginner in C programming, so C answers should be given. When somebody makes a mess of a question by answering with a mismatched language, the solution is to clean up the mess, not leave it. – Eric Postpischil Feb 18 '22 at 11:44
  • @EricPostpischil We have tag usage rules and tag moderation guidelines that you too are supposed to follow. From C and C++ tag wikis: "Be careful about re-tagging questions once there are answers posted, particularly if there are already both C and C++ answers posted. In such cases, the tags should be left alone, since changing them would make posted answers invalid." It's very easy for someone only following the C++ tag to stumble upon a question like this and post an answer without realizing the incorrect cross-tagging. – Lundin Feb 18 '22 at 11:49
  • @Lundin: Yes, it is easy for somebody following one tag to happen upon a question and answer it without reading the first sentence. Nonetheless, messes should be cleaned up. – Eric Postpischil Feb 18 '22 at 12:16

2 Answers2

0

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-2

In C/C++, you can't run codes out of function scope. But you can hack it in C++ only 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;

static 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);
}
JohnGu
  • 1
  • 2
  • Your code is not valid C (*may be valid C++, I don't know C++*). In C the initializer of a global variable must be a compile-time constant. – pmg Feb 18 '22 at 09:34
  • pmg say right. This code only vaild in C++. In C, maybe check out https://stackoverflow.com/questions/8713470/executing-code-before-main – JohnGu Feb 18 '22 at 09:44
  • Please note that the question was (incorrectly) tagged with both C and C++ from the beginner. So down-voting it for being only valid in C++ is a bit harsh. Though on the other, hand proper C++ wouldn't use this obscure function but a constructor. – Lundin Feb 18 '22 at 11:19
  • Maybe the downvoting was for using `C/C++` while 1) C and C++ are two very different languages and 2) the solution is no valid C. – Gerhardh Feb 18 '22 at 11:56