C 在函数中设置指针
当我运行这样的函数时,打印的字母是错误的:C 在函数中设置指针,c,C,当我运行这样的函数时,打印的字母是错误的: #include <stdio.h> void getletters(char *one, char *two) { scanf("%c %c",&one, &two); /* if i print one and two here, they are correct). */ } int main(void) { char one, two; getinput(&one, &
#include <stdio.h>
void getletters(char *one, char *two) {
scanf("%c %c",&one, &two);
/* if i print one and two here, they are correct). */
}
int main(void) {
char one, two;
getinput(&one, &two);
char *pone = &one;
char *ptwo = &two;
printf("your letters are %c and %c", *pone, *ptwo); /* These are coming out as the wrong characters */
}
#包括
void getletters(字符*1,字符*2){
scanf(“%c%c”、&1和&2);
/*如果我在这里打印1和2,则它们是正确的)。*/
}
内部主(空){
一,二,;
getinput(一个和两个);
char*pone=&one;
char*ptwo=&two;
printf(“您的字母是%c和%c”,*pone,*ptwo);/*这些字符是错误的*/
}
我有错误的语法吗?我是否错误地使用了指针?在scanf函数中,不需要获取变量的地址。尝试:
void getletters(char *one, char *two) {
scanf("%c %c", one, two); // one and two are already pointers...
}
您接受指向char的指针,然后使用&
操作符获取指向char的指针,然后将它们提供给需要指向char的指针的函数
你也有一个打字错误
相反,写下:
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
}
试试这个
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
/* if i pint one and two here, they are correct). */
}
1和2已经是getLetters中的指针,因此不需要将它们的地址传递给scanf
此外,在main中,您试图向printf函数传递指针。这是错误的,您必须按值传递参数:
printf("your letters are %c and %c", *pone, *ptwo);
试试这个
#include <stdio.h>
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
}
int main (int argc, char const* argv[])
{
char one, two;
getletters(&one, &two);
printf("your letters are %c and %c\n",one, two); /* These are coming out as the wrong characters */
return 0;
}
#包括
void getletters(字符*1,字符*2){
scanf(“%c%c”,一,二);
}
int main(int argc,char const*argv[]
{
一,二,;
获取信件(一封和两封);
printf(“您的字母是%c和%c\n”,一,二);/*这些字符是错误的*/
返回0;
}
转换说明符
%c
预期的参数类型是什么?将其与&one
和&two
的类型进行比较,以供参考,此处未“返回”任何内容。你的意思是我怀疑是“打印的”。是不是getletters
/getinput
不匹配是打字错误?而且你没有发现分段冲突?我真的很惊讶你这么不走运。
#include <stdio.h>
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
}
int main (int argc, char const* argv[])
{
char one, two;
getletters(&one, &two);
printf("your letters are %c and %c\n",one, two); /* These are coming out as the wrong characters */
return 0;
}
scanf("%c %c",&one, &two");