partida partida - 1 year ago 93
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download