Thread: dynamic pointer array to struct

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    1

    dynamic pointer array to struct

    please help me find my mistakes...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    const int TEST = 3;
    
    typedef struct Person
    {
        char first[64];
        char last[64];
        int age;
    
    }Human;
    
    int main()
    {
        printf("Hello");
        struct Person **ptrArray;
        ptrArray = (struct Person **)malloc(TEST * sizeof(struct Person *));
        printf(" Pointer to array generated");
        for (int i = 0; i < TEST; i++)
        {
            //struct Person tmp;
            //ptrArray[i] = &tmp;
            ptrArray [i] = (struct Person *)malloc(TEST * sizeof(struct Person *));
            
            ptrArray[i]->age = i;
            printf(" before read in");
            //strcpy((*(ptrArray[i])).first, "FIRST");
            //strcpy((*(ptrArray[i])).last, "LAST");
            
            scanf(" Vornamen %s  Nachnamen %s", ptrArray[i]->first, ptrArray[i]->last);
            
            
            
        
    
        }
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You should put newlines after your debug strings.

    You don't need to cast the return value of malloc in C. If your compiler complains then you are actually compiling as C++.

    Instead of using the type for sizeof you can use the variable itself (this doesn't require the parentheses and, along with the last point, allows the type to be changed more easily).

    Instead of the meaningless "TEST", why not "SIZE"?
    Instead of the generic "ptrArray", why not "people"?

    You can make your typedef identifier the same as the struct tag. And if you make a typedef, then you should probably use it.

    Your use of scanf looks a little weird. Does your input actually contain the words "Vornamen" and "Nachnamen" or do you want those to be prompts? If they are prompts then you need to print them with printf.

    In the code below I've used scanf with %s as you did. I "hard-coded" the maximum string size, which will have to be changed if MAXNAME is changed (it should be one less). There are better ways to do this but they require a little more code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE     3
    #define MAXNAME 64
    
    typedef struct Person {
        char first[MAXNAME];
        char last[MAXNAME];
        int age;
    } Person;
     
    int main() {
        Person **people = malloc(SIZE * sizeof *people);
    
        for (int i = 0; i < SIZE; i++) {
            people[i] = malloc(sizeof **people);
    
            people[i]->age = i;
    
            printf("First name: ");
            scanf("%63s", people[i]->first);
    
            printf("Last name: ");
            scanf("%63s", people[i]->last);
        }
    
        for (int i = 0; i < SIZE; i++)
            printf("%s %s %d\n", people[i]->first, people[i]->last,
                                 people[i]->age);
    
        for (int i = 0; i < SIZE; i++)
            free(people[i]);
        free(people);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Struct Array: base operand of '->' has non-pointer
    By Gaspar Castillo in forum C Programming
    Replies: 8
    Last Post: 02-12-2015, 01:06 PM
  2. dynamic array struct
    By trep in forum C Programming
    Replies: 10
    Last Post: 05-23-2012, 10:20 AM
  3. Please help with struct and dynamic array problem
    By stephenb in forum C Programming
    Replies: 5
    Last Post: 10-19-2011, 06:02 AM
  4. dynamic array of struct within dynamic array of struct
    By explodecomputer in forum C Programming
    Replies: 3
    Last Post: 08-03-2010, 03:25 AM
  5. dynamic array in STRUCT
    By cfdprogrammer in forum C Programming
    Replies: 15
    Last Post: 08-04-2009, 09:57 AM

Tags for this Thread