c++ - What's the difference between char str[] and char* str? -
this question has answer here:
- what difference between char s[] , char *s? 12 answers
- difference between [square brackets] , *asterisk 5 answers
- difference between char* , char[] 6 answers
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
Post a Comment