In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "what is the role of arrays and pointers in C language", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the role of arrays and pointers in C language" can help you solve your doubts.
Pointer
I believe you are no stranger to the following code:
Int iTunes 2; int * p; paired iTunes
This is the simplest pointer application and the most basic usage. Let's familiarize ourselves with what a pointer is: first, a pointer is a variable that does not hold ordinary data, but the address of the variable. As in the code above, the pointer p holds the address information of the integer variable I.
Next, let's look at how to define a pointer. Since the pointer is also a variable, its definition is the same as that of other variables: for example, int p; is an indirect addressing or indirect reference operator. In the above example, we also saw a special operator &, which is an address operator (in other appropriate cases & also a bitwise operator, & & is the intersection set operator).
In the pointer definition above, we see that what is defined is an integer pointer. Does the pointer still have a type? The answer is yes, pointers can only point to a particular type of object, that is, each pointer must point to a specific data type (an exception to * *: a pointer to a void type can hold a pointer to any type, but it cannot indirectly refer to itself. ). For example, a pointer of type int should never point to a variable of type char.
Let's give a complete example to illustrate the simple application of pointers:
# include void main () {int arecrogy brecrium page3; pamphlet 1; baccalaure3; pamphlet 1; baccalaure1; printf * (page1); printf ("% d% d / n", aPhoneBJE3);}
The running result is: 1 2-858993460 4
This is a complete example, you can debug it on your own machine. Now many people use Microsoft's Visual Studio development environment, but some people don't know how to write and debug C programs in this development environment. Please refer to the appendix for details.
In the above example, we see two expressions, broomproom1; and c = (pair1); the former means that the content in the address referred to by p is added to b by 1, which is equivalent to b=a+1 The latter is to add 1 to the address referred to by p and then assign the address referred to by p to c, of course we don't know what's in the next address of p, so we output a random value (it's dangerous to do this, remember not to use uncertain memory addresses).
Array
The array should be familiar to everyone and has a wide range of uses.
Int a [4] = {2pm 4pm 5pm 9}
This statement defines a 4-space integer array an and initializes it.
The basic knowledge of arrays can be referred to other corresponding textbooks. Here we mainly discuss the combined application of pointers and arrays.
Let's look at a complete example:
# include void main () {int a [4] = {2 int a [4]); int * p; pyoga; * pendant pauper; printf ("% d% d% dBand", * precince6 * (pendant 1));}
Running result: 4 10 5
Parse: the statement pdeclare; indicates that the address of the 0th element of the array an is assigned to the pointer p, and the array name a represents the address of the 0th element of the array A.
A [I] represents the I element of the array a, and if a pointer p is defined, then the statement ppointer p [0] means that the pointer p can be pointed to the 0th element of the array a, that is, the value of p is the address of the array element a [0]. So (palle1) refers to the contents of the array element a [1], pallei is the address of the array element a [I], and (pallei) refers to the contents of the array element a [I]. A reference to the array element a [I] can also be written as (axii). It can be concluded that & a [I] has the same meaning as ahumi, and p [I] is also equivalent to (pallei).
Although arrays and pointers have so much in common, we must keep in mind that there is a difference between array names and pointers. The pointer is a variable, so the statements paira and pendant + are legal. However, the array name is not a variable, so statements in the form of astatp and axiom + are illegal.
Let's take a look at a commonly used function strlen (char * s):
Int strlen (char * s) {int n; for (return n;})
Because s is a pointer, it is legal to perform self-increment operations on it. Performing the slots + operation does not affect the string in the caller of the strlen function, which only increments the private copy of the pointer in the strlen function. In the function definition, the formal parameters char s [] and char * s are equivalent.
Let's look at the address arithmetic operation again: if p is a pointer to an element in the array, pause + will increment p and point to the next element, while pause _ increment will increment p by I so that it points to the I element after the element that pointer p currently points to. Like other types of variables, pointers can be initialized. In general, the initialization value that is meaningful to the pointer can only be 0 or an expression that represents an address, for which the address expressed by the expression must be an address with the appropriate type of data previously defined. It makes sense for any pointer to compare with 0 equally or unequally. But arithmetic or comparison operations between pointers to elements of different arrays do not make sense. Pointers can also be added or subtracted from integers. For example, pairn represents the address of the nth object after the object that the pointer p currently points to. This conclusion holds no matter what type of object the pointer p points to. When calculating pairn, n scales proportionally according to the length of the object pointed to by p, while the length of the object pointed to by p depends on the declaration of p. For example, if the int type takes up 4 bytes of storage, the corresponding n in the calculation of the int type will be calculated as a multiple of 4.
Pointer subtraction also makes sense if p and Q point to elements in the same array, and p
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.