0

I tried using struct variables in global scope and local scope in C. When used in local scope ( inside main() ) it gets executed properly.

#include<stdio.h>

struct student{
    char *name;
    int age;
    float avg;
};

struct student s1, s2; #throws error

int main(){
    // struct student s1, s2; #gets executed

    printf("Enter name, age, avg\n");
    scanf("%s %d %f ",s1.name,&s1.age,&s1.avg);

    printf("The values are \n");
    printf("\nName : %s",s1.name);
    printf("\nage : %d",s1.age);
    printf("\navg : %f",s1.avg);
}

What is the reason if I use struct student s1, s2; above main() function it throws error ? If given in local scope it gets executed ?

I want to know the difference of What is happening to the above mentioned case. I also doubt that is it because of pointer variable declared inside struct which causes error?

Error is:

Image of the error thrown

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Why `python` tag? – buran Feb 17 '22 at 10:24
  • 2
    Both are wrong. `s1.name` is an unintialised pointer so writing to it is Undefined Behaviour. UB means behaviour is unpredictable. It may get wrong values, it may crash and it may even appear to "work". – kaylum Feb 17 '22 at 10:25
  • Actually, correction, `name` is intialised to 0 in the global case. But still wrong as obviously that's not a valid buffer address. – kaylum Feb 17 '22 at 10:26
  • Can you please post the correct program 7? @kaylum – shaila balakrishnan Feb 17 '22 at 10:26
  • The case where you claim the program is working, is actually the worse case. Both versions invoke undefined behaviour. But in one version `name` contains `NULL` because global veriables are initialized to 0. In the other version the content is not set to 0 and contains "random" content. If you were lucky, it would also crash but that is not the case here as "undefined behaviour" is not required to crash. – Gerhardh Feb 17 '22 at 10:27
  • 1
    If you don't understand that then it means you probably should go back and read a proper C book or tutorial. Change `char *name` to `char name[MAX_LEN];` where `MAX_LEN` maybe `#define MAX_LEN 64` or use dynamic allocation with `s1.name = malloc(MAX_LEN)`. – kaylum Feb 17 '22 at 10:30
  • @AdrianMole, Am I missing something or what? This has nothing to do with python. The fact that they use `#throws error` and `#gets executed` doesn't make it python, not to mention one of them is part of C comment, starting with `//` – buran Feb 17 '22 at 10:31
  • 3
    @buran What you're missing is my poor attempt at humour. I removed the python tag. – Adrian Mole Feb 17 '22 at 10:32
  • @AdrianMole, sorry, maybe not my best day :-) – buran Feb 17 '22 at 10:32
  • The problem is that writing to the uninitialized pointer `name` invokes undefined behavior, which might manifest itself in strange and inconsistent ways. It's a bug that needs to be fixed regardless of where you place the structs. – Lundin Feb 17 '22 at 10:48

1 Answers1

-2

In C, global/static values are supposed to to have defined values at compile time. What you are doing is just declaring s1 and s2 whose values are not defined thus you are getting the error. What you can do is follow the below snippet.

student s={'shaila',25,18.2};

This should work and the student object s should be initialized now without any errors.

Muhammad Ahmed
  • 318
  • 2
  • 8
  • There are two things that prevents this from even compiling. And even if you corrected those, it would not solve the problem. – klutt Feb 17 '22 at 10:35
  • `'shaila'` that is not how strings are defined. Should be `"shaila"`. But anyway that doesn't really address the OP's issue as the requirement is clearly to read in input not initialise them to some values. – kaylum Feb 17 '22 at 10:36