简体   繁体   English

在 c 中打印 char 指针值

[英]print char pointer value in c

-: was trying to learn pointers in c:- -:试图学习 c 中的指针:-

I couldn't print the value at the character pointer, rest of the program works fine.我无法打印字符指针处的值,程序的 rest 工作正常。 It just prints a blank space, didn't get any error though.它只是打印一个空格,但没有收到任何错误。

#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 = &num;
    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);
}

程序输出

Your input is 21 t , which means that scanf will read 21 into num , and the in-between whitespace into ch , which is what you specify in the first argument to scanf .您的输入是21 t ,这意味着scanf会将 21 读入num ,并将中间空格读入ch ,这是您在scanf的第一个参数中指定的内容。 You either separate the format specifiers by a blank space, ie %d %c , or enter the input without the whitespace, hence 21t .您可以用空格分隔格式说明符,即%d %c ,或者输入不带空格的输入,因此21t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM