简体   繁体   中英

Initializing a pointer to a structure through a void function in C

Let my structures be:

typedef struct{
    double x;
    double y;
   } punt;

typedef struct{
    char tip;
    punt infEsq
    punt supDret;
   } rectangle;

typedef struct{
    rectangle *llista;
    int numRect;
    int midaMax;
   } taulaRectangle;

Contextualizing,

"Punt" represents a point.

"Rectangle", char indicates if it's a square [q] or a rectangle [r], the two points are the inferior left point (infEsq) and the superior right point (supDret) of the rectangle.

"TaulaRectangle" maps a list (or table) of structures type "rectangle", indicating how many rectangles it has [numRect], and how many it can hold [midaMax] (which if I am not wrong indicates the dimension of [llista]).

Now I want to code a function to initialize a pointer to "taulaRectangle" like this:

void initaula(taulaRectangle *t,int n) /*n is associated to midaMax*/

I will not post my intents in coding a valid initialization function because I believe they're not close to what I want to achieve although here is a similar scenario where I did try something alike:

    void inipunt(punt *b){
    b->x=0;
    b->y=1;
    return;
    }
    int main (void){
    double b,n;
    punt *a;
    inipunt(a);
    puts("guay");
    a.x=b;
    a.y=n;
    printf("a.x=%lf a.y=%lf",b,n); /*I know this association is not necessary
                                     but if I directly print a.x a.y it does
                                     not work either, also a fail is *(a.x),*
                                     (a.y) */
    return 0;
    }

The compiler (gcc -ansi -Wall) returns:

final1.c:30:2: error: request for member 'x' in something not a structure or union ax=b; ^ final1.c:31:2: error: request for member 'y' in something not a structure or union ay=n; ^

Synthesizing, I want to be able to declare a pointer to "taulaRectangle" and initialize it using "void initaula()", I typed the example above because I believe what I want to achieve in both functions is similar except in "void inipunt()" I do not have to allocate memory for a pointer in the structure, I think the problem has it's roots in how i treat the pointer to the structure in my void function.

Sorry for the long question, if anyone can help It will be gladly appreciated.

As Eugene pointed out in the comments, the pointer is not allocated.

When you do punt *a; , you're only allocating a pointer to the structure, but it doesn't point to anything yet. It's not initialized.
Keep your uninitialized variables in check, as they could have any value. You have no idea what an uninitialized pointer could be pointing to.

You have two solutions for this problem:

  • Allocate space for the structure in the stack, by doing punt a; . In this case a is the struct.
  • Allocate space for the structure in the heap, by doing punt *a = malloc(sizeof(punt)) . In this case a is a pointer to the struct.

If you allocate the space in the stack, to get the pointer for a, you should do &a .
Keep in mind that to access a struct behind a pointer, you use -> instead of . .

This should work:

void inipunt(punt *p) {
    p->x = 0;
    p->y = 1;
}
int main() {
    punt a;
    inipunt(&a);
    printf("%lf %lf\n", a.x, a.y);
    punt *b = malloc(sizeof(punt));  // You should check if it returns NULL, in case of an error.
    inipunt(b);
    printf("%lf %lf\n", b->x, b->y);
    return 0;
}

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