用指针法打印出字符数组里的字符,打印出来的是同样的字符
int main(){
char str1; //MAX = 1024
char str2;
char ch;
char* target = str1;
int i, j, length = 0;
printf("请输入一串字符到str1中:");
fgets(str1,MAX,stdin);
while (*target++ != '\0')
{
length++;
}
printf("%d\n",length - 1);
for (i = 0; i < length -1 ; i++)
{
printf("str[%d] = %c\n", i, target);
}
return 0;
}
↓打印结果↓
请输入一串字符到str1中:i love
6
str = ?
str = ?
str = ?
str = ?
str = ?
str = ? 指针飞了
#include <stdio.h>
#define MAX 1024
int main()
{
char str1; //MAX = 1024
char str2;
char ch;
char* target = str1;
int i, j, length = 0;
printf("请输入一串字符到str1中:");
fgets(str1,MAX,stdin);
while (*target++ != '\0')
{
length++;
}
printf("%d\n",length - 1);
target = str1; 把飞了的指针弄回原点
for (i = 0; i < length -1 ; i++)
{
printf("str[%d] = %c\n", i, target);
}
return 0;
}
wp231957 发表于 2022-5-18 11:21
指针飞了
#include
#define MAX 1024
谢谢大佬!{:10_298:}
页:
[1]