1
typedef struct
{
    unsigned int a;
    unsigned char b[10];
    unsigned char c;
}acc1;

typedef struct
{
    unsigned char z[10];
    acc1 *x,y[10];
}acc2;

extern acc2 p[2];

I want to access struct acc1 variables from acc2 array p[2].

I'm getting segmenatation faults when I do it. Please guide on how to do this

3
  • 1
    "segmenatation fault problem is coming many times" -- Please show your code. Commented Jun 27, 2015 at 8:38
  • 1
    Note that x is a pointer to acc1 while y is an array of 10 acc1, not an array of pointers. Maybe it is what you want, maybe not. These cases are better written in separated lines: acc1 *x; acc1 y[10];. Commented Jun 27, 2015 at 8:52
  • strcpy(p[0].x->b,"1024"); getting segmantation fault on this what to do ?? Commented Jun 30, 2015 at 4:59

1 Answer 1

2

To access y's elements do:

char c = p[some index between 0 and 1].y[some index between 0 and 9].c

To access elements referred to by x do:

size_t i = some index between 0 and 1;
p[i].x = malloc(somenumber_of_elements * sizeof *p[i].x);
if (NULL == p[i].x)
{
  abort(); /* Failure to allocate memory. */
}
char c = p[i].x[some index less then somenumber_of_elements].c;

Referring kabhis comment

p[0].x->c is it not correct ?

Assuming the allocation above with somenumber_of_elements greater 0, then:

char c = p[i].x[0].c;

is equivalent to

char c = p[i].x->c;

and for somenumber_of_elements greater 1

char c = p[i].x[1].c;

is equivalent to

char c = (p[i].x + 1)->c;

and so on ...

Sign up to request clarification or add additional context in comments.

3 Comments

@kabhis: How did p[0].x got initialised? Also this might be worth another question.
typedef struct { unsigned char addr[20]; }access1; typedef struct { acess1 *st }access2; access2 arr[8] , *abb[10]; abb[i]=arr; then how can i access addr[20] printf("%s",abb[i]->st->addr);///// how to print these values.
I do not see how this is related to the question. You might want to pose another question for this.

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.