Buffer Overflow with structures - C

79 Views Asked by At

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
0

There are 0 best solutions below