craigcarpenter2
craigcarpenter2

Reputation: 35

Printing info from an array of structs in C

In my homework assignment for a programming paradigms class (learn only a little bit about many languages), we have been tasked with creating a student structs, creating an array of those structs, and then printing that information. Below is what I have so far.

#include <stdio.h>
struct student
{
  char name;
  int age;
  double gpa;
  char gradeLevel;
};

int main ()
{
  student struct classStudents[];

}

When I run this code i get an error saying "main.c:12:3: error: ‘student’ undeclared (first use in this function)".

Please help.

Upvotes: 0

Views: 52

Answers (1)

some user
some user

Reputation: 1775

Your type declaration is wrong for a structure type. Use this:

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

#define ST_SIZE 10
#define MAXNAME 64      /* don't skimp on buffer size */  //suggested by @David C Rankin

struct student {
    char name[MAXNAME]; // you want to store more than a character just,right?
    int age;
    double gpa;
    char gradeLevel; // change to array to store multiple char
};

int main() {

    struct student classStudents[ST_SIZE];

    // assigning value
    strcpy(classStudents[0].name, "First student"); // to make assign/copy some data to the name variable
    classStudents[0].age = 28;
    classStudents[0].gpa = 4.00;
    classStudents[0].gradeLevel = 'G';

    // printing it
    printf("%s %d %lf %c\n", classStudents[0].name, classStudents[0].age, classStudents[0].gpa,
           classStudents[0].gradeLevel);

    return 0;
}

Read more about structures:

Struct declaration - cppreference.com

The GNU C Manual

Upvotes: 2

Related Questions