使用带有指向字符的指针的 scanf 函数 [英] using scanf function with pointers to character

查看:37
本文介绍了使用带有指向字符的指针的 scanf 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码.

int main(){
   char arrays[12];
   char *pointers;
   scanf("%s",arrays);
   scanf("%s",pointers);
   printf("%s",arrays);
   printf("%s",pointers);
   return 0;
}

为什么我写`scanf("%s",pointers)时会报错?

Why does it give an error when I write `scanf("%s",pointers)?

推荐答案

char *pointers; 

必须被初始化.你不能将字符串扫描到pointers,直到你将它指向某个地址.计算机需要知道在哪里存储从键盘读取的值.

must be initialized.You can not scan string into pointers until you point it to some address. The computer needs to know where to store the value it reads from key board.

int main(){
   char arrays[12];
   char *pointers= arrays;
   scanf("%s",pointers);
   printf("%s",pointers);
   return 0;
}

这篇关于使用带有指向字符的指针的 scanf 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆