0

the error is : request for member 'a' in something not a structure or union.

#include<stdio.h>

typedef struct mia {
    int a;
}hola;

typedef struct m {
    hola **r;
}bic;

int main() {
    bic y;
    scanf("%d", &(y.r->a));
    printf("%d", (y.r->a));
    return 0;
}
4
  • 1
    Before you address this question, you may want to ensure r is holding a valid pointer. As it is now, it has not one, but two levels of indeterminate indirection. Commented Feb 12, 2013 at 1:28
  • y->r->a or (*y.r)->a or (**y.r).a Commented Feb 12, 2013 at 1:28
  • @CongXu, y->r->a won't work. y isn't a pointer. Commented Feb 12, 2013 at 1:30
  • @gia, your other question that you deleted, you never set nr_query to a value but used it as a control to a loop. Commented Feb 13, 2013 at 20:27

2 Answers 2

2

You will need a more complex syntax for this should do it:

int main()
{
    hola x;
    hola *ptr = &x;
    bic y = { &ptr };

    scanf("%d", &((*y.r)->a));
    printf("%d\n", (*y.r)->a);
    printf("%d\n", ptr->a);
    printf("%d\n", x.a);

    return 0;
}

Input

100

Output

100
100
100

Note you can fetch out the hola * pointer:

hola *ptr = *y.r;
printf("%d", ptr->a); 
Sign up to request clarification or add additional context in comments.

1 Comment

and what can i do if i have bic y[10] ? how do i access 'a'? or if 'a' is a pointer how do i do ?
2

Before you can start accessing data off a pointer, you need to allocate some memory first; otherwise, it's undefined behavior. Your current structure needs two levels of allocation - one for the pointer to hola, and another for the hola itself. You also need to add another level of dereference, because -> works on pointers to structs, not on pointer to pointers to structs:

bic y;
y.r = malloc(sizeof(hola*));
*y.r = malloc(sizeof(hola));
// You need an extra level of dereference (i.e. an asterisk)
scanf("%d", &((*y.r)->a));
printf("%d", ((*y.r)->a));
// Don't forget to free the memory you allocated,
// in reverse order:
free(*y.r);
free(y.r);

Here is a demo on ideone.

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.