arrays and pointers
i want to clear a doubt...
please check out the following codes..
int function (int p[])
{
//sm code
}
another style is :
int function (int *p)
{
// sm code
}
here i am passing an array to both the functions...and one receives it through p[] and another through *p....
then what is the difference between both styles ??
or just that the former uses subscript and the latter dereferences the pointer to access the values... ??
please clear out my doubt...
bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
>>then what is the difference between both styles ??
The first is most definitely an array. The second may or may may not be an array because it could also be a pointer to a single int object. function() must be made smart enough to know whether *p is a pointer to an object or an array. In both versions of the function it must also know how many elements are in the array.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>then what is the difference between both styles ??
The first is most definitely an array. The second may or may may not be an array because it could also be a pointer to a single int object. function() must be made smart enough to know whether *p is a pointer to an object or an array. In both versions of the function it must also know how many elements are in the array.
bro..
but i am passing an array to both the functions...
i just want to know the difference at that time... :|
bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2
>> just want to know the difference at that time...
There is no difference other than what I already mentioned. Its only two different ways to code arrays. The code inside the function is identical in both cases.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
bhoot_jb
Junior Poster in Training
89 posts since Mar 2008
Reputation Points: 57
Solved Threads: 2