Question

I am attempting to code a gradebook in C. However, with my inexperience in handling pointers, I am getting strange values when printing values to the console. My code is listed below:

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

int main(int agrc,char * argv[]) {
// Create null pointers
char * students = 0;
char * grades = 0;
int * namelen = 0;

// Variable to track class size
int classSize   = 0;


printf("Please enter the class size: ");
scanf("%d",&classSize);
printf("Class size = %d\n",classSize);

// Allocate the memory for the null pointers
students = malloc(classSize * sizeof(char)+classSize);
grades   = malloc(classSize * sizeof(int));
namelen   = malloc(classSize * sizeof(int));

int i = 0;
char * tmp;
int pos = 0;

for(;i<classSize;i++) {
    printf("Please enter student %d's name: ",i+1);

    // Read name into dummy variable
    scanf("%s",tmp);

    // Store the length of the name
    *(namelen + i) = strlen(tmp);

    // Read in the name of the student
    strcpy(students+pos,tmp);

    printf("Please enter %s's grade: ",students + pos);

    // Read in the grade of the student
    scanf("%d",grades+i);

    pos += *(namelen+i)+1;
}

printf("\n");
printf("The data that you entered is as follows:\n");
printf("----------------------------------------\n");

i = 0;
pos = 0;

for(;i<classSize;i++) {
    printf("Student %d: %s. Grade: %d\n",i+1,students+pos,*(grades+i));
    pos += *(namelen+i)+1;
}
}

The problem is, the names and grades of students are not being displayed properly sometimes. I know that this has something to do with the pointers. But I can't seem to spot any error. Could someone please help me? Thanks.

Was it helpful?

Solution

students = malloc(classSize * sizeof(char)+classSize); // allocates one char for each student name. 

This should be replaced with the expected size for each name.

students = malloc(classSize * sizeof(char) * 20); // assuming size of each to max of 20 bytes including null termination.

char * tmp;

This should be

char tmp[20]; // assumed max size of each name is 20 bytes including null.

OTHER TIPS

Updated your code and posted below. This works for me try.

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

int main(int agrc,char * argv[]) {
// Create null pointers
char * students = 0;
int * grades = 0;
int * namelen = 0;

// Variable to track class size
int classSize   = 0;


printf("Please enter the class size: ");
scanf("%d",&classSize);
printf("Class size = %d\n",classSize);

// Allocate the memory for the null pointers
students = malloc(classSize * 20);
grades   = malloc(classSize * sizeof(int));
namelen   = malloc(classSize * sizeof(int));

int i = 0;
char tmp[20];
int pos = 0;

for(;i<classSize;i++) {
    printf("Please enter student %d's name: ",i+1);

    // Read name into dummy variable
    scanf("%s",tmp);

    // Store the length of the name
    *(namelen + i) = strlen(tmp);

    // Read in the name of the student
    strcpy(students+pos,tmp);

    printf("Please enter %s's grade: ",students + pos);

    // Read in the grade of the student
    scanf("%d",grades+i);

    pos += *(namelen+i)+1;
}

printf("\n");
printf("The data that you entered is as follows:\n");
printf("----------------------------------------\n");

i = 0;
pos = 0;

for(;i<classSize;i++) {
    printf("Student %d: %s. Grade: %d\n",i+1,students+pos,*(grades+i));
    pos += *(namelen+i)+1;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top