The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
sending an array too and from the main function...
Discuss sending an array too and from the main function... in the C Programming forum on Dev Shed. sending an array too and from the main function... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 26th, 2002, 05:00 AM
|
|
Junior Member
|
|
Join Date: Sep 2002
Location: new zealand
Posts: 0
Time spent in forums: 19 m 57 sec
Reputation 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 
|

September 26th, 2002, 06:13 AM
|
 |
Some day I will be a Lambda!
|
|
Join Date: Jun 2002
Location: NJ
Posts: 18
Time spent in forums: < 1 sec
Reputation 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
|

September 26th, 2002, 06:29 AM
|
|
Junior Member
|
|
Join Date: Sep 2002
Location: new zealand
Posts: 0
Time spent in forums: 19 m 57 sec
Reputation 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.
|

September 26th, 2002, 07:57 AM
|
 |
Contributing User
|
|
Join Date: Jul 2002
Location: London
Posts: 349
Time spent in forums: 4 h 19 m 21 sec
Reputation Power: 11
|
|
|
Pass your array by reference (&) to avoid unneccesary copy constructor calls..
|

September 26th, 2002, 12:33 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>>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().
|

September 27th, 2002, 05:56 AM
|
 |
Contributing User
|
|
Join Date: Jul 2002
Location: London
Posts: 349
Time spent in forums: 4 h 19 m 21 sec
Reputation Power: 11
|
|
|
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.
|

September 27th, 2002, 07:03 AM
|
 |
Some day I will be a Lambda!
|
|
Join Date: Jun 2002
Location: NJ
Posts: 18
Time spent in forums: < 1 sec
Reputation 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
|

September 27th, 2002, 12:25 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
>>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.
|

September 29th, 2002, 01:08 PM
|
 |
Contributing User
|
|
Join Date: Jul 2002
Location: London
Posts: 349
Time spent in forums: 4 h 19 m 21 sec
Reputation Power: 11
|
|
Yeah thats cleared it up nicely!
Thanks for the help 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|