Char* and Char [] differences in C language

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.