Like Tree13Likes

passing array to function

This is a discussion on passing array to function within the C++ Programming forums, part of the General Programming Boards category; i am learning arrays i want to whats the following effect of cods while passing array to the following function ...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    18

    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The first and third can never work. The second sends a specific array element to a function, while the fourth sends a specific array element and a separate integer.
    bhuvaneshnick likes this.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    18
    thanks understood.what is mean passing entire array element and passing element by element.answer with example are appreciated Thank you in advance

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "passing entire array element" doesn't mean anything at all. Neither does "passing element by element". You can pass an array (the whole thing) by giving its name (without any punctuation), or you can pass a particular element (by specifying which element to pass using [brackets]). And of course, that has to match what the function expects.

    Code:
    int foo[10];
    void fun1(int bar[]); //expects an array
    void fun2(int bar); //expect an int
    
    fun1(foo); //works
    fun2(foo[7]); //works
    
    fun1(foo[]); //error: foo[] is meaningless
    fun2(foo[]); //error: foo[] is meaningless
    fun1(foo[7]); //error: fun1 expects array, not int
    fun2(foo); //error: fun2 expects int, not array
    bhuvaneshnick likes this.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    18
    fun1(foo[7]); //error: fun1 expects array, not int
    actuall fun1 expects array.you too passing array of specific element then it works right

    fun2(foo[7]); //works
    here fun 2 expects int but you passing array.how this works

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bhuvaneshnick View Post
    here fun 2 expects int but you passing array.how this works
    If I was passing an array, you would be right to be confused. However, I am not passing an array, I am passing one of the elements of the array which is just a single int and not even a little bit like passing the array.
    bhuvaneshnick likes this.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Quote Originally Posted by bhuvaneshnick View Post
    actuall fun1 expects array.you too passing array of specific element then it works right
    No, lets look at another example

    Code:
    int foo[10];
    foo[7] = 22;
    
    fun1(foo[7]); //this passes the value 22 into fun1 because thats what foo[7] equals. However fun1 wants an array so this is a failed call
    fun2(foo[7]); //Again we pass in the value 22 which is exactly what fun2 wants since it only wants an int value so this call succeeds.
    
    Hopefully that explains why both of your remarks are incorrect.
    Last edited by Ewiv; 01-08-2014 at 09:34 AM.
    bhuvaneshnick likes this.

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    18
    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

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    You're accessing memory still, that will display random numbers. Very undesirable behavior.
    bhuvaneshnick likes this.

  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    802
    Just pass std::array!
    Elysia and bhuvaneshnick like this.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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];
    
    
    }
    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
    In a function declaration (and only there), the size of an array* is irrelevant. You can always pass an array of any size to a function that declares it takes an array. In this context (only) x[0] means "any array", not "an array of size zero".

    *When you have a multi-dimensional array, only the first size is irrelevant; the rest must be specified (and specified correctly for what is passed in to it).
    bhuvaneshnick likes this.

  12. #12
    Registered User
    Join Date
    Dec 2013
    Posts
    18
    thanks Tabstop .You gave a clear view. what do you mean is "void func(int x[0]) or "void func(int x[3])" what ever the size of array it just consider to take an array.that is what u meaning am i right

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    This article covers various techniques when passing arrays to functions along with pitfalls: SourceForge.net: Safer arrays in Cpp - cpwiki
    bhuvaneshnick likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    I actually still disagree with that article quite heavily.

    Take for example this code.

    Code:
    #include <iostream>
    #include <assert.h>
    
    template <typename type, size_t N>
    size_t SizeOf ( type (&)[N] ) 
    // parameter not used, omitted name to inhibit this warning
    {
    	return N;
    }
    
    void foo(int arr[10], int& a, size_t size)
    {
    	assert(size == 10); // <--- Bad things happen if a programmer forgets this assert.
    	std::cout << "a is: " << a << std::endl;
    	arr[9] = 100;
    	std::cout << "a is: " << a << std::endl;
    }
    
    int main()
    {
    	int a = 10;
    	int arr[7];
    	foo(arr, a, SizeOf(arr));
    }
    The comment about the assert is wrong. Mainly, I think the author put the cart before the horse.

    If the intent of the function foo is to use an array of ten ints and only an array of ten ints then something like:
    Code:
    void foo( std::array<int, 10> );
    actually enforces what needs to be enforced. C arrays in C and C++, by design, decay into pointers so int[10] means int* and 10 is ignored.

    Bad things might happen if you forget the assert, but a better solution, I think, is to make foo work with any size array, or just abandon C interfacing. According to the article, it's "ugly" and in my opinion the solutions are kludges.

    Code:
     void foo(int *first, int *const last);
    Something like this is also neater.
    Last edited by whiteflags; 01-09-2014 at 06:48 PM.
    bhuvaneshnick likes this.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Well, the article's examples should probably be rewritten to loops doing stuff with the array. That makes more sense, but doesn't change the dangers of not knowing the array bounds.
    However, your range-like function has its downsides: it pushes the checking of valid ranges to the caller, which causes more bugs. Additionally, depending on how the function is written, you may prevent compiler optimizations because compilers have a harder time tracking pointers than array indices.
    bhuvaneshnick likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 12:32 PM
  3. Passing an array to a function
    By will of fortune in forum C Programming
    Replies: 6
    Last Post: 02-09-2010, 02:49 PM
  4. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  5. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21