0

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 a.x=b; ^ final1.c:31:2: error: request for member ‘y’ in something not a structure or union a.y=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.

3
  • 3
    1) the pointer is not allocated. 2) Why are you (wrongly) using the dot (.) notation in main, while correctly using the arrow -> notation in inipunt? Commented Jun 15, 2016 at 17:06
  • 1
    That would work for punt a; inipunt(&a); but double b,n; have not been initialised. Commented Jun 15, 2016 at 17:12
  • So I tried a=malloc(sizeof(punt)); and it worked, thanks. Now you are saying I should do the same to allocate the pointer to taulaRectangle and in the void function allocate the pointer to rectangle and initializing the other variables @EugeneSh. Commented Jun 15, 2016 at 17:25

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.