Dear All,
I am looking for some notes on function return values (array, pointer and char array).
help me !
Thanks.....
Dear All,
I am looking for some notes on function return values (array, pointer and char array).
help me !
Thanks.....
Use the return keyword. Ex.
Code:int func() { return 666; }
Thats ok, its returning an integer, But I need some notes on function return vales for arrays , pointers and char arrays.
Thanks............
What kind of notes are you looking for? It's the same thing.
Code:char* func() { char* buf = malloc( 80 ); return buf; } int* func() { static int array[20]; return array; }
Thats nice, Can u provide a example program on function returns pointer and function returns array.
Thanks....
The whole forum is littered with all sorts of program examples.
Or post some of your own code, and ask how to turn some of it into a 'value returning function'. Seeing how it's done in a real example which means something to you will help you figure it out.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The reason returning a pointer is different from returning some other type of data is that pointers will not extend the lifetime of the thing that they point to. So if you attempt to return a pointer to a local variable, the data that you need will likely be destroyed and you can't use it.
So it all comes down to the scope of the pointer's pointed-to data.
Does below code make any sense.....,
I am trying to return an array from a function and print it.
Code:#include <stdio.h> char* func() { char array[]=ashok; return array; } int main() { char a[10]; a=func(); printf("array a=%s\n",a); return 0; }
help me to do so...
Thanks...........
Not really.
Let's break it down to pieces. When the compiler gets to char a[10]; it allocates a spot in memory large enough to hold 10 characters, and sets the variable "a" to point to that spot in memory.
The function call then happens. The function allocates its own temporary array and notes its address. The address is returned, but when the function ends, the temporary array is destroyed. (Of course, your physical memory isn't literally destroyed; but the memory is no longer yours.) So we have this situation (ASCII art FTW):
So you have the original memory that was allocated, but you can't get there anymore. a is now a pointer to memory that has been freed, which is illegal to use; so when you attempt to use it in the next line, you will be arrested.[1]Code:+-+-+-+-+-+-+-+-+-+-+ a | | | | | | | | | | | (original memory) \ +-+-+-+-+-+-+-+-+-+-+ \ ---> undefined land
The customary idiom here is to pass the pointer into the function; the function then writes into the memory location pointed to by the pointer. (Be sure to pass in the size of the memory too!) If you feel that you must return a pointer, you have to malloc() it in the function; memory that you explicitly create will last until you explicitly destroy it with free(), unlike the stuff that is automatically created for you (such as arrays), which are automatically destroyed for you when they go out of scope.
(Also: string literals must go in "quotes".)
[1] This is not really true; it is undefined, however, so your system is free to do whatever it pleases. And what pleases your system may not please you.
From what i can see from your previous post, i think you are after something which is similar to the following code. Have a look, i the func function takes char array and copies some value and exits the function with out returning a value.
But the function foo return a char *, cos the memory was allocated in the function and the address of the memory location which was allocation is returned to the main function. And also make a note of free function. Since i explicitly allocated memory using malloc, it the programmer job to free it.
ssharishCode:#include <stdio.h> #include <string.h> #include <stdlib.h> void func(char *str) { strcpy(str, "Ashok"); } char *foo(void) { char *str; str = malloc( sizeof(char) * 10 ); if(str != NULL) strcpy(str, "Hello"); else return NULL; return str; } int main() { char a[10]; char *p = foo(); func(a); if( p != NULL ) printf("array a = %s %s\n", p, a ); else printf("array a = %s\n", a); free(p); getchar(); return 0; } /* my output array a = Hello Ashok */
Thats great ... Thank you very much
It helped me alot to understand all these.
Thanks ssharish.