相关疑难解决方法(0)

在字符串上使用指针

我真的很困惑在字符串上使用指针.感觉他们遵守不同的规则.请考虑以下代码

  1. char *ptr = "apple";// perfectly valid here not when declaring afterwards like next
    
    ptr = "apple"; // shouldn't it be *ptr = "apple"
    
    Run Code Online (Sandbox Code Playgroud)
  2. printf()表现不同-

    printf("%s", ptr) // Why should I send  the address instead of the value
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我还在一本书中看到了以下代码

    char str[]="Quest";
    char *p="Quest";
    
    str++; // error, constant pointer can't change
    
    *str='Z'; // works, because pointer is not constant
    
    p++; // works, because pointer is not constant
    
    *p = 'M'; // error, because string is constant
    
    Run Code Online (Sandbox Code Playgroud)

我无法理解应该暗示什么

请帮忙,我在其他地方找不到任何信息

c c-strings char-pointer

4
推荐指数
1
解决办法
329
查看次数

标签 统计

c ×1

c-strings ×1

char-pointer ×1