2

Possible Duplicate:
How to pass an array of struct using pointer in c/c++?

I have an array of structures and need to pass a pointer to the array to a function.

I get these compile warnings and it crashes at runtime:

test.c:35:5: warning: passing argument 1 of ‘displayArray’ from incompatible pointer type
test.c:20:6: note: expected ‘struct tagdata **’ but argument is of type ‘struct tagdata (*)[10]’

typedef struct tagdata{
    char lastName[20];
    char firstName[20];
    int age;
} PERSON[10], *PPERSON[];

int genNumber();
void displayArray(PPERSON pPerson);

int main(){
    PERSON personDetails;
    int i;
    char buf[20];

    for (i = 0; i < 10; i++){
        sprintf(buf, "Charlie%d", i);
        strcpy(personDetails[i].firstName, buf);
        sprintf(buf, "Brown - %d", i);
        strcpy(personDetails[i].lastName, buf);
        personDetails[i].age = genNumber();
    }

    displayArray(&personDetails);

    exit(0);
}
void displayArray(PPERSON pPerson){
    int i = 0;

    while (i < 10){
        printf("Last Name   First Name  Age\n");
        printf("%s          %s          %d\n",
               pPerson[i]->lastName,
               pPerson[i]->lastName,
               pPerson[i]->age);
        i++;
    }
}
int genNumber(){
    int n;
    n=random();

    return(n);
}
Community
  • 1
  • 1
  • what message do you get at the crash? – elyashiv Oct 09 '12 at 14:01
  • ./test Last Name First Name Age Segmentation fault (core dumped) – user1731859 Oct 09 '12 at 14:05
  • You're over complicating this. You're just passing an array to a function. It doesn't matter what's in the array. Check out [any example](http://stackoverflow.com/questions/2360794/how-to-pass-an-array-of-struct-using-pointer-in-c-c) of passing an array to a function. – Mike Oct 09 '12 at 14:13
  • 2
    It is not nice to post an error message about line 35 when actually it is line 23 (±2) in the code snippet you post, unless you identify line 35. You should lose the `&` in `displayArray(&personDetails);` — the address of an array is a different type from the pointer to the first element of the array (even if the numerical value of the address is the same). And that's what the compiler is telling you. Oftentimes, typedefs of arrays and pointers make things more confusing. Having a typedef of, say, `Person` for the structure makes sense. Use that as much as you like. But not `PERSON[10]` etc. – Jonathan Leffler Oct 09 '12 at 14:13
  • 1
    @JonathanLeffler At least part of that should be copied into a new answer for this question. – tomfanning Oct 09 '12 at 14:41

5 Answers5

7

I have a structure that is also array

Although there is no such thing as a "structure that is also array", you can make an array of structs. You should untangle the typedef and the declaration of a struct variable first, like this:

typedef struct {
    char lastName[20];
    char firstName[20];
    int age;
} PERSON; 

PERSON person[10];

Now you can use PERSON as a name of a type, declare variables of type PERSON, make pointers to them, pass arrays, and so on, as if it were a value of a built-in type, such as an int:

void displayArray(PERSON pPerson[], int count) {
    int i = 0;

    printf("Last Name   First Name  Age\n");
    while (i < count){
        printf("%s          %s          %d\n",
               pPerson[i].lastName,
               pPerson[i].lastName,
               pPerson[i].age);
        i++;
    }
}
Wug
  • 12,956
  • 4
  • 34
  • 54
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    +1 For splitting the `typedef`. I was going to suggest the same as I find it (the posted code) very difficult to read. – hmjd Oct 09 '12 at 14:09
0

You should pass a void pointer and cast it to PERSON[10].

As you see yourself, you are not passing the pointer to the PERSON stuct but rather you are passing pointer to an array which itself contains the struct. They are not equal.

To make it work properly, pass a void pointer and an integer stating the number of elements in the array. When you receive it, it will help you to avoid reading array beyond its allocated memory.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • No, you should definitely not use a void pointer. That will just lead to problems since you lose any type safety. – interjay Oct 09 '12 at 14:06
0

Roughly written: ** whatever = * whatever[10]

So you probably just want *PPerson

Friedrich
  • 5,916
  • 25
  • 45
0

An array can be passed to functions as a pointer, so instead of attempting to pass as a pointer to an array (by the way, now PPERSON is defined as an array of pointer, which is not what you have) just pass the array as-is, and make PPERSON a normal pointer (i.e. typedef struct tagdata *PPERSON;).

Then you can use the "pointer" as a normal array in the function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

*PPERSON[]; Is this you really want to user or you want something like this: typedef PERSON *PPERSON;

s.s
  • 138
  • 8