0
struct employee {
        int employeeId;
        double payrate;
        char *inputname;
    };



    int main (void){

        //remember the struct before employee

        struct employee davidB;
        fgets(davidB.inputname, 20, stdin);
        davidB.employeeId = 1;
        davidB.payrate = 45020.50;


        printf("The employees name is %s\n The employees id num is  %d\n The employees payrate is %f\n", davidB.inputname, davidB.employeeId, davidB.payrate);

        struct employee ericG;

        printf("Please enter employee name, id number, and payrate\n");

        scanf("%s", ericG.inputname);
        scanf("%d", &ericG.employeeId);
        scanf("%lf", &ericG.payrate);

        printf("The employees name is %s\n The employees id num is  %d\n The employees payrate is %f\n", ericG.inputname, ericG.employeeId, ericG.payrate);

        return 0;
    }

My questions involve:

                          fgets(davidB.inputname, 20, stdin);  
                          scanf("%s", ericG.inputname);

Why does the first one work and why does the second one cause stack overflow? Furthermore, when can I directly assign a "string" to a pointer, and when does it need to be a pre-defined char array?

for example:

   const char *string = "Hey"; //works? so can't I do the same with the scanf above?
3
  • 1
    Neither of those should work (UB). inputname is never initialized to point at anything. Commented Jan 28, 2013 at 23:54
  • the fgets reads in the string and then it gets printed, so that one works. Commented Jan 28, 2013 at 23:58
  • Then it's working by accident (probably trashing some random memory location somewhere else). Commented Jan 29, 2013 at 0:00

3 Answers 3

1

You need to allocate memory in order to use those functions, otherwise you get stack overflow since this pointer (davidB.inputname) is pointing to garbage.

One option would be to define it like char inputname[21]; but this will only work for the fgets function since you can limit the number of chars your read. But with scanf there is nothing you can do unless you can be 100% on the size of the string you're about to read so it's better avoid it otherwise.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok thanks, that makes sense. I was really trying to figure out how to make it work with scanf, but if I can't that helps, at least I know
It happens sometimes, but it doesn't mean it works, only that you got lucky.
1

It is just random luck that one way works, but the other does not. Both methods are incorrect, as you are not allocating any memory for inputname to point to. So, the functions are just inputting data to some random memory location.

The const char *string = "Hey" works because this is static data, and the compiler is allocating space for it somewhere.

2 Comments

You need to allocate memory such as davidB.inputname = (char*)malloc(20); you also then need to "free" the memory when done, or you will have memory leaks.
So you can only assign a string to a pointer directly in program?
1

You haven't allocated memory for `inputname. So both invoke undefined behaviour. So both don't work, not in legal C anyway.

But when you directly assign like this:

const char *string = "Hey";

string merely points to a string literal. So there's no need for memory allocation there.

So in this way, you can directly assign to the struct member:

    struct employee davidB;
    davidB.inputname = "Eric"; // Thiis is valid.
    davidB.employeeId = 1;
    davidB.payrate = 45020.50;

Otherwise, you have to dynamically allocate memory for inputname:

    struct employee davidB;
    davidB.inputname = malloc(256);
    fgets(davidB.inputname, 256, stdin); 
    davidB.employeeId = 1;
    davidB.payrate = 45020.50;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.