Arrays 为什么我不能用一个字符串来分隔字母?
我想把所有的字母都放在一个字符串中,然后分别放入一个数组中。但我收到了一些错误,我无法理解 10 20 E:\FALL SEM 20-21\CS\C codes\Untitled3.C[警告]传递'strcpy'的参数2将从整数生成指针,而不进行强制转换 我的代码是Arrays 为什么我不能用一个字符串来分隔字母?,arrays,c,Arrays,C,我想把所有的字母都放在一个字符串中,然后分别放入一个数组中。但我收到了一些错误,我无法理解 10 20 E:\FALL SEM 20-21\CS\C codes\Untitled3.C[警告]传递'strcpy'的参数2将从整数生成指针,而不进行强制转换 我的代码是 #include<stdio.h> #include<string.h> char array[10][100],string[100]; int top=0; void push(char elem)
#include<stdio.h>
#include<string.h>
char array[10][100],string[100];
int top=0;
void push(char elem)
{
strcpy(array[top],elem);
top++;
}
int main()
{
printf("Enter the string: \n");
fgets(string,100,stdin);
int length;
length=strlen(string);
int i=0;
while((string[i])!='\0')
{
push(string[i]);
i++;
}
printf("%d",length);
}
strcpy用于复制字符串和以空字符结尾的字符序列。要使用它,您应该从字符中生成字符串并传递它们。此外,还必须修复push参数的类型
如果需要有关错误的帮助,则需要显示错误。字符串是数组。如果要复制字符串,只需使用strcpy,无需使用自己的push函数一次复制一个字符:strcpyarray[0],string;。或者,若要将字符串复制到数组中10次,可以在循环中执行。当然,一定要保持在边界内。@Baba_yaga您的函数推送没有意义。描述你正在尝试做什么。这是一个简单的例子。您的代码所要做的并没有实际的应用程序。即使您满足警告并按下面的答案推送一个2个字符的字符串,如果字符串大于10个字符,您将溢出数组,并且数组中有所有额外的水平空间[100],其中98个未使用。你能描述一下你想做什么吗?你说你一直收到警告。。。您确定要对源代码进行更改、保存或从该源代码编译吗?strcpyarray[0]和字符串都不是;下面的答案也不应该在你的操作中产生警告。如果我按照你的更正仍然收到错误,如27 8 E:\FALL SEM 20-21\CS\C codes\Untitled3.C[warning]传递参数1“push”会使指针从整数变为无整数cast@Baba_yaga . 确保您正在编译预期的源文件。
#include<stdio.h>
#include<string.h>
char array[10][100],string[100];
int top=0;
void push(const char* elem) /* use const char* to receive strings that won't be modified */
{
strcpy(array[top],elem);
top++;
}
int main(void)
{
printf("Enter the string: \n");
fgets(string,100,stdin);
int length;
length=strlen(string);
int i=0;
while((string[i])!='\0')
{
char str[2] = {string[i], '\0'}; /* create a string */
push(str); /* and push that */
i++;
}
printf("%d",length);
}