![](/img/trans.png)
[英]C - Using a char pointer to print int value, not getting expected result
[英]print char pointer value in c
-:试图学习 c 中的指针:-
我无法打印字符指针处的值,程序的 rest 工作正常。 它只是打印一个空格,但没有收到任何错误。
#include<stdio.h>
void main() {
int num, *num_ptr;
char ch, *ch_ptr;
printf("Enter a number and a single character : ");
scanf("%d%c",&num,&ch);
num_ptr = #
ch_ptr = &ch;
//printf("\ncontent at num_ptr = %p \n",num_ptr);
//printf("content at ch_ptr = %p\n",ch_ptr);
//printf("value of the content at num_ptr = %d\n",*num_ptr);
/* error part */
printf("value of the content at ch_ptr = %c\n",*ch_ptr);
/* error part */
//printf("\n");
//printf("num_ptr + 2 = %p\n",num_ptr+2);
//printf("ch_ptr + 2 = %p\n",ch_ptr+2);
//printf("\n");
//printf("num_ptr + 1 = %p\n",++num_ptr);
//printf("num_ptr - 1 = %p\n",--num_ptr);
//printf("ch_ptr + 1 = %p\n",++ch_ptr);
//printf("ch_ptr - 1 = %p\n",--ch_ptr);
//printf("\n");
//printf("num_ptr + 5 = %p\n",num_ptr+5);
//printf("ch_ptr - 5 = %p\n\n",--ch_ptr-5);
}
您的输入是21 t
,这意味着scanf
会将 21 读入num
,并将中间空格读入ch
,这是您在scanf
的第一个参数中指定的内容。 您可以用空格分隔格式说明符,即%d %c
,或者输入不带空格的输入,因此21t
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.