Thread: Help! Structures, Pointers (Code included)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Help! Structures, Pointers (Code included)

    Help! Program doesn't crash but the data doesn't seem to save in the structure nor it can be displayed adequately (assuming the data is saved to the structure)

    Program for creating a student record of up to ten studentsjust a partial part; I actually suspect that there is something wrong with my pointers)
    note that I am trying to make an array of structures:

    Code:
    #include<stdio.h>
    #include<string.h>
            
    typedef struct str{
            char *name[50];//array of structure of variables
            char *studnum[11];
            int *age;
            float *GWA;
            }studRec;
            
    void AddStud(int n){
         studRec tch[10];
                    printf("Name:");
                    scanf("%s", tch[n].name);
                    printf("Student Number:");
                    scanf("%s", tch[n].studnum);
                    printf("Age:");
                    scanf("%d", &tch[n].age);
                    printf("GWA:");
                    scanf("%f", &tch[n].GWA);}

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You just have a bunch of pointers, not any actual data.
    Code:
    struct foo
    {
        char name[50]; /* 50 characters for a name */
        char studnum[11] /* 11 characters for num */
        int age; /* an int */
        ...etc...
    };
    Lose all those * you have there. Then do:
    Code:
    scanf( "%d", &mystructarray[ arrayspot ].age );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Thanks for the suggestion. Tried doing so, up to no avail.
    WHole code:

    Code:
    #include<stdio.h>
    #include<string.h>
            
    typedef struct str{
            char name[50];//array of structure of variables
            char studnum[11];
            int age;
            float GWA;
            }studRec;
            
    void AddStud(int n){
         studRec tch[10];
                    printf("Name:");
                    scanf("%s", tch[n].name);
                    printf("Student Number:");
                    scanf("%s", tch[n].studnum);
                    printf("Age:");
                    scanf("%d", &tch[n].age);
                    printf("GWA:");
                    scanf("%f", &tch[n].GWA);}                   
    
    void ViewStud(int n){
         studRec tch[10];//viewing
                    printf("Row Number %d\n", n+1);
                    printf("Name:");
                    printf("%s", tch[n].name);
                    printf("\nStudent Number:");
                    printf("%s", tch[n].studnum);
                    printf("\nAge:");
                    printf("%d", tch[n].age);
                    printf("\nGWA:");
                    printf("%f", tch[n].GWA);
                    printf("\n");
                    }
    main(){
           int n, x, viewNum, htr, htx, hty, htz, ........r;
           studRec tch[10];
           printf("---STUDENT RECORD---\n");
           for(;;){
           printf("[1] Add Student\n");
           printf("[2] Edit Student\n");
           printf("[3] View Student\n");
           printf("[4] Exit Record\n");
           printf("Enter:");
           scanf("%d", &x);
           switch(x){
           case 1: if(........r==10){
                   printf("Memory Full\n");break;}
                   else {htz=10-........r;
                     AddStud(htz); ........r++; break;}
           case 2: 
                printf("Enter which to edit:");
                scanf("%d", &hty);
                hty--;
                ViewStud(hty);
                AddStud(hty);
                printf("Updated Record:\n");
                ViewStud(hty); break;
           case 3:  for(;;){
                printf("Choose an option:\n");
                printf("[1] All\n");
                printf("[2] One of them\n");
                printf("[3] Back to main menu\n");
                scanf("%d", &viewNum);
                if(viewNum==1){
                               for(htr=0; htr<10; htr++){
                               ViewStud(htr);}
                               }
                if(viewNum==2){
                               printf("Enter the row number:");
                               scanf("%d", &htx);
                               ViewStud(htx-1);} 
                if(viewNum==3) break;}
           case 4: break;
           default: printf("Invalid; No action allocated so far\n"); break;
           }
           if(x==4) break;}
    }

    edit: those dots means 'count'. dunno the one with o removed is a profanity. didn't noticed that
    Last edited by raichanh; 02-26-2011 at 06:28 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void AddStud(int n){
         studRec tch[10];
    
    void ViewStud(int n){
         studRec tch[10];//viewing
    Those are two different arrays. They cease to exist when the function call ends.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Is that so? So..?
    It doesn't work when I remove the other arrays, or when I put it outside the functions, orr should I simply put it inside the main function?

    Sorry, I am clueless on this one.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A variable declared inside a function only exists while inside that function, and is destroyed when that function returns (unless it's static). You need to have the array in your main function, then pass it as an argument to the functions you want to use it in.
    Code:
    void dostuff( int array[] )
    {
        ...do stuff...
    }
    
    int main( void )
    {
        int array[4];
    
        dostuff( array );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    I see. So how can I declare the array of structures as part of argument?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just like you would with any other argument.
    Code:
    void somefunction( struct something somename[], int n )
    {
        somename[ n ].age = 5;
    }
    The reason this works is because arrays are passed as pointers. This is why that works, but this doesn't:[code]
    Code:
    void somefunction( struct something somename[], int n )
    {
        n = 5;
    }
    It's legal to assign n a value here, but you'll notice that outside the function, it doesn't keep. That's because the only way a function can update the value of something passed, is if the something that's passed is an address (pointer) which can be used to update the actual object being referenced.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    I wonder how this works:
    Code:
    void somefunction( struct something somename[], int n )
    {
        somename[ n ].age = 5;
    }
    Because I plan to state somename[ n ].age as entered by user in the program, with n separately determined by the program.

    But if you mean that 5 becomes the pointer in somename[ n ].age = 5; , then how is the real data entered?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    struct foo { int bar; };
    void fill( struct foo array[], int size )
    {
        int x;
        for( x = 0; x < size; x++ )
            array[ x ].bar = 5 * x;
    }
    void show( struct foo array[], int size )
    {
        int x;
        for( x = 0; x < size; x++ )
            printf( "%d\n", array[ x ].bar );
    }
    int main( void )
    {
        struct foo x[ 2 ];
        fill( x, 2 );
        show( x, 2 );
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    EDIT! I really hate to see that the main problem is that I am not returning the values created in the separate functions!!

    Ahh. Wait sorry I have not told you something:

    when I try editing, the data is saved. could it be related to switch statement?

    EDIT: BTW thanks for the explanations about pointers. learned something new
    Last edited by raichanh; 02-26-2011 at 08:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d array of structures and pointers.
    By CopperHead4750 in forum C Programming
    Replies: 2
    Last Post: 12-09-2009, 03:02 PM
  2. Arrays of pointers inside structures.
    By Posto2012 in forum C Programming
    Replies: 7
    Last Post: 11-18-2009, 12:58 AM
  3. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  4. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread