c_cpp C\C ++中字符串常量的存储问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C\C ++中字符串常量的存储问题相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char *pstr = "hello";
    char sstr[] = "hello";
    printf("%d ", strlen(pstr));
    printf("%d ", strlen(sstr));
    printf("%d\n", strlen("hello"));
    
    printf("%d ", sizeof(pstr));
    printf("%d ", sizeof(sstr));
    printf("%d\n", sizeof("hello"));
}
#include<stdio.h>
#include<stdlib.h>

int main()
{
  char str1[] = "hello world";
  char str2[] = "hello world";

  char* str3 = "hello world";
  char* str4 = "hello world";

  if(str1 == str2)
    printf("str1 and str2 are same.\n");
  else
    printf("str1 and str2 are not same.\n");

  if(str3 == str4)
    printf("str3 and str4 are same.\n");
  else
    printf("str3 and str4 are not same.\n");
}

以上是关于c_cpp C\C ++中字符串常量的存储问题的主要内容,如果未能解决你的问题,请参考以下文章