Hritikesh Semwal
Hritikesh Semwal

Reputation: 49

Understanding Array of pointers

I am doing something like this;

int main()
{
    int *b[2], j;
    for (j = 0; j < 2; j++)
    {
        b[j] = (int *)malloc(12 * sizeof(int));
    } 
    return 0;
}

Please tell me what this instruction really means? And how can I pass this array of pointers to a function to access values like *(B[0]+1),*(B[1]+1) etc?

Upvotes: 2

Views: 86

Answers (1)

Sinking
Sinking

Reputation: 95

int main(void)
{
    int *b[2], j; // initialization of an array of pointers to integers (size = 2)
    for (j = 0; j < 2; j++) // for each of the pointers 
    {
        b[j] = malloc(12 * sizeof (int)); // allocate some space = 12 times size of integer in bytes (usually 4)
    } 
    return 0;
}

If you want to pass this array to a function you can just pass b

foo(b);

Upvotes: 2

Related Questions