#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    new zealand
    Posts
    0
    Rep Power
    0

    sending an array too and from the main function...


    Hi guys... I have started building a library of small reminder programs for C...

    I have managed to successfully send an array from the main funtion to a pass function...

    Even managed to send an array from the pass function to the main...

    When I try and send one from the main to pass and a new array back to the main then I get one error message, no warnings... saying too few arguments...

    PHP Code:
    #include <stdio.h>
    int pass(int arrayA[]);
    main()
    {
        
    int arrayA[1];
        
    arrayA[0] = 1;

        
    pass(arrayA);
        
    printf("element 0 from function two equals %d \n"pass());
    }
    pass(int arrayA[])
    {
        
    int arrayB[1];
           
    arrayB[0] = 1;

        
    printf("array number 0 from main is %d \n",arrayA[0]);       
       return (
    arrayB[0]);


    This happens in the printf statement in the main function when I call the pass function...

    I have not yet approached pointers... thats next on the list...lol

    Any help is appreciated... thanks:)
  2. #2
  3. Some day I will be a Lambda!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    NJ
    Posts
    18
    Rep Power
    0
    Here is what is happening. You are returning an integer from your pass function just as you should but in your main I think you are trying to treat the pass() as a variable. You need to either declare a variable and then assign it the value which is returned by the pass function or do as I did below and let the pass function return the value directly to the printf.

    Code:
    #include <stdio.h>
    int pass(int arrayA[]);
    main()
    {
        int arrayA[1];
        arrayA[0] = 1;
    
        printf("element 0 from function two equals %d \n", pass(arrayA));
    }
    pass(int arrayA[])
    {
        int arrayB[1];
           arrayB[0] = 1;
    
        printf("array number 0 from main is %d \n",arrayA[0]);       
       return (arrayB[0]);
    }
    Hope this helps.
    Eric
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    new zealand
    Posts
    0
    Rep Power
    0

    thanks a lot...:)


    Thanks a lot... I tried it and it works ok funny cause I'm sure that I tried that approach before coming online...lol

    I thought the result gave an answer where the printf statement in the main fuction returned a result from the main array...

    It works though so cool... will have to try and play some more...:)

    Thanks again... have a good one.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    London
    Posts
    349
    Rep Power
    13
    Pass your array by reference (&) to avoid unneccesary copy constructor calls..
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    >>Pass your array by reference (&) to avoid unneccesary copy constructor calls..
    Arrays are already passed by reference in C automatically. For example, pass(arrayA) is a synonym for pass(&arrayA[0]);, so there's no question of a copy constructor being called. To illustrate this further, I made a few modifications to Beans4You's code:
    Code:
    #include <stdio.h>
    int pass(int array[]);
    main()
    {
      int arrayA[1];
      arrayA[0] = 1;
    
      printf("Before calling pass() array number 0 from main is %d \n",arrayA[0]);
      pass(arrayA);
      printf("After calling pass() array number 0 from main is %d \n",arrayA[0]);
    }
    pass(int array[])
    {
      array[0] = 2;
      return (1);
    }
    As you can see, the change to array[0] in pass() will affect arrayA[0] in main().
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    London
    Posts
    349
    Rep Power
    13
    Would that be the same in C++ as well then?
    I must admit, I didn't read the bit about a C program, I just assumed it was C++...

    In C++ passing parameters by value will result in the complete parameter being copied on to the stack.

    This is acceptable for regular types like integer, pointer etc. as these types are generally restricted to four bytes so there is no overhaed.
    When passing bigger types, the cost of copying the object on the stack can be prohibitive.

    In case of passing classes there will be an additional overhead of invoking the constructor for the temporary copy that is created on the stack. When the function exits the destructor will also be invoked, but thats by the by.
  12. #7
  13. Some day I will be a Lambda!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    NJ
    Posts
    18
    Rep Power
    0
    I'm sorry, I too did not see that he was talking about C. I have no experience with C, only VC++ 6.0, C++, which was on a DEC Alpha, and GC++.

    Would you need to pass LPCTSTR's by value? Sometimes I get a memory access violation during run-time. This can be solved by lowering the size of my LPCTSTR array's.

    Eric
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    >>Would that be the same in C++ as well then?
    Yes it is. Note that this is the case for arrays of any type in both C and C++.

    >>Would you need to pass LPCTSTR's by value?
    By definition, LP = Long Pointer, so you're passing a pointer, NOT a copy of the array. Anyways, like I said above, when you're passing arrays, C (and C++) automatically pass by reference (i.e. by passing the pointer to the array instead of a copy), so there's no question of decreasing the size of your array or whatever. Hope this clears things up a bit.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    London
    Posts
    349
    Rep Power
    13
    Yeah thats cleared it up nicely!
    Thanks for the help :D

IMN logo majestic logo threadwatch logo seochat tools logo