Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the diference between two declarations?

C++
char a[] = "My name is Peter!";
char *a = "My name is Peter!";
Posted

1 solution

C#
char a1[] = "My name is Peter!";
char *a2 = "My name is Peter!";

*a1 = 'm'; // Allowed.
a2[0] = 'm'; // It will create access violation.

Array declaration creates allocates chars required for the string, and can read/write.
But char* creates a pointer, and it locates to a constant memory, where "My Name is Peter" is copied.
This memory cannot be overwritten.

Considering sizeof these declarations, char* a will give the size of a pointer, usually 4 bytes.
char[] declaration will give the entire size of the string.


Please refer the following link.
Difference between pointer and array [duplicate][^]
 
Share this answer
 
v3
Comments
Shmuel Zang 26-May-13 9:51am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900