Hi all.
If I have a function named getwords(void), and I want the function to return an array of pointers to type char, how do I declare the return type in the function prototype?
(char *[])getwords(void)?
Thanks in advance.
Hi all.
If I have a function named getwords(void), and I want the function to return an array of pointers to type char, how do I declare the return type in the function prototype?
(char *[])getwords(void)?
Thanks in advance.
I would use a typedef, but note that you cannot actually have an array type as the return type, so it would be a pointer type instead. Actually, why not have the caller pass a pointer as an argument?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I would like to return an array of pointers back to main(). So what should the return type look like? When you say a pointer type, do you mean like below? Thanks.
Code:#include <stdio.h> char *test(void); int main(void) { int i; char *array[4]; array = test(); for(i = 0; i < 4; i++) { printf("%s", array[i]); } return 0; } char *test(void) { char *string[4] = {"How", "now", "brown", "cow"}; return string; }
That array is local to the test function, with automatic storage duration. Returning a pointer to the first element of that array will likely be a mistake still accessing that array using that pointer in main would result in undefined behaviour.
One way out could be to declare the variable as static:
Note that I also introduced const qualification as is proper to the string literals and the presumption that the static array should not be changed at all.Code:#include <stdio.h> const char * const *test(void); int main(void) { int i; const char * const *array; array = test(); for(i = 0; i < 4; i++) { printf("%s", array[i]); } return 0; } const char * const *test(void) { static const char * const string[4] = {"How", "now", "brown", "cow"}; return string; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I think here it is a good link const-correctness - Wikipedia, the free encyclopedia
I think here it is a good link const-correctness - Wikipedia, the free encyclopedia
I learned so much from this site years ago:
http://untroubled.org/articles/cdecls.txt
Building on laserlights example and the tutorial I have given above, here is another way of doing it:
"Declare test as a function returning a pointer to an array of constant char pointers"
const char *(*test())[];
Code:#include <stdio.h> const char *(*test())[]; int main(void) { int i; const char * const *array; array = test(); for(i = 0; i < 4; i++) { printf("%s", array[i]); } return 0; } const char *(*test())[] { static const char * const string[4] = {"How", "now", "brown", "cow"}; return string; }
Fact - Beethoven wrote his first symphony in C
anonymous function? (did they even exsist in C? )
Now, you are going to have to help me with that - I had a look around and saw a definition of "anonymous function" being one declared without a name (I'm sure that it is much more than that). Also, I confirmed that they definitely don't exist in C (But have recently been added to C++) - Anonymous function - Wikipedia, the free encyclopediaanonymous function? (did they even exsist in C? )
@std10093: Would you be able to help me with what you meant by that post?
I should correct my code from my last post - You will need to add 'void' to the inputs of the function.
i.e.
Code:const char *(*test(void))[];
Fact - Beethoven wrote his first symphony in C
Excuse me,but the word anonymous is greek so the meaning comes to my immediately , that's why i did not give a definition of the world
Anonymous function means a function with no name.I was taken away by web development,that's why it came to my mind
And at last ,it is not an anonymous function
You also need to remove the brackets at the end. They're not needed (and it causes the test function to return a different pointer type than the value it's returning and different type than the array variable).
I couldn't agree more - I think that it's important to have your declaration match what you meant. In the other post, it would have been better to use the same as the declaration as the variable (looking back at it, I think that it was a fluke that it worked)
On my version of codeblocks, removing the square brackets will also work in this (quite different, but more appropriate) example, but I think that it is more descriptive of what is going on to leave the square brackets in -
Code:#include <stdio.h> typedef enum { brown_cow=0, black_cow, white_cow } cow_colour; /****************************** * declare "test" as a function * with input "cow_colour" * returning a pointer to * an array with 4 elements of * constant char pointers ******************************/ const char * (* test(cow_colour))[4]; int main(void) { int i; const char * (* array)[4] = test(brown_cow); //const char * (* array)[4] = test(black_cow); //const char * (* array)[4] = test(white_cow); for(i = 0; i < 4; i++) { printf("%s ", (*array)[i]); } return 0; } const char * (* test(cow_colour cow))[4] { static const char *brown_str[4] = {"How", "now", "brown", "cow"}; static const char *black_str[4] = {"How", "now", "black", "cow"}; static const char *white_str[4] = {"How", "now", "white", "cow"}; static const char *unknown_str[4] = {"How", "now", "unknown coloured", "cow"}; switch (cow) { case brown_cow: return &brown_str; case black_cow: return &black_str; case white_cow: return &white_str; } return &unknown_str; }
If anyone has trouble reading the declaration, they should look at the tutorial that I put up before - http://untroubled.org/articles/cdecls.txt
Fact - Beethoven wrote his first symphony in C