Josefhu15
Josefhu15

Reputation: 83

I can't seem to print out a string from a structure but I can print an integer

My code is suppose to get the names of students and the student grade. After that I try to print the student names from the structure that I made and I can only get the grade to print. I get an error when trying to print the string using

printf("%s", test[0].names);

and the error says,

Unhandled exception at 0x0fe113af (msvcr100d.dll) in StudentNamesAndGrades.exe: 0xC0000005: Access violation reading location 0x65736f4a.

But when I use

printf("%d", test[0].studentScores);

It prints out the score of the first student. Here is the entire code because it might be something other than the way I'm trying to print it out.

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

/*
    this program will get a name of students
    and then you will enter the grade for each one :)
*/

struct students
{
    char *names;
    int studentScores;
};


int main(void)
{
    int numStudents = 0;
    students *test;
    int i;

    printf("Enter the number of students in your class: ");
    scanf("%d", &numStudents);

    test = (students*)malloc(numStudents * sizeof(students));

    printf("Enter the names of the %d students\n", numStudents);

    for (i = 0; i < numStudents; i++)
    {
        printf("Enter the name of student %d: ", i + 1);
        scanf("%s", &test[i].names);
        printf("Enter the students score: ");
        scanf("%d", &test[i].studentScores);
    }

    printf("%d", test[0].studentScores);
    printf("%s", test[0].names);    // This is where I get a problem :/

    return 0;
}

Upvotes: 2

Views: 241

Answers (2)

Sadique
Sadique

Reputation: 22841

You did not allocate memory for char *names; while taking input at all.

Your struct could be like:

typedef struct students
{
    char names[30];
    int studentScores;
}students;

Also using fgets is safer than using scanf.

Upvotes: 4

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53026

You need to allocate space for the names field, but I would recommend a different approach

struct students
{
    char names[100];
    int studentScores;
};

and then change the scanf() to

scanf("%99s", test[i].names);

there is another mistake in your first scanf() your are passing the address to the pointer, instead of the pointer.

You should use the address of & operator for the integer, because you need to pass a pointer to an integer for the "%d" specifier, but your names field variable was already a pointer, so no neet to take it's address.

Some other tips you might be interested in

  1. Don't cast the result of malloc, although it seems that you are erroneously using a c++ compiler to compile c code, and in c++ you do need the cast, in c you don't, it makes your code harder to read and other problems which you can read with the most popular c question on Stack Overflow.

  2. Check the return value from malloc, it doesn't matter how unlikely it could fail, since it could theoretically fail, you must check it's return value, which is NULL on failure.

Upvotes: 1

Related Questions