-2

This is my code on getting details of a student.

#include <stdio.h>

struct det{
    char fname[25], lname[25], shift[10], sec[2];
    int roll, clss, id;
};

int details();

int main(){
    details();

    getchar();
    getchar();

    return 0;
}

int details(){
    char rl;
    FILE *fp;
    struct det n;

    printf ("\n Enter Student Informations Below : \n\n");

    printf (" First Name : ");
    scanf ("%s",&n.fname);

    printf (" Last Name : ");
    scanf ("%s",&n.lname);

    printf (" Roll : ");
    scanf ("%d",&n.roll);

    rl = (char) n.roll + ".txt";

    fp = fopen(rl, "w");

    printf (" ID : ");
    scanf ("%d",&n.id);

    printf (" Class : ");
    scanf ("%d",&n.clss);

    printf (" Shift : ");
    scanf ("%s",&n.shift);

    printf (" Section : ");
    scanf ("%s",&n.sec);

    // Works fine till here. Shows in console that segmentation fault, core dumped.

    fprintf (fp, "\n Name : %s %s\n", n.fname, n.lname);
    fprintf (fp, " Class : %d\n Roll : %d\n ID : %d\n", n.clss, n.roll, n.id);
    fprintf (fp, " Section : %s\n Shift : %s\n", n.sec, n.shift);

    fclose(fp);

    printf ("\n\n Details Stored.\n\n Press Enter To Exit...");

    return 0;
}

Works fine till the marked line. But then i get this message on console 'Segmentation fault. Core(Dumped). Can someone please tell me what's wrong in the code and how to fix it?

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
Anik Shahriar
  • 151
  • 1
  • 11
  • Have you run your code through a debugger? Or with something like valgrind? –  Nov 12 '16 at 12:26
  • Have you looked at any compiler warnings? Make sure to turn them all on, and don't ignore them if you're not sure about them. –  Nov 12 '16 at 12:28
  • At least format your code correctly. Even better, make some effort to try and understand where your program is going wrong (by using a debugger or inserting debugging statements), and include results of such investigation in the question. – davmac Nov 12 '16 at 12:31

1 Answers1

1

Problem #1

char fname[25];
scanf ("%s",&n.fname);

As explained elsewhere, this might seem to work, but it is wrong. Use scanf("%s", fname);

Problem #2

rl = (char) n.roll + ".txt";

This statement does not concatenate strings, as you probably meant to do; the binary + operator in C does not work like that. What is happening here:

  • ".txt" is an expression of type "pointer to char", that points to the first element of the sequence (C11 6.3.2.1p3)
  • (char) n.roll casts an int to a narrower integer type, char, and thus discards some information. If you need a char, use a char, otherwise document its purpose
  • (char) n.roll is of integer type, therefore if its value N is in the range [0, 4], the result is of type "pointer to char" and points to the N-th element of the array object, otherwise the behaviour is undefined (C11 6.5.6p8)
  • the result is assigned to rl, which is of type char, which means you are trying to assign a pointer to an integer (and should be seeing a warning that says exactly that)

If you wanted to safely create a character string, which contains a string of the form "NNN.txt", you use string formatting functions like snprintf:

char buffer[MAXIMUM_FILENAME_SIZE];
snprintf (buffer, sizeof buffer, "%d.txt", n.roll);

Then check for errors and handle them appropriately.

Community
  • 1
  • 1
Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123