character arrays, character pointers please don't tangle

Source: Internet
Author: User

In C language character array, character pointer some of the characteristics of a little vague, sometimes only know to do this but do not know why, after some time to forget, the next time to take a laborious and expensive to find the answer, uncomfortable. OK, think about it today

Let's start with two basic concepts:

1. Declaring a character array

int a[10];
Defines an array of length 10 a. In other words, it defines a collection of 10 objects, 10 of which are stored in adjacent memory areas with names A[0], a[1] 、... If the PA declaration is

int *pa;
Indicates that it is a pointer to the Shaping object, then the assignment statement

PA = &a[0];
You can point the pointer pa to the NO. 0 element of array A, which means that the value of the PA is the address of the group element A[0]. Thus, the assignment statement

x = *pa;
The contents of the array element A[0] are copied to the variable x.

Then, based on the definition of the pointer operation, PA+1 will point to the next element, and Pa+i will point to the I element after the array element that the PA points to. The above conclusion is true regardless of the type of element in array A or the length of the array. The pointer plus 1 means pointing to the next object.

Therefore, PA = &a[0] can also be written in the following form:

PA = A;
All in all: &a[i] and a+i mean the same. Correspondingly, if the PA is a pointer, it can also be labeled in its expression ( Yes, you didn't read it right.

char A[100];char *b = Null;memset (A, 0, sizeof (a)), a[0] = ' 0 '; a[1] = ' 1 '; a[2] = ' 2 '; b = A;char test= b[1];       Now the value of test is 1.

but one thing to remember is that there is a difference between the array name and the pointer: The pointer is a variable, so in the C language, the statement pa = A and pa++ are all legal. But the array name is not a variable, so statements that resemble A = PA and a++ form are illegal.


2. About character pointers

Char *a = "Test";
Pointer a here is a variable that points to the first address of the "test" in the static constant area, and the "test" is immutable in the general state (which, of course, can be changed by some means).


With the above two points, continue below ...

1. Character array to character pointer

Sometimes you need to convert between the character array and the character pointer, and inevitably use the strcpy () function, first look at its source code:

void strcpy (char *s, char *t) {while       ((*s++ = *t++)! = ' + ')           ;}

Let's look at a super-simple example:

Char *a = Null;char b[5]= {' 1 ', ' 2 '};a = b;            OK, so the correct strcpy (a, b);   This will report a memory error.
At first, the pointer variable points to null,a in memory where there is no space allocated, and a = b when pointer variable a points to the first element address of the array, which is ' 1 '. But when strcpy, *s cannot be assigned because it does not open up memory space for it. Someone will ask, open up memory space is it, that line, I so char *a = "abc"; Now *a has the memory space! Try to find still not, because *a is now immutable, forced assignment of course error. If you have to use strcpy, then you have to re-open the memory to a.

Char *a = (char *) malloc (+);   Size yourself See the situation fixed char b[5] = {' 1 ', ' 2 '};strcpy (A, b);                    That would be fine.

The difference between the two approaches is:

When you assign a value directly, it only changes the pointer's direction, that is, a points to array B.

When using the function strcpy, there is a lot of space in memory, that is, there are now two pieces of space in memory, all {' 1 ', ' 2 '}, one point from a, and the other by B.


2. Character pointer to character array

Char *a = "123"; Char B[5];memset (b,0,sizeof (b)); b = A;                Will error strcpy (b,a);          Ok

As I said earlier, the array name is not a variable and cannot be left, obviously B = A will error.

strcpy two parameters passed to char * There is no problem, s points to the first address of B, t points to the ' 1 ' of the constant area.

The value of a single element of a character array can be changed. Here is the question of character array initialization, generally three cases:

1, the definition of the time directly with a string assignment
Char a[10]= "Hello";
Note: You cannot define and assign a value to it first .

2. Assigning characters in an array to a value
Char a[10]={' h ', ' e ', ' l ', ' l ', ' o '};

3. Using strcpy


Reference: the C programming Language

character arrays, character pointers please don't tangle

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.