c++ - What's the difference between char str[] and char* str? -


i have code , has problem.

#include <iostream> #include <stdio.h> using namespace std; void main() { char* str="hello_world"; cout<<str<<endl; str[3]='\0'; cout<<str<<endl; } 

but if change char* str char str[]. works fine.why?

when declare char str[] declaring array of chars (which accessible both read , written), , array initialized sequence of characters i.e. "this test string" copied elements in array.

when declare char* str, declaring pointer points directly constant literal - not copy. these can read.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -