0

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.

2
  • 5
    student struct --> struct student Commented Feb 5, 2020 at 21:19
  • Note: the error is not occurring when you run the program. It is happening when you compile it. It is important to understand what happens in each phase of programming: pre-process, compile, link, run. Commented Feb 5, 2020 at 21:33

1 Answer 1

2

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

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

3 Comments

See Updated Example -- feel free to incorporate any part you wish in your answer.
@DavidC.Rankin thank you for pointing out initializing error, I was using -std=c++17
I could tell... :)

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.