Thread: typedef struct method

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    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. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    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
    Last edited by cable; 11-20-2010 at 01:22 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    but is not working

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    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. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can we just stop for a minute and talk about the use of CODE TAGS?!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Cool

    Cool link here how to use code tags ---> << !! Posting Code? Read this First !! >>

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    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
    Last edited by cable; 11-20-2010 at 01:22 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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 subscribe to a feed

Similar Threads

  1. 'typedef struct' and 'struct'
    By nawaf in forum C Programming
    Replies: 11
    Last Post: 10-24-2010, 04:57 AM
  2. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  3. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  4. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM