A deep understanding of the difference between Char *a and Char a[] _c language

Source: Internet
Author: User

Objective

There are some essential differences between pointers and arrays. Of course, in some cases, such as when the array is passed as a function parameter, since the array is automatically degraded to the same type of pointer, the pointer passed in as a function parameter does have a certain consistency within the function, but this is only a special case, in essence, the two are different.

Let's take a look at the detailed introduction.

char *a = "hello" A is a pointer to the first character ' a '

char a[20] = "hello" Array name A 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" and "write" ability

At char *a = "abcd";  this point "ABCD" is stored in the constant area. You can only access a string constant through a pointer, not change it.

At char a[20] = "abcd"; this point "ABCD" is stored on the stack. You can access and modify the contents of the array through pointers.

Two. Time to assign value

char *a = "abcd"; is determined at compile time (because it is a constant).

and char a[20] = "abcd"; at run time to determine

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"; on the stack. Fast

Also note:

char a[] = "01234",Although there is no indication of the length of the string, the system is now open, the size is 6-----' 0 ' 1 ' 2 ' 3 ' 4 ' ' 5 ' ' + ' ', (note ' strlen(a) is not counted ')

Look at the same problem that arises in a structure:

So the red section Init appears "Segment Default" when the function is called, because at this point the pointer n is static, 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.

1, static storage area: Within the existence of the program compile time has been allocated, this block exists throughout the operation of the program. It mainly holds static data, global data and constants.

2, Stack area: In the function, the function (including the main function) of local variables in the storage unit can be created on the stack, the function at the end of these storage units are automatically released. 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. )

3, 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.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.