: Hi
:
: I was studying the simpliest code to return the value to the main function from the other function.
:
: I want to type "ABCDE" and want to print it out in the
trim function and in the
main function as a return value from the
trim function.
:
:
:
: #include <stdio.h>
:
: void trim (char charArray[]){
: int* trimmed[5];
:
: //just to see if I could pass the string to this function from main
: printf("This is in the trim function, the data entered are:\n"
: "%s\n", charArray);
:
: //copy the content of charArray to trimmed just to see...
: *trimmed = *charArray;
:
: //if I can return trimmed, instead of charArray
: return *trimmed;
:
: }/*end trim*/
:
: int main(){
: char data[5];
:
: printf("Enter a line:");
: gets(data);
: trim(data);
:
: //then, I want to see if *trimmed will return the same
: printf("This is the main:%s\n", data);
:
: system("PAUSE");
:
: }/*end main*/
:
:
: In the
trim function, I did not do anything to the string passed from the main on purpose. I just want to see how I can return the string back to the main function by using another variable(?) in case if I process something on the original
charArray.
:
: But, actually, I think.. the following part is doing nothing...?
:
: *trimmed = *charArray;
:
: return *trimmed;
:
:
:
: Could anyone advise how to return *trimme (or trimme) value to the main!!
:
:
:
: (I once posted the similar question with more complicated coding..
:
:
: cheers!
:
Well, that was confusing... I'll try to give a general explanation about how to pass arrays and pointers.
First thing you need to know is that you can't pass a whole array as a parameter in C. This is good, because there is never a reason to do so, and passing a whole array would take up loads of stack memory. Instead you pass a pointer to the array. This means that no copy of the array is made, the function will work with the array that is allocated by the caller (main in this case).
Because it has a pointer to the array's address, the function may change the contents of that array. Therefore, any change done to the array by the function will remain when the function is finished.
This is different from passing parameters by value. A function like this would do nothing:
void func (char x)
{
x = 5;
}
int main()
{
char ch = 3;
func(ch);
}
Because x is a -copy- of the value in main, passed to the function.
If you passed it by reference however, it would change the value in main:
void func (char* x)
{
*x = 5;
}
int main()
{
char ch = 3;
func(&ch);
}
Arrays work in the same way, but when you use them, you don't have the option to pass them by value.
Returning pointers:
If you wish, you could re-write the function above as
char* func2 (char* x)
{
*x = 5;
return x;
}
But it is pretty pointless - all it does is to return a copy of the pointer.
What you -never- can do is to return a pointer to a local variable in the function. You will get a pointer to a memory area that has ceased to exist:
char* badFunc (char* x)
{
char ch;
ch = *x + 5;
return &ch; /* NOT ALLOWED! */
}
In almost every case, returning a pointer is a bad idea. I never do it in my programs, but there are probably people that won't agree with me on that, sence it is a matter of coding style.
(As a side-note for those people: no, dynamic allocation from inside the function is not a good example of when it should be done. All allocation should be handled by the caller if possible, to avoid memory leaks.)