Simple question: How can i make a function return a string?
I have tried this:
Thanks in advance!Code:char "name of function"(Arguments) { char string1[16]; string1 = "Hello World"; return string1; }
Simple question: How can i make a function return a string?
I have tried this:
Thanks in advance!Code:char "name of function"(Arguments) { char string1[16]; string1 = "Hello World"; return string1; }
Yes, except the string will be destroyed when the function returns. Not to mention the return type 'char' won't work for arrays.
What you want is to return an address to a static variable in the function (note that means you'll only be able to return one string at any one time).
Or a better technique,Code:char * nameOfFunction(void) { static char str[] = "Hello world"; return str; }
Code:void nameOfFunction(char * buff, size_t n) { if(buff == NULL) return; memset(buff, 0, n); strncpy(buff, "Hello world", n - 1); return; } /* ... */ char str[256]; nameOfFunction(str, sizeof(str)); printf("%s\n", str);
Last edited by zacs7; 07-05-2008 at 06:47 AM.
What is a static variabel? and whats is void?
Last edited by MKirstensen; 07-05-2008 at 08:36 AM.
I think you need a book, tutorial, reference or something at this point.
There is something i dont get
When i call the function:
Why isnt there & in front of str, cause it seems logical to me that you passes the address into the pointer buff, and not the data inside str.Code:nameOfFunction(str, sizeof(str));
And why do you need to fill buff up with 0?
And finally why isnt there a * in front of buff in:
Thanks in advance!Code:memset(buff, 0, n); strncpy(buff, "Hello world", n - 1);
When an array is passed as an argument, it is converted to a pointer to its first element, thus the address of its first element is passed.Why isnt there & in front of str, cause it seems logical to me that you passes the address into the pointer buff, and not the data inside str.
To ensure that there is a null character at the end of the string.And why do you need to fill buff up with 0?
buff is the string, *buff is the first character of the string.And finally why isnt there a * in front of buff in:
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks!
No, buff is a pointer that (initially) contains the address of the first character of str.So inside buff we have the memory address of str...right?
By declaring buff as a parameter with a type of char*, you have told the compiler that buff is a pointer.And dont i have to tell the compiler that buff is a pointer and it shut write to the memory address inside? ... and why not?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks sorry for the edit...
When I take one glance at this:So you mean that when i use buff in memset function for example, the compiler already looks at it as a pointer to the mem block, but when u uses the * it just points to the first section in the array?
I can tell you that buff is a pointer to a char. Since buff is a pointer to a char, *buff is a char.Code:void nameOfFunction(char * buff, size_t n)
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)