C中的指针初始化概念 [英] Pointer initialization concept in C

查看:66
本文介绍了C中的指针初始化概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这是错误的?

char *p;   
*p='a';

这本书只说了-使用未初始化的指针. 请任何人解释这是怎么回事?

The book only says -use of uninitialized pointer. Please can any one explain how that is?

推荐答案

char *c; //a pointer variable is being declared 
*c='a';

您使用了解引用运算符来访问c所指向的变量的值,但指针变量c没有指向任何变量,这就是您遇到运行时问题的原因.

you used the dereferencing operator to access the value of the variable to which c points to but your pointer variable c is not pointing to any variable thats why you are having runtime issues.

char *c; //declaration of the pointer variable
char var; 
c=&var; //now the pointer variable c points to variable var.
*c='a'; //value of var is set to 'a' using pointer 
printf("%c",var); //will print 'a' to the console

希望这会有所帮助.

这篇关于C中的指针初始化概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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