How can I return an array of strings in an ANSI C program?
如何在ANSI C程序中返回字符串数组?
For example:
例如:
#include<stdio.h>
#define SIZE 10
char ** ReturnStringArray()
{
//How to do this?
}
main()
{
int i=0;
//How to do here???
char str ** = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
printf("%s", str[i]);
}
}
3 个解决方案
#1
3
You could do the following. Error checking omitted for brevity
您可以执行以下操作。为了简便起见,省略了错误检查
char** ReturnStringArray() {
char** pArray = (char**)malloc(sizeof(char*)*SIZE);
int i = 0;
for ( i = 0; i < SIZE; i++ ) {
pArray[i] = strdup("a string");
}
return pArray;
}
Note that you'd need to correspondingly free the returned memory.
注意,您需要相应地释放返回的内存。
Additionally in your printf call you'll likely want to include a \n
in your string to ensure the buffer is flushed. Otherwise the strings will get queued and won't be immediately printed to the console.
另外,在printf调用中,您可能希望在字符串中包含一个\n,以确保刷新缓冲区。否则,字符串将排队,不会立即打印到控制台。
char** str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
printf("%s\n", str[i]);
}
#2
2
Do it this way
这样做
#include<stdio.h>
#define SIZE 10
char ** ReturnStringArray()
{
//How to do this?
char **strList = (char **)malloc(sizeof(char*) * SIZE);
int i = 0;
if (strList != NULL){
for (i = 0; i < SIZE; i++){
strList[i] = (char *)malloc(SIZE * sizeof(char) + 1);
if (strList[i] != NULL){
sprintf(strList[i], "Foo%d", i);
}
}
}
return strList;
}
main()
{
int i=0;
//How to do here???
char **str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
printf("%s", str[i]);
}
}
- Problem 1: Your double pointer declaration was incorrect
- 问题1:您的双指针声明是错误的。
- Problem 2: You need to know the size of the string for each pointer in the double-pointer..
- 问题2:你需要知道双指针中每个指针的字符串大小。
- Problem 3: The onus is placed on you to free the memory when done with it..
- 问题3:你有责任释放记忆
The code sample above assumes that the maximum size of the string will not exceed the value of SIZE
, i.e. 10 bytes in length...
上面的代码示例假设字符串的最大大小不会超过大小的值,即10字节的长度……
Do not go beyond the boundary of the double pointer as it will crash
不要超过双指针的边界,因为它会崩溃
#3
1
pls dont typecast the return of malloc, you have not included <stdlib.h>
and as someone pointed out above lack of prototype will result in int being casted to char **. Accidently your program may or may not work at all.
请不要打印malloc的返回,您没有包含
#1
3
You could do the following. Error checking omitted for brevity
您可以执行以下操作。为了简便起见,省略了错误检查
char** ReturnStringArray() {
char** pArray = (char**)malloc(sizeof(char*)*SIZE);
int i = 0;
for ( i = 0; i < SIZE; i++ ) {
pArray[i] = strdup("a string");
}
return pArray;
}
Note that you'd need to correspondingly free the returned memory.
注意,您需要相应地释放返回的内存。
Additionally in your printf call you'll likely want to include a \n
in your string to ensure the buffer is flushed. Otherwise the strings will get queued and won't be immediately printed to the console.
另外,在printf调用中,您可能希望在字符串中包含一个\n,以确保刷新缓冲区。否则,字符串将排队,不会立即打印到控制台。
char** str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
printf("%s\n", str[i]);
}
#2
2
Do it this way
这样做
#include<stdio.h>
#define SIZE 10
char ** ReturnStringArray()
{
//How to do this?
char **strList = (char **)malloc(sizeof(char*) * SIZE);
int i = 0;
if (strList != NULL){
for (i = 0; i < SIZE; i++){
strList[i] = (char *)malloc(SIZE * sizeof(char) + 1);
if (strList[i] != NULL){
sprintf(strList[i], "Foo%d", i);
}
}
}
return strList;
}
main()
{
int i=0;
//How to do here???
char **str = ReturnStringArray();
for(i=0 ; i<SIZE ; i++)
{
printf("%s", str[i]);
}
}
- Problem 1: Your double pointer declaration was incorrect
- 问题1:您的双指针声明是错误的。
- Problem 2: You need to know the size of the string for each pointer in the double-pointer..
- 问题2:你需要知道双指针中每个指针的字符串大小。
- Problem 3: The onus is placed on you to free the memory when done with it..
- 问题3:你有责任释放记忆
The code sample above assumes that the maximum size of the string will not exceed the value of SIZE
, i.e. 10 bytes in length...
上面的代码示例假设字符串的最大大小不会超过大小的值,即10字节的长度……
Do not go beyond the boundary of the double pointer as it will crash
不要超过双指针的边界,因为它会崩溃
#3
1
pls dont typecast the return of malloc, you have not included <stdlib.h>
and as someone pointed out above lack of prototype will result in int being casted to char **. Accidently your program may or may not work at all.
请不要打印malloc的返回,您没有包含