Guys, i have 2 doubts


int* p[10];

Does it mean that
1)I have an array of 10 pointers, that means, each of them is capable of holding an address OR
2)I have a single pointer that points to an array holding 10 integers

I too, feel , the first is correct, after all, since an array is contiguous memory locations, it doesnt matter whether ur array is of 10-integers or 1000 integers. But in many places, i saw the opposite.
Ok, just in case, the answer happens to be 1 itself, then please tell me whether a concept like 2 exists and when do we need it.

My doubt arose, cuz when i did

int* p;
int* q;

(assume q was holding some address ), it worked

But when i did,

int* a[10];

(assume q was holding some address ), it DOESNT WORK
Why Why Why ???? a[0] is also a pointer right ? which means a[0] is also capable of holding the address of an int right ?

5 Years
Discussion Span
Last Post by ravenous
int* p[8]; // An array of pointers
int (*p)[8]; // A pointer to an array

Edited by sergent: n/a


int* p[10] is array of pointers

@sergent : exactly what i felt sir, but , why then does the problem stated in doubt2 happen

int* p[8]; // An array of pointers
int (*p)[8]; // A pointer to an array

oh thank you very much sir, that was very informative.
C's syntax .. urghhhh..
Anyways sir, if possible can u answer doubt2 as well.
Thanks once again

Edited by tubby123: n/a


In regard to your "doubt-2", I don't think what you found is correct. Consider this test program:

#include <stdio.h>

int main()
    int x = 2, y = 5;
    int* a[2];
    int* p;

    p = &x;
    a[0] = p;
    a[1] = &y;

    printf( "p    = %p -> %d\n", p, *p );
    printf( "a[0] = %p -> %d\n", a[0], *a[0] );
    printf( "a[1] = %p -> %d\n", a[1], *a[1] );

    return 0;

The result I get when I run this is:

p    = 0xbfdf0b78 -> 2
a[0] = 0xbfdf0b78 -> 2
a[1] = 0xbfdf0b74 -> 5

which is exactly what I'd expect. Do you find something different?

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.