0

I wrote below code for reading/printing the structure using pointer. The application is printing the data, but later crashes. Seems to be some silly mistake somewhere. Unable to find the exact problem. I tried using pass by value in printStruct and returning structure in readStruct. There is no issue. When I use pointer not sure what is going wrong.

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

struct  student{
    char USN[10];
    char name[50];
    char gender;
    uint8_t age;
};


void printStruct (struct student*);
void readStruct(struct student*);


int main() {

    uint8_t numStudents;
    printf("Enter number of students\n");
    scanf("%d",&numStudents);
    struct  student firstSemStudent[numStudents];
    readStruct(firstSemStudent+0);
    printStruct(firstSemStudent+0);
    return 0;
}

void printStruct (struct  student *var1)
{
    printf("USN= %s\n",var1->USN);
    printf("Name = %s\n",var1->name);
    printf("Age = %d\n",var1->age);
    printf("Gender = %c\n",var1->gender);
}

void readStruct(struct student *temp)
{
    printf("\nEnter USN: ");
    fflush(stdin);
    gets(temp->USN);
    printf("\nEnter Name: ");
    fflush(stdin);
    gets(temp->name);
    printf("\nEnter age\n");
    scanf("%d",&temp->age);
    printf("\nEnter the Gender as M/F");
    fflush(stdin);
    scanf("%c",&temp->gender);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Rajesh
  • 1,085
  • 1
  • 12
  • 25

1 Answers1

1

First of all, uint8_t * is not a valid argument type for %d format specifier for scanf(). Using wrong type of argument invokes undefined behavior.

After that, technically speaking, fflush(stdin); is again, undefined behaviour, don't do it.

Thirdly, DO NOT use gets(), it is dangerous due to easy possibility of buffer overrun. use fgets() instead.

Fix all the issues (or rather, the warning messages your compiler is screaming to complain about), and run the code.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • It worked after correcting the issues except fflush(). But my questions are: Why there is a gets() and fflush() if they do not work? How one has to know such issues? Scanf also gives trouble if we do not use fflush. I am really confused which is the best way to read from stdin? Also how can I read data for uint8_t? Assume that I do not want to go for int or short int. – Rajesh Nov 28 '16 at 14:52
  • What about format specifiers for unsigned char? I do not think it is there. How do we read 8 bit value without making use of %c? Also I read somewhere that uint8_t, uint16_t etc are standard way of declaring variables because they are independent of architecture. Any comments! – Rajesh Nov 28 '16 at 16:41
  • @Rajesh I have deleted earlier comment, which might be misleading. The point was, `scanf()` does not have a defined format specifier for `uint_t` types, instead, you should be using `SCNuN` MACROs, as specified in the standard. You'll be needing `` for doing that. – Sourav Ghosh Nov 28 '16 at 17:22