0

I am a beginner with programming and I have no idea how to fix my code. I want every students[i] to have the overall variable preset with the characters "none". Every way I have tried to write my code for this has not worked and I get Buffer Overflow and Stack Smashing errors. The problem is my overall variable. I don't know what to do.

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

typedef struct student_tag
{
  char name[20];
  char surname[20];
  int ID;
  float average;
  char overall[20];
} student_tag;

int main(void)
{
  int i;
  student_tag students[i];
  
  for(i=0; i<1; i++)
    {
      scanf("%s", students[i].name);
      scanf("%s", students[i].surname);
      scanf("%d", &students[i].ID);
      students[i].average = 0;
      strcpy(students[i].overall, "none");

      printf("%s\n", students[0].name);
      printf("%s\n", students[0].surname);
      printf("%d\n", students[0].ID);
      printf("%f\n", students[0].average);
      printf("%s\n", students[0].overall);
    }
 
  return 0;
}

I was hoping for my code to give me this output for my overall variable;


none
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Kai
  • 1
  • 2
  • Does [this](https://godbolt.org/z/3o7eE9x6K) help? – Ted Lyngmo May 06 '23 at 12:15
  • 1
    What is your input? Possibly, the problem is with one of the `scanf("%s", ...`, which is a very dangerous piece of code to write. As a beginner, you should avoid scanf. (As an experienced user, you will avoid `scanf`.) – William Pursell May 06 '23 at 12:16
  • 2
    What do you expect `student_tag students[i];` to do? (what is `i`?) – William Pursell May 06 '23 at 12:17
  • I have no idea what I'm doing, honestly. My code would not do anything unless I had the `[i]` at the end. I wanted to be able to have as many students as I needed, I plan on making a menu later for the user to make any amount of students. As for `scanf` , what would you recommend I try instead? – Kai May 06 '23 at 12:24
  • 2
    After the defnition `int i;`, the value of `i` in not determined because it has not been initialized. So `student_tag students[i];` is undefined behavior. Try `student_tag students[128];`. Or, assign some value to `i`. (either at compile time, or read it from the input stream, but it must happen before the array is created). – William Pursell May 06 '23 at 12:30
  • 1
    Instead of `scanf`, the simplest thing to do is probably to read a line of input with `fgets`, and then parse it. You *could* parse it with `sscanf`, but it is more instructive to use `strtol` and friends to read the integers and `strchr` and friends to find the strings. You will learn more doing that, and in the end you usually end up with cleaner code. (scanf has so many hard edges, that getting robust code with it ends up being more complicated that parsing the string with other tools). – William Pursell May 06 '23 at 12:33
  • If the maximum possible number of students is not known at compile time, you should look into dynamic memory allocation i.e. `malloc` and `free` – anton-tchekov May 07 '23 at 00:41

0 Answers0