Hello I have this code that fills the array with a pointer and print it with a pointer as well. The result is faulse. Here is my code:
您好我有这个代码用指针填充数组并用指针打印它。结果是faulse。这是我的代码:
#include<stdio.h>
#include<stdlib.h>
int main(){
int p;
char po[8];
char *i;
i=po;
for(p=0; p<8; p++){
scanf("%c\n", i++);
}
for(p=0; p<8; p++){
printf("%c\n", *(i++));
}
return 0;
}
where is my fault?
我的错在哪里?
3 个解决方案
#1
1
There can be good reasons for increasing pointers. Simply do not forget to reset them to their initial value before reusing them!
增加指针可能有充分的理由。在重复使用它们之前,不要忘记将它们重置为初始值!
Your code should be:
你的代码应该是:
i=po;
for(p=0; p<8; p++){
scanf("%c\n", i++);
} /* i is now po + 8 ... */
i = po;
for(p=0; p<8; p++){
printf("%c\n", *(i++));
}
Now you should learn not to write such code:
现在你应该学会不写这样的代码:
- blocks are not indented => harder to read
- 块没有缩进=>更难阅读
- value of scanf is never tested: what happens on end of file (Ctrl-D on Unix-like, Ctrl-Z on Windows)?
- 从未测试过scanf的值:文件末尾会发生什么(类似Unix的Ctrl-D,Windows上的Ctrl-Z)?
- not even one single comment...
- 甚至没有一条评论......
#2
1
i
is already pointing at the end of the array. You want to print po
using p
as index:
我已经指向数组的末尾了。您想使用p作为索引打印po:
for(p=0; p<8; p++){
printf("%c\n", po[p]);
Also, you don't need the \n
in the scanf()
call. Any whitespace character in format specifier will ignore all whitepaces in the input and as such you will need to input a non-whitespace character at the end to end the input.
此外,您不需要scanf()调用中的\ n。格式说明符中的任何空白字符都将忽略输入中的所有空格,因此您需要在末尾输入非空白字符以结束输入。
#3
0
The problem is that the pointer i
being incremented in the first loop, will point to the end of the array at the end of the loop (more precise, at the next memory location after the one allocated for po
, i.e. will point to po[8]
which is not part of the array). You can add
问题是指针i在第一个循环中递增,将指向循环结束时数组的末尾(更准确地说,在为po分配的指针之后的下一个内存位置,即将指向po [ 8]不是数组的一部分)。你可以加
i = po;
before the second loop, to make it point again to the beginning of po
(to po[0]
).
在第二个循环之前,使其再次指向po的开头(到po [0])。
#1
1
There can be good reasons for increasing pointers. Simply do not forget to reset them to their initial value before reusing them!
增加指针可能有充分的理由。在重复使用它们之前,不要忘记将它们重置为初始值!
Your code should be:
你的代码应该是:
i=po;
for(p=0; p<8; p++){
scanf("%c\n", i++);
} /* i is now po + 8 ... */
i = po;
for(p=0; p<8; p++){
printf("%c\n", *(i++));
}
Now you should learn not to write such code:
现在你应该学会不写这样的代码:
- blocks are not indented => harder to read
- 块没有缩进=>更难阅读
- value of scanf is never tested: what happens on end of file (Ctrl-D on Unix-like, Ctrl-Z on Windows)?
- 从未测试过scanf的值:文件末尾会发生什么(类似Unix的Ctrl-D,Windows上的Ctrl-Z)?
- not even one single comment...
- 甚至没有一条评论......
#2
1
i
is already pointing at the end of the array. You want to print po
using p
as index:
我已经指向数组的末尾了。您想使用p作为索引打印po:
for(p=0; p<8; p++){
printf("%c\n", po[p]);
Also, you don't need the \n
in the scanf()
call. Any whitespace character in format specifier will ignore all whitepaces in the input and as such you will need to input a non-whitespace character at the end to end the input.
此外,您不需要scanf()调用中的\ n。格式说明符中的任何空白字符都将忽略输入中的所有空格,因此您需要在末尾输入非空白字符以结束输入。
#3
0
The problem is that the pointer i
being incremented in the first loop, will point to the end of the array at the end of the loop (more precise, at the next memory location after the one allocated for po
, i.e. will point to po[8]
which is not part of the array). You can add
问题是指针i在第一个循环中递增,将指向循环结束时数组的末尾(更准确地说,在为po分配的指针之后的下一个内存位置,即将指向po [ 8]不是数组的一部分)。你可以加
i = po;
before the second loop, to make it point again to the beginning of po
(to po[0]
).
在第二个循环之前,使其再次指向po的开头(到po [0])。