Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中读取字符串数组_C_Arrays_String - Fatal编程技术网

在C中读取字符串数组

在C中读取字符串数组,c,arrays,string,C,Arrays,String,我想读取数组中的一组字符串。数组的大小将在运行时使用malloc或alloc确定,即字符串的数量被接受为输入。我尝试了以下代码,但无效。 char *array; array=(char*)malloc(sizeof(char)*t); //t is the size of array for(int temp=0;temp<t;temp++) { gets(a[temp]); } char*数组; 数组=(char*)malloc(sizeof(char)*t)//t是数组的大

我想读取数组中的一组字符串。数组的大小将在运行时使用
malloc
alloc
确定,即字符串的数量被接受为输入。我尝试了以下代码,但无效。

char *array;
array=(char*)malloc(sizeof(char)*t); //t is the size of array
for(int temp=0;temp<t;temp++)
{
    gets(a[temp]);
}
char*数组;
数组=(char*)malloc(sizeof(char)*t)//t是数组的大小

对于(int temp=0;tempC)而言,没有任何自动内置的字符串存储支持,没有“字符串”变量,可以自动增长以容纳适当数量的字符。您需要自己分配内存

您现在要做的是为适当数量的字符指针分配位置,而不是为任何字符
调用正在写入未分配的内存,这是一种未定义的行为。由于每个整数都完全适合单个分配,因此类似的代码适用于整数。字符串更复杂,因此需要做更多的工作

如果这些字符串的长度有任何已知的上限,您可以使用固定长度的临时缓冲区,然后在知道所需大小后将其复制到新分配的动态内存中。如果没有这样的上限,您需要重复相同的概念,读取固定块,存储该块,如果找不到行尾,则重新读取在另一个块中,使用
realloc()
增加此字符串的内存分配,添加新字符,并重复,直到行尾


作为旁注,您应该始终避免使用
,因为它不支持保护您的程序免受缓冲区溢出。最好使用fgets(),记录在同一页上。

您分配的空间正好是一个字符串,以零终止格式存储

如果要存储多个字符串的数组,则需要
malloc()
一个
char*
加上多个字符串的数组

此外,
gets()
是不安全的,因为没有大小限制。请改用。它具有以下签名:

char *fgets(char *restrict s, int n, FILE *restrict stream);
由于它需要一个流,您只需给出
stdin

所以读一行最好的方法是

char * read_one_line(uint32_t maxbufsize)
{
    char * s = malloc(maxbufsize);
    if (!fgets(s, maxbufsize, stdin)) {
        free(s);
        return NULL;
    } else {
        char * s2 = realloc(s, strlen(s)+1); // +1 for the NUL at the end
        if (s2) {
            return s2;
        } else {
            return s; // should never happen as the memory block is shrinked
        }
    }
}

此函数分配一行所需的内存,读取后适当调整其大小,并将其留给调用者在适当的时间再次释放。

首先需要为字符串数组(
char*
)分配空间:

然后需要为每个字符串分配空间(让我们
50
作为这些字符串的最大字符数):

inti=0,m=50;
对于(i=0;i
然后你可以做你想做的事:

for(i = 0; i < t; ++i)
    scanf("%50s", array[i]);
(i=0;i scanf(“%50s”,数组[i]);
与其使用
gets
,不如使用
scanf
和指定的宽度(
%50s
=50个字符+
'\0'
)。

只要记住,字符数组中最后应该有一个
NUL
字符

因此,使用
alloc
malloc
分配
t+1
字节内存

t
字节用于字符,1字节用于终止
NUL
字符

for
循环之后,即循环之外,只需写

a[temp] = '\0';
然后你可以在工作中接受它

我希望它能起作用

看起来像这样

char *a = malloc(sizeof((char)*(t+1)));//t is number if characters
int temp;
for(temp=0; temp<t; temp++) {
    gets(a[temp]);
}
a[temp] = 0;

读取输入中字符串数组的另一种有效方法是什么?什么,你想为每个字符调用
malloc
?“times”在你的上下文中是什么意思?你的意思是字节吗?是的,我的意思是(t+1)字节…,“t”字节用于存储字符串,1字节用于存储空值。你的代码示例只读取一个字符串(一行)。2.它使用了本质上不安全的
get()
。不好。顺便说一句,sizeof(char)是冗余的;C标准定义并要求它为1。@glglglgl对,sizeof(char)是冗余的。相反,我应该使用malloc(1*t)。为什么不直接使用
malloc(t)
?:-)
a[temp] = '\0';
char *a = malloc(sizeof((char)*(t+1)));//t is number if characters
int temp;
for(temp=0; temp<t; temp++) {
    gets(a[temp]);
}
a[temp] = 0;
printf("%s",a);