CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2013
    Posts
    19

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,397

    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

  3. #3
    Join Date
    Dec 2013
    Posts
    19

    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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,397

    Re: passing array to function

    Quote Originally Posted by bhuvaneshnick View Post
    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 be
    Code:
    void func(int x[])
    or
    Code:
    void func(int* x)
    Besides, if this function does not modify the array then declare it as
    Code:
    void func(const int* x)
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2013
    Posts
    19

    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

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,397

    Re: passing array to function

    Quote Originally Posted by bhuvaneshnick View Post
    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

  7. #7
    Join Date
    Dec 2013
    Posts
    19

    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

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,397

    Re: passing array to function

    Quote Originally Posted by bhuvaneshnick View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured