Learn about Pointers (3)--pointers and arrays

Source: Internet
Author: User

1.
In general, the array name represents the array itself, but if the array name is considered a pointer, it points to the address of the first element of the array.
Example 1:
int array[10]={0,1,2,3,4,5,6,7,8,9};
int value;
VALUE=ARRAY[0]; Can also be written as: Value=*array;
VALUE=ARRAY[1]; Can also be written as: value=* (array+1);
VALUE=ARRAY[2]; Can also be written as: value=* (array+2);

Example 2:
In a 32-bit system,
sizeof (int (*) [10])//value: 4
sizeof (INT[10])//value: 40
sizeof (PTR)//value: 4
Note: sizeof (object) measured the size of the type of the object itself.

2.
The special relationships of pointers and arrays can be extended to C-style strings. The string is equivalent to an array, stored as an array in memory, except that the string is an array constant, the content is immutable, and can only be an rvalue. If you look at a pointer, he is a constant pointer and a pointer constant.
Example 3:
Char *str[3]={
"Hello,thisisasample!",
"Hi,goodmorning.",
"Helloworld"
};
Char s[80];
strcpy (S,str[0]); Can also be written as strcpy (S,*STR);
strcpy (s,str[1]); Can also be written as strcpy (s,* (str+1));
strcpy (s,str[2]); Can also be written as strcpy (s,* (str+2));
In the above example, STR is an array of three cells, each of which is a pointer to a string. With the pointer array name STR as a pointer, it points to the No. 0 element of the array, which is of type char * *, which is the type char *.
*STR is also a pointer to the type char *, which is the type char, and the address it points to is the string "hello,thisisasample!" The address of the first character, which is the address of ' H '.

Note: the function strcpy () function is to copy a string from one location to another. strcpy () accepts 2 parameters, the first one is the destination address, and the second is the address of the string to copy.



Summary:
Declares an array of type array[n], the array name arrays has two meanings: first, it represents the entire array, its type is type[n], and second, it is a constant pointer, the type of the pointer is type*, the pointer is of type That is, the type of the array cell, the memory area that the pointer points to is the first element of the array, which occupies a separate memory area, noting that it and the memory area occupied by the first element of the group are different. The value of the pointer cannot be modified, that is, an expression like array++ is wrong. Array names in different expressions can play different roles.
In the expression sizeof (array), array name arrays represent the array itself, so the sizeof function detects the size of the entire array. In the expression *array, array acts as a pointer, so the result of this expression is the value of the first element of the array. sizeof (*array) measured the size of the array element.
Expression array+n (where n=0,1,2, ...). ), array acts as a pointer, so the result of Array+n is a pointer, which is of type *, and it points to the type, which points to the nth element of the array. So sizeof (ARRAY+N) measured the size of the pointer type. The result in a 32-bit system is 4.

Learn about Pointers (3)--pointers and arrays

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.