|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
int main()
{
char str1[MAX]; //MAX = 1024
char str2[MAX];
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[i]);
}
return 0;
}
↓打印结果↓
请输入一串字符到str1中:i love
6
str[0] = ?
str[1] = ?
str[2] = ?
str[3] = ?
str[4] = ?
str[5] = ?
指针飞了
#include <stdio.h>
#define MAX 1024
int main()
{
char str1[MAX]; //MAX = 1024
char str2[MAX];
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[i]);
}
return 0;
}
|
|