0

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......!!!

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Aia
0

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.

This topic 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.