partida partida - 6 months ago 35
C++ Question

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

I have this code and it has problem.

#include <iostream>
#include <stdio.h>
using namespace std;
void main()
char* str="hello_world";

but if I change
char* str
char str[]
it works fine.Why?


When you declare char str[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters i.e. "This is test string" is copied to the elements in this array.

When you declare char* str, you are declaring a pointer that points directly to some constant literal - not a copy. These can only be read.