Guys, i have 2 doubts

Doubt-1
--------

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.

Doubt-2
--------
My doubt arose, cuz when i did

int* p;
int* q;
p=q;

(assume q was holding some address ), it worked

But when i did,

int* a[10];
a[0]=q;

(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 ?

Recommended Answers

All 5 Replies

int* p[10] is array of pointers

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

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.