Can any onbe say me the exact difference between a pointer (*p) and the array p[]????

I mean to ask that when we pass an array to a function by reference, we can have both in the function definition, the * or we can manipulate the array too... But there must be some difference b/w these two......!!!

Recommended Answers

All 2 Replies

A pointer *p is not necessarily a pointer to an array. It could be a pointer to some object

void foo(int* p)
{
    *p = 0;
}

int main()
{
    int x;
    foo(&x);
}

or a pointer to an array of ints

void foo(int* p)
{
    *p = 0; // set first element of the array to 0
}

int main()
{
    int x[20];
    foo(x);
}

In the above two examples the function foo() must know whether it is getting a pointer to a single object or to an array. And the calling function must pass the correct type of pointer. If there is a mismatch then the program will most likely crash. And that is one of the most common types of errors made by C programmers.

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.