Thread: typedef struct method

1. typedef struct method

typedef struct { int x[3] ; int y[3]; float m[3];} example;

example p[3];

i want to make one example program just for understanding the struct method about reading number x which is one int numer , reading 3 y numbers which are grades for each person and the m number which is the average (y[1]+y[2]+y[3])/3

how can i make itfor prinding that
for (i=0;i<max_num;i++)
printf(" the student %d got %f : average", p.x[i], p.m[i]);

2. Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{

int i,studnum;
typedef struct { int id[10] ; int vath[3]; float tel_vath;} students ;
students n;
scanf("%d", &studnum);
if(studnum<1 || studnum>10)
exit(-1);
for (i=0;i<studnum;i++)
{
printf("AFT");
scanf("%d",&n.id[i]);
printf("grade  1: \n");
scanf("%d",&n.vath[i+i+i]);
printf("grade  2: \n");
scanf("%d",&n.vath[i+i+i+1]);
printf("grade  3: \n");
scanf("%d",&n.vath[i+i+i+2]);
}

for (i=0;i<studnum;i++)
if (n.vath[i]<0 || n.vath[i]>100)
break;

for (i=0;i<studnum;i++)
{  n.tel_vath[i]=n.vath[i+i+i]+n.vath[i+i+i+1]+n.vath[i+i+i+2];
n.tel_vath[i]=(float) n.tel_vath[i]/3;
}

for (i=0;i<studnum;i++)
printf("%d    %f ", n.id[i],n.tel_vath[i]);

return(0);
}

this is what i did```

3. but is not working

4. Did you forget the #define for NUM_STUDENTS? This is not declared in your program, but looks like you meant it to be a defined value.

5. fixed but still getting this error
a.c:47: error: subscripted value is neither array nor pointer
a.c:48: error: subscripted value is neither array nor pointer
a.c:48: error: subscripted value is neither array nor pointer
a.c:53: error: subscripted value is neither array nor pointer

6. Can we just stop for a minute and talk about the use of CODE TAGS?!

7. Cool link here how to use code tags ---> http://cboard.cprogramming.com/cplus...ead-first.html

8. Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{

int i,studnum;
typedef struct { int id[10] ; int vath[3]; float tel_vath;} students ;
students n;
scanf("%d", &studnum);
if(studnum<1 || studnum>10)
exit(-1);
for (i=0;i<studnum;i++)
{
printf("AFT");
scanf("%d",&n.id[i]);
printf("grade  1: \n");
scanf("%d",&n.vath[i+i+i]);
printf("grade  2: \n");
scanf("%d",&n.vath[i+i+i+1]);
printf("grade  3: \n");
scanf("%d",&n.vath[i+i+i+2]);
}

for (i=0;i<studnum;i++)
if (n.vath[i]<0 || n.vath[i]>100)
break;

for (i=0;i<studnum;i++)
{  n.tel_vath[i]=n.vath[i+i+i]+n.vath[i+i+i+1]+n.vath[i+i+i+2];
n.tel_vath[i]=(float) n.tel_vath[i]/3;
}

for (i=0;i<studnum;i++)
printf("%d    %f ", n.id[i],n.tel_vath[i]);

return(0);
}

this is what i did```

9. error: subscripted value is neither array nor pointer
You're getting these errors because you're using array subscripts (the []'s) on something that's not an array. Look at those line numbers. You're indexing tel_vath[i], which is declared as a simple float, not an array of floats.

You also have some crazy errors related to array indexes, that will likely cause a segmentation fault:
Code:
```  for (i=0;i<studnum;i++)
if (n.vath[i]<0 || n.vath[i]>100)
break;```
Your loops go from 0 to studnum (which can be up to 10), but vath only has 3 elements, indexes 0, 1 and 2.

Code:
```  for (i=0;i<studnum;i++)
{  n.tel_vath[i]=n.vath[i+i+i]+n.vath[i+i+i+1]+n.vath[i+i+i+2];
n.tel_vath[i]=(float) n.tel_vath[i]/3;
}```
Here again, your loop goes from 0 to studnum. You then index vath[i+i+i], which, when studnum is 10, is like saying vath[30]. Also, you index tel_vath, which is not an array!

I don't think you're getting the struct concept very well.
Code:
```typedef struct {
int id[10];
int vath[3];
float tel_vath;
} students;```
You've made one conceptual object, called students, that has 10 ID's (presumably for up to 10 students), 3 "vaths" (grades?) and 1 tel_vath (average grade?). That means that between 10 students, you can only have a total of 3 scores recorded and one average. You then only declare a single instance of this, n, in your program.

Try creating a struct that represents a single student, with 1 id, 3 grades and 1 average, and call it student (singular). Then, declare an array of 10 of these structs like so:
Code:
```typedef struct {
// fill in struct members
} student;

student n[10];```
Make sure you index n and vath properly, being sure not to exceed the bounds of the arrays.

Popular pages Recent additions