I have a function that needs to return an array, as this is not possible it instead returns the pointer to that array. I then need to print this array in the main function. How do I do this?
Here is an example of what I mean:
char* myFunction (void)
{
char myArray[5] = {'one','two','three','four','five'};
return myArray;
}
int main(void)
{
printf("%s",myFunction);
return 0;
}
But this just prints the pointer: uF$. Or if I print the function as an integer instead, it prints: 791013. So how do I actually print the 5 elements in the array?
Thanks!
char* myFunction (void)
{
char myArray[5] = {'one','two','three','four','five'}; // WRONG see 1 and 2
return myArray; // WRONG see 3
}
int main(void)
{
printf("%s",myFunction); // WRONG see 4
return 0;
}
As Kerrek pointed out many things are wrong with this code. Two of them are
1.You can't declare char
array like this
char myArray[5] = {'one','two','three','four','five'};
2.You can't return a pointer to an automatic local variable, here pointer is myArray
(after decay). This will invoke undefined behavior .
In this example you can't print array, because it simply is not well-formed string.
Of sure we can give you right answer and we give you correct piece of code, but I think in this case it will be more useful to you to understand the basic elements of C. You probably need to read more about difference between chars literals and strings. Book The C Programming Language by Kernigan and Ritchie will better source of information to you. You only need to read Chapter 1 - A Tutorial Introduction and you can catch the main idea.
By the time I wrote this code, the gentlemen had already answered the question. You may use the following code to get through. Please take note that I did not free the memory. You must free both the strings str[x] and str
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** create_array();
int main(int argc, char** argv) {
char** str = create_array();
while (*str) {
printf("%s\n", *str);
++str;
}
return (EXIT_SUCCESS);
}
char** create_array() {
char** strings = (char**) malloc(sizeof (char*) * 6);
strings[0] = strcpy(malloc(sizeof ("abc")), "abc");
strings[1] = strcpy(malloc(sizeof ("two")), "two");
strings[2] = strcpy(malloc(sizeof ("three")), "three");
strings[3] = strcpy(malloc(sizeof ("four")), "four");
strings[4] = strcpy(malloc(sizeof ("five")), "five");
strings[5] = 0; //Ends the loop
return strings;
}
You have declaration error in your array char is only 1 character '@' whatever you can do is
char myArray[5] = {'o','n','e',',','t','w','o',',','t','h','r','e','e',',','f','o','u','r',',','f','i','v','e'};
however it is just unreadable, I would suggest to use strings or char** myArray; to make array off char arrays :-B
printf("%s",myFunction);
will never work , since function calls in c must have () fixed:
tempArray = myFunction();
for (i=0;i<5;i++)
printf("%c",tempArray[i]);
string str_array[5] = { "one", "two", "three", "four", "five" };
string* pointer = &str_array[0];
for(int i=0; i<5; ++i)
cout << *pointer++ << endl;
OR
#include <stdio.h>
#include <stdlib.h>
char** myFunction()
{
char** myArray = (char**)malloc(sizeof(char*)*5);
myArray[0] = "one";
myArray[1] = "two";
myArray[2] = "three";
myArray[3] = "four";
myArray[4] = "five";
return myArray;
}
int main()
{
char** result = myFunction();
for(int i=0; i<5; i++)
printf("%s\n", result[i]);
free(result);
system("PAUSE");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.