youyongsong
5/1/2014 - 11:51 AM

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

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");
}