Original: Char* and Char [] differences in C language
Want to pick up the lost things, or very hard ah, today I found that I even char* and Char [] The difference is not known.
Many people think that the two definitions have the same effect, in fact, the difference is very large. Here are some views of the individual, and there is an incorrect place to look.
In essence, char *s defines a pointer to a char type that knows only the memory unit it points to and does not know how large the memory unit is, so:
When char *s = "Hello", you cannot use the s[0]= ' a ' statement to assign a value. This is the hint that memory cannot be "written".
When s[]= "Hello" with char, it is perfectly possible to use s[0]= ' a '; assignment, which is a regular array operation.
If char s[] = "Hello";
char *p = s;
You can also use p[0] = ' A ', because this is P ==s, which is a pointer to an array.
Here's another definition:
Char *s = (char *) malloc (n (www.111cn.net));//where n is the size to open space
This sentence is actually equivalent to:
Char S[n]; Defines a pointer to an array that can be used to subscript the array
Example
Code to copy code as follows
#include <stdio.h>
int main (int argc, char* argv[]) {
char* BUF1 = "This is a test";
Char buf2[] = "This is a test";
printf ("Size of Buf1:%d\n", sizeof (BUF1));
printf ("Size of Buf2:%d\n", sizeof (BUF2));
return 0;
}
The result is:
$ >./main
Size of Buf1:4
Size of Buf2:15
From:http://www.111cn.net/net/c/66247.htm
Related content
- 2014.02.15 solve the problem of ASP. NET MVC3 and Fusioncharts garbled
- 2011.01.03asp.net charipet characters commonly used segment and static method
- The difference between 2011.01.01nchar,char,varchar and nvarchar
- Char type lengths in 2010.12.26Java and C + + learn notes
- 2010.12.25string,pchar,pbyte,array of Char,array of Byte
- Conversion between 2009.10.11CString, int, string, char*
- 2008.04.26char,tchar,wchar differences
- WebChart usage of 2008.01.12ComponentOne
Char* and Char [] differences in C language