C how to pass pointer to struct array pointer

thedeg123

I realize this is most likely a syntax issue, however, I have a struct:

struct pixel{
int pixel_num;
double lowest_dist;
int centroid;
double * location;
};

and an array of these structs struct pixel * pixel_data[10] and a method: void foo(double ** arr) and am trying to pass the address of the location pointer in an instance of the struct to update the pointer in the method foo.

I attempted to pass it like so foo(&(pixel_data[0]->location)) however this is clearly not correct as when I run through gdb the *arr values are different in foo than what they were in main() where I call foo.

Thanks.

An Example:

#define DIMENSIONS 5
//Struct is declared in header.
//max 10 pixels
struct pixel * pixel_data[10];

int main(){
    double newLoc[DIMENSIONS]={1.1,2.2,3.3,4.4, 5.5};
    pixel_data[0]=add_pixel(newLoc);
    update_centroid_location(&(pixel_data[0]->location), 0, DIMENSIONS);
}
void update_centroid_location(double **centroid, int centroid_num, int numpix){
    double * old_cent=malloc(DIMENSIONS*sizeof(double));
    if(!old_cent){
    perror("couldnt allocate memory");
    exit(4);
    }   
    for(int i=0;i<DIMENSIONS;i++){
    old_cent[i]=*centroid[i]; //segfaults here as cannot access memory address
    }   
}
struct pixel * add_pixel(double * location){
    struct pixel * new=malloc(sizeof(struct pixel));
    if(!new){
    perror("Could not allocate space for pixel");
    exit(4);
    }   
    new->lowest_dist=DBL_MAX;
    new->centroid=0;
    new->location=location;
    return new;
}
PeterT

In the line old_cent[i]=*centroid[i]; there is a misunderstanding about operator precedence.
The * operator has a lower precedence than the [].
So you are essentially doing old_cent[i]=*(centroid[i]);

Try to use parenthesis to explicitly express that you want to first dereference then add the offset for the second dereference like so:

old_cent[i]=(*centroid)[i];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass slice of struct as pointer to a function and modify it?

Pass pointer to struct by reference in C

C Declare array of struct/pointer to array of structs

pass pointer value into a struct in c

How to get struct array (or pointer) that is part of another struct? in C

struct with a pointer inside that is an array (C)

How to pass a Reference / Pointer to a Rust Struct to a C ffi interface?

How to pass a struct member pointer to a function?

How to pass a method pointer to a struct (in C/C++)?

passing array (not pointer) to a struct in c

pass pointer of struct in c programming

How to pass/access a double pointer to/from a struct?

Cannot pass struct pointer as function argument in C

How to return a pointer struct array from a function in C

pointer to anonymous struct array in c

How to pass a string array pointer to a new character array? in C

C: Struct pointer-array

Pass a pointer to an array of pointers to a C struct

How to initialize an int pointer inside a struct assingning it to an array in C?

Pass array of struct by pointer in C function to update pointer reference

How to return pointer to array of struct in C

how to pass parameter as a pointer to an array in c

C language pointer array in Struct

Filling array in struct pointer in C

How to pass const struct pointer to a setter method in C++

how to pass a pointer to struct to another function in C, in calling function if I have access to only pointer variable

Pointer to struct in C vs pointer to array

Pass array of struct to C from C# via struct pointer

How to pass a struct pointer between multiple functions