The difference between Char *a and Char a[]

Source: Internet
Author: User

Char *a = A In "Hello" is a pointer to the first character ' a '

Char a[20] = array name A in "Hello" is also a pointer to the first character ' H ' of the array

But they are not the same :

See example: Add two strings:

Results:

Contrast:

Results:

Add the string to the pointer to the word string, there is a segment error, the essential Reason: *d= "0123456789" stored in the constant area, can not be repaired. The array is stored in the stack and can be modified. The difference between the two is as follows:

I. "read" "Write" ability char *a = "ABCD"; At this point "ABCD" is stored in the constant area. You can only access a string constant through a pointer, not change it. and char a[20] = "ABCD", at which time "ABCD" is deposited on the stack. You can access and modify the contents of the array through pointers.

Two. Assign value moment char *a = "ABCD"; is determined at compile time (because it is a constant). While char a[20] = "ABCD", determined at run time

three. Access efficiency char *a = "ABCD"; stored in a static storage area. The array on the stack is faster than the string pointed to by the pointer. So slow and char a[20] = "ABCD"; stored on the stack. Fast

Also note:

Char a[] = "01234", although there is no indication of the length of the string, the system is already open, that is, the size is 6-----' 0 ' 1 ' 2 ' 3 ' 4 ' ' 5 ', ( Note that strlen (a) is not counted '")

Look at the same problem that arises in a structure :

So the red section appears "Segment Default" when calling the Init function, because the pointer n is static, and only the ability to "read" can not be changed.

Memory allocation method
There are three types of memory allocations: static storage, heap area, and stack area. Their functions are different, and they use them in different ways. Static Storage : The existing program is already allocated when it is compiled, which exists throughout the running of the program. It mainly holds static data, global data and constants. Stack Area : When the function is executed, the storage units of the local variables within the function (including the main function) can be created on the stack, and the storage units are automatically freed at the end of the function execution. Stack memory allocation operations are placed within the processor's instruction set, which is highly efficient, but allocates a limited amount of memory. (any variable is in the station, such as int a[] = {1, 2}, and variable A is in the stack area.) The contents of the array also exist in the stack area. Heap Area : Also known as dynamic memory allocation. The program uses malloc or new to request memory of any size at run time, and the programmer is responsible for releasing the memory with free or delete at the appropriate time. The lifetime of dynamic memory can be determined by us, and if we do not release the memory, the program will release the dynamic memory at the end. However, good programming habits are: If a dynamic memory is no longer in use, you need to release it, and immediately set the pointer to NULL to prevent the creation of wild pointers.

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.