dear all

what is difference when a function was called with an array as an argument and with a point as an argument? thanks. anyone can give me an example?

Recommended Answers

All 4 Replies

do you mean a pointer as an argument

well the syntax obviously changes but they are similar.
when an array is passed its actually a pointer to the address of the first element of the array of values
if you say a[1] its the same as *(a+1)
when you pass a pointer you can make it point to any address of something of the same type and if your using it to access the values of an array you have to make sure it doesnt go past the end of the array

void scores(int* num)
{
	for (int i= 0 ; i<5; i++)
	{
		cin >>*num;
		num++;
	}


}

once the for loop ends num is still pointing to 5 th element so you'd have to explicitly tell it to point to the first element of the array again

void scores(int* num)
{
	for (int i= 0 ; i<5; i++)
	{
		cin >>*num;
		num++;
	}
     num -= 5;

}

do you mean the difference between void foo(int *array) and void foo(int array[]); ??

The difference: nothing. They are identical, just two different ways of saying the same thing.

do you mean the difference between void foo(int *array) and void foo(int array[]); ??

The difference: nothing. They are identical, just two different ways of saying the same thing.

yes, you are right i mean that is a pointer, when i read the book, it said it is more better to pass a point rather than an array, it no need more time. so i don't understand well. thanks

I don't understand it either. All arrays are passed by refernce -- never ever by value. But passing a pointer is different than passing an array

int main()
{
   int x;
   int ay[10];

   foo(&x); // pass a pointer to some object
   foo(ay); // pass an array to the same function

}

The compiler will not complain about the above, but foo() will probably crash if it is expecting an array and main() passes a pointer to an int.

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.