7
struct counter{
    long long counter;
}

struct instruction{
    struct counter *counter
    int repetitions;
    void (*work_fn)(long long *);
};

int ncounter; //number of counters
struct counter *counter; //counter array

int nthreads; //number of threads
int *ninstructions; //number of instructions

struct instruction **instructions; 

How does this actually works ? I am having trouble with ** pointers

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
Jono
  • 223
  • 2
  • 3
  • 10

3 Answers3

12

A ** is just a pointer to a pointer. So where an instruction* contains the address of an instruction struct, an instruction** contains the address of an instruction* that contains the address of an instruction object.

To access the instruction pointed to by the pointer pointed to by an instruction**, you just use two asterisks instead of one, like (**p).repetitions or something similar.

You can visualize it like this:

instruction*  ----> instruction
instruction** ----> instruction* ----> instruction

Remember, however, that simply declaring struct instruction** instructions; doesn't actually create an instruction struct. It just creates a pointer that holds a garbage value. You'll have to initialize it:

struct instruction inst;
// set members of inst...
*instructions = &inst;

...

(*instructions)->repetitions++; // or whatever

However, it looks like you're using an instruction** to point to an array of instruction*s. To initialize the array, you need a for loop:

instructions = malloc(sizeof(struct instruction*) * num_of_arrays);
for (i = 0; i < num_of_arrays; ++i)
    instructions[i] = malloc(sizeof(struct instruction) * size_of_each_subarray);

And then you can access an element like instructions[i]->datamember.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • but say I use struct counter *counters and made an array. When i access the elements, counter[i]->counters ..... isn't that the same thing?!? but counters are * not ** – Jono Apr 28 '11 at 07:56
  • @Jono, If you say `struct counter* counters`, that makes a pointer to a `counter`. To access an element, you do `counters[i].something`. `counters[i]` gives you a `counter`. However, if you do `struct counter** counters`, you have to do `counters[i]->something`, notice the `->` instead of the `.`. In this case, `counters[i]` gives you a _pointer_ to a `counter`, not an actual `counter`. – Seth Carnegie Apr 28 '11 at 14:28
1

struct instruction **instructions; // How does this actually works ? I am having trouble with ** pointers

I'm not sure what the real issue is, but I'll try to answer the question.

Double pointer is a pointer to pointer. It can be sued as array of pointers for example (if you allocate memory accordingly). For example:

instructions = malloc(5*sizeof(struct instruction*));
for (int i = 0; i < 5; i++)
    instructions[i] = malloc(sizeof(struct instruction));

And you got yourself nice array of 5 pointers to struct instruction. Use it like this:

instructions[0]->repetitions = 0;
littleadv
  • 20,100
  • 2
  • 36
  • 50
0

instructions is a pointer to a pointer to struct instruction.

This means that *instructions will give you a pointer to a struct instruction. This kind of construct is often used to create a dynamic array of pointers to some compound type.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169