简体   繁体   中英

Error when passing pointer to array of structs

#include <stdio.h>
#include <stdlib.h>

struct Point {
    double x;
};

void test(struct Point **a, int len)
{
    int i;

    printf("a = %p\n", a);
    for (i = 0; i < len; ++i)
        printf("%f\n", a[i]->x);
}

int main()
{
    int i;
    int len = 4;

    struct Point *P;

    P = malloc(len*sizeof(struct Point));

    for (i = 0; i < len; ++i) {
        P[i].x = i;
        printf("%f\n", P[i].x);
    }
    printf("&P = %p\n", &P);
    test(&P, len);

    return 0;
}

I am trying to pass an array of structs to a function (I want to pass a pointer to the array, not make a copy). When I try to use the array inside the function, I get an access violation. What is the correct way to do this? What am I doing wrong? a == &P , so it should work, right?

Why's you want a struct Point ** ? You can rewrite the same as

void test(struct Point *a, int len)
{
   //some stuff
    printf("%f\n", a[i].x);

}

and call it like

 test(P, len);

This way, IMHO, the requirement

I want to pass a pointer to the array

is also met # .


(#) NOTE: To be strict, here we pass the pointer to the first element of the array , however, the behaviour compares equal. Thanks to Mr. @alk for the comment.

Passing &p to function test means that you are passing a pointer to the first element of an array of one element of type struct Point * . Therefore, only a[0] is valid (and hence a[0]->x ) and all other a[i] are out of bound access. This will invoke undefined behavior.

Change a[i]->x to a[0][i].x or (*a)[i].x in test function.

Using pointer to pointer in this case is not worthy. This would be of worth using if passed pointer is to be modified in the function and that modification is expected to seen in the caller.

The array should be passed using the parameter struct Point *a . When you increment a the pointer will move by sizeof(struct Point) .

void test(const struct Point *a, int len)
{
    ...
}

Other answers offer you better alternatives.But I'll put this here to help anyone (myself) understand why it is wrong.

在此输入图像描述

I want to pass a pointer to the array,

Taking your requirement literally you do it like this:

void test(size_t len, struct Point (*a)[len])
{
  size_t i;

  printf("a = %p\n", (void *) a);

  for (i = 0; i < len; ++i)
    printf("%f\n", (*a)[i].x);
}

And call it like this:

size_t len = 4;
struct Point * p = malloc(len * sizeof *p);

for (i = 0; i < len; ++i) {
    p[i].x = i;
    printf("%f\n", p[i].x);
}

printf("p = %p\n", (void *) p);
printf("&p = %p\n", (void *) &p);

test(len, &p);

You could also implement the same functionality (looping over the array's elements) by going the way proposed by Sourav Ghosh 's answer . You then would pass a pointer to the array's 1 st element then, but a pointer to the array itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM