为什么不我需要取消引用在C字符指针在打印前? [英] Why don't I need to dereference a character pointer in C before printing it?

查看:131
本文介绍了为什么不我需要取消引用在C字符指针在打印前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这code的工作?我希望,我需要解引用 PTR 的printf(%S \\ n,* PTR); 之前,我可以把它打印出来,但我得到一个分段错误如果我尝试这样做的。

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
        CHAR名称[] =乔丹;
        字符* PTR =名称;
        的printf(%S \\ n,PTR);
}

希望你们可以给我一些见解。


解决方案

当您打印的字符串:我们需要在启动字符串的地址。

 的printf(%S \\ n,PTR);
                ^与%s地址

它打印的字符,直到 \\ 0 NUL相遇。

尽管打印的聊天 INT ..我们需要的变量的值

 的printf(%C \\ n,* PTR);
               ^与%C打印第一个char *

如果在 scanf()的字符串你总是需要给地址:

  scanf函数(%S,PTR);
            ^字符串地址

也为INT scanf()的一个char

  scanf函数(%C,PTR);
            ^阅读在第一位置的字符地址

注意: scanf()的需要%C 地址来存储扫描的价值在记忆中。

小心的你的 PTR 指向字符串常量,所以你不能在scanf中的的使用。


  

为什么分段故障有下列code?

 的printf(%S \\ n,* PTR);


当你不喜欢这一点,因为%S 的printf间$ P $点 * PTR 作为地址,但它其实不是一个地址,如果你把它当作地址指向被读保护您的程序(过程)的一些位置,这样它会导致分段错误。

PTR 通过名称指向内存中的一些常量字符串(乔丹),如下图:

  2002名
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│J│O│R│D│A│N│'\\ 0'│........
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
  ^
  |
 PTR =名称 ==> PTR = 2002
     * PTR ='J'

的printf(%S \\ n,* PTR); * PTR ='J'和char'J'的ASCII值 74 ,但 74 地址是不是你的过程控制之下,你想从内存读取的位置和它的内存冲突和分割故障。

如果您编译您包含code 的printf(%S \\ n,* PTR); 然后用适当的选项说 - 华尔街 GCC ,你会得到一个警告象下面这样:

警告:格式'%s'的预期类型的char *'的说法,但参数2的类型'诠释'

%S 需求(预计)键入的char * 的地址,但你把值

通知:

 的printf(%S \\ n,* PTR);
        ^^说法-2
        参数-1

Why does this code work? I would expect that I would need to dereference ptr, printf("%s\n", *ptr); before I could print it out, but I get a Segmentation Fault if I try to do it that way.

#include <stdio.h>

int main(int argc, char *argv[])
{
        char name[] = "Jordan";
        char *ptr = name;
        printf("%s\n", ptr);
}

Hope you guys could give me some insight.

解决方案

When you print string we need starting address of string.

printf("%s\n", ptr);
                ^ address with %s   

it prints chars till \0 nul encounter.

Whereas to print chat int .. we need value variable:

printf("%c\n", *ptr);
               ^ * with %c print first char

Where as in scanf() a string you always need to give address:

scanf("%s", ptr);
            ^ string address

Also for int scanf() a char

scanf("%c", ptr);
            ^ read at first location char address 

Note: Scanf() need address with %c to store scanned value in memory.

Be careful your ptr points to constant string so you can't use in scanf.

Why Segmentation fault with following code ?

    printf("%s\n", *ptr);

When you do like this, because of %s printf interprets *ptr as an address, but its actually not an address and if you treat it as address it points to some location that is read protected for your program(process) So it causes an segmentation fault.

Your ptr via name points to some constant string in memory ("Jordan") as in below diagram:

name 2002
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│ 'J' │ 'o' │ 'r' │ 'd' │ 'a' │ 'n' │'\0' │ ........
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
  ^
  |
 ptr = name 

 ==> ptr = 2002
     *ptr = 'J'

In printf("%s\n", *ptr); the *ptr = 'J' and ASCII value of char 'J' is 74 but 74 address is not under your process control and you are trying to read from that memory location and its a memory violation and segmentation fault occurs.

If you compiles you code containing printf("%s\n", *ptr); then with proper option say -Wall with GCC you will get a warning like below:

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’

Says %s need (expects ) an address of type char* but you are putting value

notice:

printf("%s\n",   *ptr);
        ^          ^ argument-2
        argument-1 

这篇关于为什么不我需要取消引用在C字符指针在打印前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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