0

I am rather new to C and am probably doing something stupid but I can't find out why my string array isn't keeping the previous values while the grade is keeping all the values entered:

for(int i=0; i <noOfStudents; i++)
{
    do{
        printf("Please enter Student Full Name.");
        //read string till enter
        scanf(" %[^\n]", &studentFullName);
        if(studentFullName == "")
        {
            printf("Invalid Student Name!\n\n");
            getchar();
        }
        else
        {
            names[i] = studentFullName;
            validStudentName = 1;
        }
    }while(validStudentName != 1);

    do{
        printf("Please enter Student Grade.");
        scanf_s(" %d", &grade);
        if(grade < -1 && grade >100)
        {
            printf("Invalid Student Grade!\n\n");
            getchar();
        }
        else
        {
            grades[i] = grade;
            validGrade = 1;
        }
    }while(validGrade != 1);

    printf("\n");
}
3
  • 3
    You 'd be surprised to learn that studentFullName == "" does not work. You need to use strcmp instead, or in this case the more suitable if(!strlen(studentFullName)). Commented Dec 2, 2011 at 23:23
  • 1
    Could you tell us where you allocate studentFullName and how you do it? Commented Dec 2, 2011 at 23:26
  • Also you might want to think about the expression that you are using to attempt to check for invalid grades... Commented Dec 2, 2011 at 23:32

4 Answers 4

2

In short, pointers. You're just assigning the same pointer to each element in the names array. You need to allocate separate strings for each one.

Additionally, you have a bug: studentFullName == "" will never return true. You should be using strcmp(studentFullName, "") == 0.

Oh, and additionally, you should be using scanf(" %[^\n]", studentFullName);, without the reference operator, since otherwise you're doing something incredibly undefined with your memory, assuming studentFullName is allocated as a char *.

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

1 Comment

Thank you for the explanation on string comparisons in C also my char is 40 is that ok?
1

grade is an integer. When you do:

int a;
int b = 3;
a = b;
b = 4;

then a continues to be 3, because integers are copied.

studentFullName is, however, a pointer. Just like integers, pointers are copied. However, the data the pointer points to is not. So, basically, you've got your entire names[] array as a bunch of pointers, all pointing to the exact same data.

There are several functions that will copy the pointed-to data for you, the one you're looking for is probably strdup. Note that you must free all the pointers you get back from strdup.

1 Comment

Thanks for mentioning the strdup it saved me since i couldn't understand what I am supposed to do.
1

You have to use strcmp or stricmp to compare strings in C because studentFullName is simply the location of the start of the character buffer in memory.

It's not valid to write:

if(studentFullName == "")

Instead, you can write

if(strcmp(studentFullName, ""))

Similarly, names[i] = studentFullName; isn't making a copy of studentFullName, it's setting names[i] to point at the studentFullName buffer, which you overwrite each time through the loop.

To make copies of "strings" (or better thought of as character buffers), you should use strcpy.

Comments

1

You write:

if(grade < -1 && grade >100)

I think you want to get the grade between -1 and 100. "&&" means "and", as your code, it means grade should less-than "-1" and greater-than "100" at the same time. So, it always equal to :

if (false)

So the "else" condition is always running.

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.