Array of Structures & Pointers

This is a discussion on Array of Structures & Pointers within the C Programming forums, part of the General Programming Boards category; Hi, I am a bit stumped by a small issue and I am sure that it is something simple I ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Array of Structures & Pointers

    Hi,

    I am a bit stumped by a small issue and I am sure that it is something simple I am overlooking and could use some direction. Below I have put my code where I define an array of structures and a pointer to it. What I am doing is taking the input and assigning a name to each Citizen in newCits. However when I am testing the code and I print out the names, they all come out the same. Any help is much appreciated!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct {
    char *name;
    } Citizen;
    
    
    void mainMenu(Citizen *c);
    void uploadMenu(Citizen *c);
    void citizenUpload(Citizen *c);
    void printCitizens(Citizen *c);
    
    
    void inputFlush();
    
    
    int main(){
    
    
    Citizen newCits[5];
    Citizen *p = newCits;
    mainMenu(p);
    
    
    return (0);
    
    
    }
    
    
    void mainMenu(Citizen *c){
    
    
    int input;
    
    
    printf("Welcome to the HGSM Main Menu! What would you like to do?\n(1) Access Upload Menu\nSelection: ");
    scanf("%d", &input);
    inputFlush();
    
    
        if (input == 1){
        uploadMenu(c);
        }
    
    
    }
    
    
    void uploadMenu(Citizen *c){
    
    
    int input;
    
    
    printf("Welcome to the HGSM Upload Menu. What would you like to do?\n(1) Upload Citizen Data\nSelection: ");
    scanf("%d", &input);
    inputFlush();
    
    
        if (input == 1){
        citizenUpload(c);
        }
    
    
    }
    
    
    void citizenUpload(Citizen *c){
    
    
    int input, i;
    char citInput[20];
    
    
    printf("Welcome to the Citizen Upload Menu. How many Citizens do you want to Upload?\nNumber of Citizens: ");
    scanf("%d", &input);
    inputFlush();
    
    
        for (i = 0; i < input; i++){
            printf("Enter Citizen Information in the format (Name, District #, Year of Birth, Sex):\nInput: ");
            scanf("%s", citInput);
            inputFlush();
            (c+i) -> name = citInput;
        }
    
    
        printCitizens(c);
    
    
    }
    
    
    void printCitizens(Citizen *c){
    
    
    int i;
    
    
        for (i = 0; i < 2; i++){
            printf("%s\n", (c+i) -> name);
        }
    
    
    }
    
    
    void inputFlush(){
    
    
    int ch;
    
    
        while ((ch = getchar()) != '\n' && ch != EOF);
    
    
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    You do not allocate memory for the name.You should do it with malloc.
    Also in the print function notice that you have i<2 and not i<3

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Anyway, I solved this problem on my own, just sharing information for anyone who might be interested. The problem was not the loop counter or a malloc for char name, the problem was with not directly accessing the array/pointer that I wanted to add the information.

    The way I had this set up was an array that held 5 structures of type Citizen that I had defined. The problem therein was I only passing a pointer to the Citizen structures I wanted to add information, when the function actually required a double pointer:

    Code:
    void citizenUpload(Citizen **c);
    Then in order to add the information to the individual fields of a structure you have to again use some pointer arithmetic to access the address of the data and overwrite it:

    Code:
     strcpy((*c+i) -> name, "I want this string in there!");
    Well hopefully this will help someone else who is having some trouble with pointers and structures, but I would highly recommend reading more into pointers, double pointers and general pointer arithmetic as it proved very helpful to me.

    Cheers.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    If i was a beginner and wanted to use your instructions i might write something like this
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char* strPointer;
    
        strcpy(strPointer,"Some boys thing they know everything...");
    
        printf("%s\n",strPointer);
    
        return 0;
    }
    output
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ pico px.c
    Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
    Macintosh-c8bcc88e5669-9:~ usi$ ./px
    Segmentation fault: 11
    I hope you understand why in your case it worked
    ......

    The loop counter was not a problem.You do not access all the elements. It is similar to have this
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int array[5] = {0,1,2,3,4};
        int i;
    
        for(i = 0 ; i < 4 ; i++)
           printf("%d\n",array[i]);
    
        return 0;
    }
    output
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ pico px.c
    Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
    Macintosh-c8bcc88e5669-9:~ usi$ ./px
    0
    1
    2
    3
    But ok maybe you want this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-04-2012, 01:19 PM
  2. Problem with array of pointers to structures and such
    By bungkai in forum C Programming
    Replies: 6
    Last Post: 07-09-2011, 04:31 PM
  3. Replies: 5
    Last Post: 05-26-2011, 06:44 AM
  4. Help with 2d array of structures and pointers.
    By CopperHead4750 in forum C Programming
    Replies: 2
    Last Post: 12-09-2009, 02:02 PM
  5. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 02:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21