C语言中字符进入数组的分割错误
我试图将不同的核苷酸碱基输入一个数组,但我的数组有问题。它要么在我输入一个字符后返回一个分段错误,要么在提示我输入另一个字符之前循环运行3次。提前谢谢你的帮助 这是我的代码:C语言中字符进入数组的分割错误,c,arrays,segmentation-fault,C,Arrays,Segmentation Fault,我试图将不同的核苷酸碱基输入一个数组,但我的数组有问题。它要么在我输入一个字符后返回一个分段错误,要么在提示我输入另一个字符之前循环运行3次。提前谢谢你的帮助 这是我的代码: #include<stdio.h> #include<string.h> #include<ctype.h> int main(void) { char strand[20]; char nucleotide; int position=0; whil
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(void)
{
char strand[20];
char nucleotide;
int position=0;
while(position<20)
{
printf("Enter one capital character:\n");
scanf("%c",nucleotide);
if(toupper(nucleotide) == 'A')
{
printf("You entered an A\n",nucleotide);
strand[position] = nucleotide; // store the letter in an array
position = position + 1; // gives the new letter a position in the array
}
else if(toupper(nucleotide) == 'G')
{
printf("You entered a G\n");
strand[position] = nucleotide; // store the letter in an array
position = position + 1; // gives the new letter a position in the array
}
else if(toupper(nucleotide) == 'C')
{
printf("You entered a C\n");
strand[position] = nucleotide; // store the letter in an array
position = position + 1; // gives the new letter a position in the array
}
else if(toupper(nucleotide) == 'U')
{
printf("You entered a U\n");
strand[position] = nucleotide; // store the letter in an array
position = position + 1; // gives the new letter a position in the array
}
else
{
printf("You have not entered a valid character.\n Please enter a character found in an RNA strand.\n");
}
printf("Congratulations, you entered %c\n",nucleotide);
}
return 0;
}
#包括
#包括
#包括
内部主(空)
{
炭链[20];
焦核苷酸;
int位置=0;
而(位置您的错误是scanf(“%c”,核苷酸);
用scanf(“%c”,核苷酸);
更改它,因为scanf接受指针作为参数
此外,您不需要知道输入了哪个字符,您可以这样做:
#include<stdio.h>
#include<ctype.h>
int main(void) {
char strand[20];
char nucleotide;
int position = 0, i;
while (position < 20) {
printf("Enter one capital character:\n");
scanf("%c", &nucleotide);
if (toupper(nucleotide) == 'A' || toupper(nucleotide) == 'G' || toupper(nucleotide) == 'C' ||
toupper(nucleotide) == 'U') {
printf("You entered an %c\n", toupper(nucleotide));
strand[position] = nucleotide; // store the letter in an array
++position;// gives the new letter a position in the array
} else
printf("You have not entered a valid character.\n Please enter a character found in an RNA strand.\n");
printf("Congratulations, you entered %c\n", nucleotide);
}
printf("Your sequence is: ");
for (i = 0; i < 20; ++i)
printf("%c", strand[i]);
printf("\n");
return 0;
}
#包括
#包括
内部主(空){
炭链[20];
焦核苷酸;
int位置=0,i;
同时(位置<20){
printf(“输入一个大写字符:\n”);
scanf(“%c”和核苷酸);
如果(核苷酸)='A'| |核苷酸)='G'| |核苷酸)='C'||
toupper(核苷酸)='U'){
printf(“您输入了一个%c\n”,toupper(核苷酸));
链[position]=核苷酸;//将字母存储在数组中
++position;//为新字母指定数组中的位置
}否则
printf(“您没有输入有效字符。\n请输入在RNA链中找到的字符。\n”);
printf(“祝贺您,您输入了%c\n”,核苷酸);
}
printf(“您的序列是:”);
对于(i=0;i<20;++i)
printf(“%c”,股[i]);
printf(“\n”);
返回0;
}
您在零件中留下了&-符号。{scanf(“%c”,核苷酸);}
while(position当我编译你的代码时,我从编译器那里得到这个
gcc main.c
main.c:在函数“main”中:
main.c:20:9:警告:格式“%c”要求参数类型为“char*”,但参数2的类型为“int”[-Wformat]
编译器告诉您,参数2应为char*类型。您的第二个参数是nucleotice
,您将其声明为char类型。
在这种特殊情况下,解决方案是在核苷酸
的左侧添加&
,因为这将返回一个指向核苷酸的指针,这是scanf期望的第二个参数
但是这里的重要教训是,您应该阅读编译器警告和错误,因为它们通常足以知道问题所在。您需要编辑问题以使其更具可读性。点击问题底部标志下方的edit
。然后选择问题中的所有代码在上。点击编辑框顶部的{}
。然后提交编辑过的问题。并删除每行代码之间的所有空行,使其比要求的长。scanf(“%c”,&d)
请不要通过修改代码格式给他鱼。这是每个SO用户都需要学习的事情,因此编辑帖子是没有帮助的。人们确实需要花点时间,至少在发布之前快速浏览一下他们自己的问题。“核苷酸”之前的“&”符号有什么作用为什么你不看一本书?为什么你要用C++编译器编译这个C代码?错误是唯一的解释。删除我的答案的时间。或者用C编译器的正确信息编辑它。谢谢你注意到Jens Gustedt。
while(position<20)
{
printf("Enter one capital character:\n");
scanf("%c",nucleotide);
if(toupper(nucleotide) == 'A')