1

I'm basically trying to create an array of struct pointers. Each of these pointers is supposed to point to another element of the same structure array i.e BLOCKS[2].

This is what I've done so far.

typedef struct bb {
      ..
      ..
      ..
    struct bb **successor;
} BLOCK;

BLOCK BLOCKS[10];

struct bb **A = malloc(sizeof(struct bb*)*5);        //create an array of pointers of type struct bb, 5 units i.e A[0]->A[4]. 

BLOCKS[0].successors = A                            //just assigning

Now......how do I assign the first element of the pointer array, A, to another structure?

I tried:

A[0] = &BLOCKS[6];

It compiles fine but I get a seg fault.

10
  • 2
    Have you tried running the program in a debugger? Commented May 21, 2012 at 8:31
  • 1
    Where does it seg fault? Commented May 21, 2012 at 8:31
  • It seg faults at "A[0] = &BLOCKS[6];". Commented May 21, 2012 at 8:40
  • 1
    Your code runs fine on my system. The error is probably somewhere else in your program. What other members does struct bb have? What other things do you do with it? Unless malloc fails to allocate memory, there is no reason why that assignment should fail. Commented May 21, 2012 at 8:41
  • Well it's definitely that line that causes the segfault. I have two other members of type unsigned int. But I might add that in my code that line is in a loop, does that matter?? Commented May 21, 2012 at 8:46

2 Answers 2

1

Have you tried this one:

typedef struct bb {
      ..
      ..
      ..
    struct bb *successor;
} BLOCK;

BLOCK BLOCKS[10];

struct bb *A = malloc(sizeof(struct bb)*5);        //create an array of pointers of
type struct bb, 5 units i.e A[0]->A[4]. 

BLOCKS[0].successors = A[0];

Because after looking at it quickly I think the ** should render into * and your malloc is reserving memory not for the size of 5 structures but the size of 5 pointers to this structure.

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

Comments

0

Quote from Question: "I'm basically trying to create an array of struct pointers."

The array of structure pointer should be

BLOCK *ptrBlockArr[10]; //This an array of size 10, which can store 10 pointers to the structure BLOCK

Now, since, these are pointers, you will have allocated memory for each of the elements. This should be done like

for(int i=0; i<10; i++)
{
   ptrBlockArr[i] = (BLOCK *)malloc(sizeof(BLOCK));
}

You question also included: "Each of these pointers is supposed to point to another element of the same structure array" . This can be done like

for(int i=0; i<9; i++) // Run the loop till the last but one element
{
  ptrBlockArr[i]->successor = ptrBlockArr[i+1];
}
//Assign the last's element's sucessor as the first element. This will make it circular. Check if this is your intention
ptrBlockArr[9]->successor = ptrBlockArr[0]

Please note that in your structure successor is struct bb**, whereas it should be struct bb*.

Also, you can optimize the code to combine the two loops shown by me above into one loop. I'll leave that to you to learn by yourself and implement.

1 Comment

sorry but this doesn't follow the structure of my code. i'm trying to create an array of struct pointers that is also a member of each structure.

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.