-
January 8th, 2014, 06:30 AM
#1
passing array to function
i am learning arrays i want to whats the following effect of cods while passing array to the following function
void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)
please help me .thank you in advance
-
January 8th, 2014, 06:52 AM
#2
Re: passing array to function
Please, post the declaration of your array and explain what your function void fun() is supposed to do.
Victor Nijegorodov
-
January 8th, 2014, 07:00 AM
#3
Re: passing array to function
Code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void func(int x[0])//my doubt is here
{
cout<<x[2]<<x[3];
}
int main()
{
int a[4]={1,2,3,5};
func(a);
return 0;
}
in void func i have declared size of array x has 0.but it displaying value of x[2],x[3].how is this
-
January 8th, 2014, 07:09 AM
#4
Re: passing array to function
Originally Posted by bhuvaneshnick
Code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void func(int x[0])//my doubt is here
{
cout<<x[2]<<x[3];
}
in void func i have declared size of array x has 0.but it displaying value of x[2],x[3].how is this
Well, your doubt is correct. BTW, does it compile?
the correct way to declare this function would beor Besides, if this function does not modify the array then declare it as
Code:
void func(const int* x)
Victor Nijegorodov
-
January 8th, 2014, 08:04 AM
#5
Re: passing array to function
thanks understood.And what is mean passing entire array element and passing element by element.answer with example are appreciated Thank you in advance
-
January 8th, 2014, 08:16 AM
#6
Re: passing array to function
Originally Posted by bhuvaneshnick
And what is mean passing entire array element and passing element by element.answer with example are appreciated
Sorry, I dont understand what it means.
Note that you go not pass the "entire array elements" but only the pointer to the first array element (with the index zero).
And the function should care to not access something outside the array bounds. To provide it you should also pass the array size as additional parameter and let your function check whether the index of accessed array elements is between zero and its upper bound.
Victor Nijegorodov
-
January 8th, 2014, 08:31 AM
#7
Re: passing array to function
ok sir.i tried to rate you.but it showing u have to spread reputation.how can i do that
-
January 8th, 2014, 08:44 AM
#8
Re: passing array to function
Originally Posted by bhuvaneshnick
ok sir.i tried to rate you.but it showing u have to spread reputation.how can i do that
It is because you already rated me today. You cannot rate the same person again unless you have not rated someone else before...
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|