将字符串复制到数组中? #包括 #包括 #包括 #定义字符串长度20 #定义最大值30 int read_字符串(字符字符串[],int n); int main(){ int i=0; 字符*name_列表[MAX]; 字符字[字符串长度+1]; 对于(;;i++){ printf(“输入一个单词。\n”); 读取字符串(字、字符串长度); 如果(字[i]='\0') 打破 名称列表[i]=malloc(sizeof(char)*20); strcat(名称/列表[i],文字); } } int read_字符串(字符字符串[],int n){ int ch,i=0; 而((ch=getchar())!='\n') if(i
这个程序的目的是读入单词并将它们放入指针数组中进行排序。这就是我到目前为止所知道的,我的调试器说strcat的使用是不安全的,但我不知道为什么。它说要使用strcat___s,但这会使我的程序崩溃。有关如何使其工作的任何帮助?好的,我测试了您的代码,得到了以下最后一个对我有效的代码,并且在使用将字符串复制到数组中? #包括 #包括 #包括 #定义字符串长度20 #定义最大值30 int read_字符串(字符字符串[],int n); int main(){ int i=0; 字符*name_列表[MAX]; 字符字[字符串长度+1]; 对于(;;i++){ printf(“输入一个单词。\n”); 读取字符串(字、字符串长度); 如果(字[i]='\0') 打破 名称列表[i]=malloc(sizeof(char)*20); strcat(名称/列表[i],文字); } } int read_字符串(字符字符串[],int n){ int ch,i=0; 而((ch=getchar())!='\n') if(i,c,arrays,string,C,Arrays,String,这个程序的目的是读入单词并将它们放入指针数组中进行排序。这就是我到目前为止所知道的,我的调试器说strcat的使用是不安全的,但我不知道为什么。它说要使用strcat___s,但这会使我的程序崩溃。有关如何使其工作的任何帮助?好的,我测试了您的代码,得到了以下最后一个对我有效的代码,并且在使用-Wall编译时不会给我任何警告 由于您使用的是strcat而不是strcpy,因此存储在words中的字符串将添加到数组name\u列表中的数据中。但是,由于您没有将该数组中的所有值都设置为0,因此可能会
-Wall
编译时不会给我任何警告
由于您使用的是strcat
而不是strcpy
,因此存储在words
中的字符串将添加到数组name\u列表中的数据中。但是,由于您没有将该数组中的所有值都设置为0
,因此可能会发生一些垃圾数据存储在name\u list[i]
中,并且字符串在垃圾数据之后被连接起来
因此,我使用了这样的方法,即您分配的内存中的所有值都是零。另一种方法是只保留malloc
,然后在strcpy()
中更改strcat()
#包括
#包括
#包括
#定义字符串长度20
#定义最大值30
int read_字符串(字符字符串[],int n);
int main(){
int i;
字符*name_列表[MAX];
字符字[字符串长度+1];
对于(i=0;i
使用memcpy()函数:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STRING_LENGTH 20
#define MAX 30
int read_string(char string[], int n);
int main(){
int i;
char *name_list[MAX];
char word[STRING_LENGTH + 1];
for (i = 0; i < MAX; i++){
printf("\nEnter a word.\n");
read_string(word, STRING_LENGTH);
printf("\nword%d=%s", i, word);
if (strcmp(word, "") == 0)
break;
name_list[i] = calloc(STRING_LENGTH + 1, 1);
strcat(name_list[i], word);
printf("\nname_list[%d] = %s", i, name_list[i]);
}
return 0;
}
int read_string(char string[], int n){
int ch, i = 0;
while ((ch = getchar()) != '\n')
if (i < n)
string[i++] = ch;
string[i] = '\0';
return i;
}
void *memcpy(void *str1, const void *str2, size_t n)
或strcpy()函数:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STRING_LENGTH 20
#define MAX 30
int read_string(char string[], int n);
int main(){
int i;
char *name_list[MAX];
char word[STRING_LENGTH + 1];
for (i = 0; i < MAX; i++){
printf("\nEnter a word.\n");
read_string(word, STRING_LENGTH);
printf("\nword%d=%s", i, word);
if (strcmp(word, "") == 0)
break;
name_list[i] = calloc(STRING_LENGTH + 1, 1);
strcat(name_list[i], word);
printf("\nname_list[%d] = %s", i, name_list[i]);
}
return 0;
}
int read_string(char string[], int n){
int ch, i = 0;
while ((ch = getchar()) != '\n')
if (i < n)
string[i++] = ch;
string[i] = '\0';
return i;
}
void *memcpy(void *str1, const void *str2, size_t n)
很抱歉,我忘了添加一点,如果用户在提示程序结束后不输入任何内容。我修复了这一问题,但调试器仍然说strcat、strcpy和strcat_、strcpy_的使用是不安全的。此外,for循环现在是无限的。您应该将其更改为(i=0;一旦用户在不输入单词的情况下按enter键,循环就会中断。@moffeltje这是C;您希望得到一个IndexOutOfBounds错误,反而会得到更糟糕的结果。非常感谢,尽管我仍然收到警告消息,但当我打印名称列表数组时,程序不会崩溃,尽管会得到一堆奇怪的字符,而不是世界除了其他可能的问题,调用calloc()
似乎应该是calloc(STRING_LENGTH+1,1)
。为了避免这样的问题,我建议使用strdup()
而不是字符串上的分配/复制操作集。实际上代码很好,但我实际上需要使用malloc而不是calloc