|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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:
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
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
Pass your array by reference (&) to avoid unneccesary copy constructor calls..
__________________
Online Designer Baby Clothes Store FiftyFifty Web Page, advertising space, with 50% profit share Free web development scripts! Tattoo Blog Free graphical wedding tickers |
|
#5
|
||||
|
||||
|
>>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(). |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
||||
|
||||
|
>>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. |
|
#9
|
||||
|
||||
|
Yeah thats cleared it up nicely!
Thanks for the help ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > sending an array too and from the main function... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|