C: Behaviour of arrays when assigned to pointers

前端 未结 4 1490
名媛妹妹
名媛妹妹 2021-01-27 20:17
#include 

main()
{
  char * ptr;

  ptr = \"hello\";


  printf(\"%p %s\" ,\"hello\",ptr );

  getchar();

}

Hi, I am trying to underst

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-27 21:17

    #include 
    
    main()
    {
       char * ptr;
    
       ptr = "hello"; 
    
      //instead of above tow lines you can write char *ptr = "hello" 
    
       printf("%p %s" ,"hello",ptr );
    
       getchar();
    
    }
    

    Here you have assigned string literal "hello" to ptr it means string literal is stored in read only memory so you can't modify it. If you declare char ptr[] = "hello";, then you can modify the array.

提交回复
热议问题