1

My aim is do declare an array of pointer to my person struct and then use it to modify the name and the age of the persons.I am pretty lost in my code so any help would be appreciated.Thanks!!!

#include <stdio.h>

#define HOW_MANY 7

char *names[HOW_MANY]= {"p1", "p2", "p3", "p4", "p5", "p6", "p7"};

int ages[HOW_MANY]= {2, 1, 16, 8, 10, 3, 4};

struct person
{
    char person_name[30];
    int person_age;
}*array[10];

static void insert(struct person array[], char *name, int age) 
{
    static int nextfreeplace = 0;
    array[nextfreeplace] = (struct person*) malloc(sizeof(struct person));
    &array[nextfreeplace]->person_name = name;
    &array[nextfreeplace]->person_age = age;
    nextfreeplace++;  
}

int main(int argc, char **argv) 
{ 
    struct person *array[10];
    int index;
    int personNumber = 1;

    for (index = 0; index < HOW_MANY; index++) 
    {
        insert (array[index], names[index], ages[index]);
    }


    for (index = 0; index < HOW_MANY; index++)
    {
        printf("\nPerson number %d has name %s and age %d \n", personNumber,
             array[index]->person_name, array[index]->person_age);
        personNumber++;
    }

    return 0;
}

The errors I get:

arrays.c: In function ‘insert’:
arrays.c:20: warning: implicit declaration of function ‘malloc’
arrays.c:20: warning: incompatible implicit declaration of built-in 
function   ‘malloc’
arrays.c:20: error: incompatible types when assigning to type 
‘struct   person’ from type ‘struct person *’
arrays.c:21: error: invalid type argument of ‘->’ (have ‘struct person’)
arrays.c:22: error: invalid type argument of ‘->’ (have ‘struct person’)
make: *** [arrays] Error 1
4
  • 1
    Does it compile? Could you be more specific about your problems? Commented Oct 25, 2015 at 23:49
  • oo I forgot to to put the error msgs... Commented Oct 25, 2015 at 23:52
  • do not cast the returned value from malloc(), copying a string is not = but rather strcpy( destPtr, srcPtr); to use ANY system function, the appropriate header file must be part of the source via: `#include <stdio.h> #include <stdlib.h> #include <string.h> etc. read the man page for each system function used to see what header file is required (and perhaps what #define is needed before the #include of the header file) Commented Oct 26, 2015 at 0:14
  • in the insert() function, how is main() to know if the operation was successful or not? code needs to check (!=NULL) the returned value from malloc() to assure the operation was successful. Suggest returning some indicator/status from insert() that main() can check to assure the insert operation was successful. main() needs to pass all the malloc'd memory pointers to free() to avoid a memory leak Commented Oct 26, 2015 at 0:38

1 Answer 1

1

You have successfully declared an array of pointers to your struct here

  struct person *array[10];

You then need to allocate memory such that each pointer in this array actually points to an instance of your struct

Currently you pass

array[index]

to insert as first argument but array[index] is of type person* but your insert function expects person [].

You try to access array[nextfreeplace] within your insert function, but as you are passing a person* to insert this will attempt to read a memory address that is potentially not valid because

array[nextfreeplace] = (struct person*) malloc(sizeof(struct person));

is identical to

*(array + nextfreeplace) = (struct person*) malloc(sizeof(struct person));

and so whenever nextfreeplace is nonzero you are effectively attempting to allocate dynamic memory and store a pointer to it at an address that you should not be.

This leads to potential undefined behaviour. You cannot treat the pointers inside array as if there are arrays themselves (unless you allocate sufficient memory and treat them as such - which is not the case in your code). You certainly store the return value of malloc to an address at some offset of this pointer. As your code stands each array[index] is not an array that you can freely read from and write to and should not be treated as such without expecting problems.

You might consider changing insert to

void insert(struct person** p, char *name, int age) 
{
    *p = malloc(sizeof(struct person));
} 

and then changing your loop in main to

for (index = 0; index < HOW_MANY; index++) 
{
    insert (&array[index], names[index], ages[index]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for ur response.I changed the things in ur way and still get this: arrays.c:21: error: lvalue required as left operand of assignment arrays.c:22: error: lvalue required as left operand of assignment
If you still have a problem, perhaps you should not accept my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.