Abh*_*hek 2 c pointers segmentation-fault
我只是在C中学习一些指针,我碰巧知道使用*one可以取消引用指针.所以我编写了以下代码来检查它.
#include<stdio.h>
#include<string.h>
char *findChar(char *s, char c){
while(*s!=c){
s++;
}
return s;
}
int main(){
char myChar='a';
const char myString[]="Hello abhishek";
char *location;
location = findChar(myString,myChar);
puts(location);
char temp = *location;
printf(temp);
}
Run Code Online (Sandbox Code Playgroud)
我假设temp应该得到字符指针位置指向的值,但是这个程序给了我一个分段错误.请清楚我做错了什么?
以下是不正确的:
char temp = *location;
printf(temp);
Run Code Online (Sandbox Code Playgroud)
如果要打印出char,请使用以下命令:
char temp = *location;
printf("%c\n", temp);
Run Code Online (Sandbox Code Playgroud)
第一个参数printf()应该是格式字符串.
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |