How can I find the error in this C program?

Viewed 89

I am learning data structures and pretty much new to C. I can't figure out what's causing the error in this function:

struct Student{
    int id;
    char name[30];
    double gpa;
    int ch;
};

void displayStudents() {
 struct Student students[10]={
    {1, ahmed ,3.6,43},
    {2, mohamed, 3.2, 40},
    {3,hassan, 3.0, 41},
    {4, hossam, 2.5, 35},
    {5, hany, 2.3, 32},
    {6, hala, 2.7, 37},
    {7, hana, 2.8, 33},
    {8, ramadan,3.8,46},
    {9, sameh,3.7,43},
    {10, nour,3.3,42}
 };

  for(int i=0; i<9; i++) {
    printf("%d,%s,%lf, %d", students[i].id, students[i].name, students[i].gpa, students[i].ch);
  }

}

It gives me those errors when compiling:

||=== Build: Debug in Data Structure Project (compiler: GNU GCC Compiler) ===|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c||In function 'displayStudents':|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|94|error: 'ahmed' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|94|note: each undeclared identifier is reported only once for each function it appears in|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|95|error: 'mohamed' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|96|error: 'hassan' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|97|error: 'hossam' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|98|error: 'hany' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|99|error: 'hala' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|100|error: 'hana' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|101|error: 'ramadan' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|102|error: 'sameh' undeclared (first use in this function)|
C:\Users\Shiko\Desktop\C Tutorials\Data structure project\main.c|103|error: 'nour' undeclared (first use in this function)|
||=== Build failed: 10 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I suspect the problem might be in the parameters of the function displayStudents(). I don't know what to type there, I searched far and wide and couldn't find anything.

3 Answers

You have to use "" for a string

void displayStudents() {
 struct Student students[10]={
    {1, "ahmed" ,3.6,43},
    {2, "mohamed", 3.2, 40},
    {3, "hassan", 3.0, 41},
    {4, "hossam", 2.5, 35},
    {5, "hany", 2.3, 32},
    {6, "hala", 2.7, 37},
    {7, "hana", 2.8, 33},
    {8, "ramadan",3.8,46},
    {9, "sameh",3.7,43},
    {10, "nour",3.3,42}
 };

  for(int i=0; i<9; i++) {
    printf("%d,%s,%lf, %d", students[i].id, students[i].name, students[i].gpa, students[i].ch);
  }

}

If you want to see your function, just call that function.

int main(void) {
  displayStudents();
  return 0;
}

Output:

1,ahmed,3.600000, 432,mohamed,3.200000, 403,hassan,3.000000, 414,hossam,2.500000, 355,hany,2.300000, 326,hala,2.700000, 377,hana,2.800000, 338,ramadan,3.800000, 469,sameh,3.700000, 43

There is the error about using strings in C. Your code would work if you delfine enumeration type declaring all the used personal names/surnames. You should also change the definition of structure. Otherwise, strings in C require more work, e.g. memory allocation.

EDIT: in your code - you define structure using the initialization block - it is enough to add quotes. However, this is not a general solution!

In most of the languages, chars and strings are initialised in quotes, single or double. In C, there is no concept of string, rather there is char array. when a char is declared, then the value inside it is initialised in single quotes, (when the value is a single character), but when char array or pointer to char is used, for the purpose of string, then , the value inside it is initialised using double quotes. Also, its a good habit to use typedef in struct, it makes the code more readable. you may change to

typedef struct Std{ //any random name
    int id;
    char name[30];
    double gpa;
    int ch;
    }Student;

void displayStudents() {

Student students[10]={ //no need to write struct everytime any more

{1, "ahmed" ,3.6,43},
{2, "mohamed", 3.2, 40},
{3,"hassan", 3.0, 41},
{4, "hossam", 2.5, 35},
{5, "hany", 2.3, 32},
{6, "hala", 2.7, 37},
{7, "hana", 2.8, 33},
{8, "ramadan",3.8,46},
{9, "sameh",3.7,43},
{10, "nour",3.3,42}
};
Related