An array of C language cores and pointers

Source: Internet
Author: User

Pointer

I believe you are not unfamiliar with the following code:

int i=2;
int *p;
p=&i;
This is the simplest pointer application and is the most basic usage. Let's get acquainted. What is a pointer: First the pointer is a variable, it holds not the usual data, but the address of the variable. As in the code above, the address information of the integer variable i is saved in pointer p.

Next look at how to define a pointer, since the pointer is also a variable, then its definition as well as other variables defined as: int p; is an indirection or indirect reference operator. In the example above we also see a special operator, &, which is a fetch-address operator (in other appropriate cases & also the bitwise operator,&& as the intersection operator).

In the pointer definition above, we see the definition of an integer pointer, does the pointer have a type? The answer is yes, pointers can only point to objects of a particular type, that is, each pointer must point to a specific data type (the unique exception: a pointer to type void can hold pointers to any type, but it cannot indirectly refer to itself.) )。 For example, a pointer of type int must never point to a variable of type char.

Here we give a complete example of the simple application of pointers:

#include <stdio.h>
void Main ()
{
int a,b,c,*p;
A=1;
b=3;
p=&a;
b=*p+1;
c=* (p+1);
printf ("%d%d%d%d/n", a,b,c,*p+3);
}
Operation results are: 1 2-858993460 4

This is a complete example, you can debug on your own machine, and now many people are using the visual Studio development environment of Microsoft, some people do not know how to write C program in the development environment and debugging C program, the specific situation can refer to the appendix.

In the example above, we see such two expressions b=p+1, and c= (p+1), the former means that the contents of the address referred to in P plus 1 are assigned to B, which is equivalent to b=a+1, the latter is the address referred to by P plus 1 and the address (p+1) is assigned to C, Of course we don't know what's in the next address of P, so we're outputting a random value (it's dangerous to do this, remember not to use an indeterminate memory address).

Array

The array should all be familiar and very versatile.

int a[4]={2,4,5,9};
This statement defines a 4 space-sized integer array A and initializes it.

The basics of arrays can refer to other corresponding textbooks, which we mainly discuss in conjunction with pointers and arrays.

Let's look at a complete example:

#include <stdio.h>
void Main ()
{
int a[4]={2,4,5,9};
int *p;
P=a;
*p=*p++;
printf ("%d%d%d/n", *p,*p+6,* (p+1));
}
Run Result: 4 10 5

Analysis: the statement p=a; the address of the No. 0 element of array A is assigned to the pointer p, and the array name a represents the address of the No. 0 element of array A.

A[i] Represents the first element of array A, if a pointer p is defined, then the statement p=&a[0]; indicates that the pointer P can point to the No. 0 element of array A, that is, the value of P is the address of the group element A[0]. Then (p+1) refers to the contents of the array element a[1], P+i is the address of the array element A[i], (p+i) refers to the contents of the array element A[i]. A reference to an array element A[i] can also be written as (A+i). It can be concluded that &a[i] and a+i have the same meaning, P[i] and (p+i) are also equivalent.

Although arrays and pointers have so many common places, we must remember that there is a difference between the array name and the pointer. The pointer is a variable, so statements p=a and p++ are legal. But the array name is not a variable, so statements like a=p and a++ are illegal.

Let's look at a common function strlen (char *s):

int strlen (char *s)
{
int n;
for (n=0;*s!= '/0 '; s++)
n++;
return n;
}
Because S is a pointer, it is legal to perform a self-increment operation on it. Performing a s++ operation does not affect the string in the caller of the strlen function, it only performs a self-increment operation on the private copy of the pointer in the Strlen function. In the function definition, the formal parameter char s[] and char *s are equivalent.

Let's take a look at the address arithmetic operation: If P is a pointer to an element in the array, then p++ will perform a self-increment operation on p and point to the next element, and P+=i will increment the p by a plus I to point to the I element after the current point of the pointer p. As with other types of variables, pointers can also be initialized. In general, the initialization value that is meaningful to a pointer can only be 0 or an expression that represents an address, and for the latter, the expression must be an address that has the appropriate type of data defined earlier. Any pointer that is equal to or unequal to 0 has meaning. However, there is no point in arithmetic or comparison operations between pointers to elements of different arrays. Pointers can also be added or subtracted from integers. An p+n that represents the address of the Nth object after the object P is currently pointing to. This conclusion is true regardless of the type of object that the pointer p points to. When P+n is calculated, N is scaled proportionally to the length of the object to which P points, and the length of the object to which P points is determined by the Declaration of P. For example, if the int type occupies 4 bytes of storage space, the corresponding n in the calculation of the int type is calculated as a multiple of 4.

The subtraction of pointers is also meaningful if p and Q point to elements in the same array, and p<q, then q-p+1 is the number of elements between the elements that the P and Q point to. Let's take a look at another version of strlen (char *s):

int strlen (char *s)
{
Char *p=s;
while (*p!= '/0 ')
p++;
return p-s;
}
Program, p is initialized to point to S, which points to the first character of the string, while a while loop statement examines each character in the string in turn until it encounters the character '/0 ' at the end of the identity character array. Since P is a pointer to a character, each execution with this p++,p will point to the address of the next character, and P-s indicates the number of characters that have been checked, that is, the length of the string.

Summary: Valid pointer operations include assignment operations between pointers and integers, subtraction or comparison between two pointers to elements in the same array, assigning a pointer to 0, or a comparison between a pointer and 0. All other forms of pointer arithmetic are illegal.

Let's look at two more statements:

Char a[]= "I am a Boy"; Char *p= "I am a Boy";
A is a one-dimensional array that is just enough to hold the initialization string and the null character '/0 '. A single character in an array can be modified, but a always points to the same storage location. P is a pointer to a string constant, which can then be modified to point to a different address, but if you try to modify the contents of the string, the result is undefined.

To make it easier to understand the relationship between arrays and pointers, let's look at a function:

void strcpy (char *s,char *t)
{
int i;
i=0;
while ((S[i]=t[i])! = '/0 ')
i++;
}
Because parameters are passed by value, the arguments s and T can be used in any way in the strcpy function.

Here are a few versions of the pointer implementation:

void strcpy (char *s,char *t)
{
while ((*s=*t)! = '/0 ') {
s++;
t++;
}
}
Minimal version:

void strcpy (char *s,char *t)
{
while (*s++=*t++)
;
}
Here, the self-increment operation of S and T is placed in the test section of the loop. The value of the expression *t++ is the character that T points to before the self-increment operation is performed. The suffix operator + + indicates that the value of T is not changed until the character is read. Similarly, before S performs a self-increment operation, the character is stored in the old position pointed to by the pointer s. Nanjing Lou Feng The comparison of the expression with '/0 ' in the above version is superfluous, because it is only necessary to determine whether the value of the expression is 0.

Pointer arrays and pointers to pointers

These two words sound very novel, in the End what meaning?

Because the pointers themselves are variables, they can also be stored in arrays like other variables. This is easy to understand.

#include <stdio.h>
#include <string.h>
void Main ()
{
int i;
Char b[]={"wustrive_2008"};
Char *a[1];
*a=b;
For (I=0;i<strlen (b); i++)
printf ("%c", * (A[0]+i));
printf ("/n");
}
Running Result: wustrive_2008

Here the library function is strlen,strlen as a standard library function of the string class, so include the # include.

Let's write a strlen function for ourselves, and we'll take the example above as follows:

#include <stdio.h>
int strlen (char *s)
{
Char *p=s;
while (*p!= '/0 ')
p++;
return p-s;
}
void Main ()
{
int i;
Char b[]={"wustrive_2008"};
Char *a[1];
*a=b;
For (I=0;i<strlen (b); i++)
printf ("%c", * (A[0]+i));
printf ("/n");
}
The result is the same as the previous example, the difference is that we have implemented the Strlen function, we re-programming using the library function, are the language of the developer or the system for us to write the function, in fact, we can also write their own.

This example demonstrates the use of an array of pointers, and the value of the pointer array a a[1] is a pointer to the first character of the character array.

Pointers are also well understood, that is, one pointer puts the address of another pointer, and the other pointer may point to the address of a variable, and may also point to another pointer.

Pointers and multidimensional arrays

See two definition statements: int a[5][10]; int *b[5];

Syntactically speaking, a[3][4] and b[3][4] are legitimate references to an int object. Nanjing Lou Feng Www.jsgren.com/forum-42-1.html But A is a true two-dimensional array that allocates 50 bytes of storage space of type int. But the B definition allocates only 5 pointers, and without initialization, they must be initialized for display, assuming that each element of B points to an array of 10 elements, then the compiler assigns it 50 storage spaces of type int length and 5 pointer storage space. An important advantage of an array of pointers is that each line of the array can be of different lengths, that is, each element of B does not have to point to a vector with 10 elements.

Pointers to functions:

In the C language, a function is not a variable, but a pointer to a function can be defined. Pointers of this type can be assigned values, stored in arrays, passed to functions, and returned as functions.

If the following statement is an argument to a function, what does that mean:

Int (p) (void, void *)
It indicates that P is a pointer to a function that has a parameter of two void type and whose return value type is int. Statement if ((p) (V[i],v[left]) <0), the use of P is consistent with its declaration, p is a pointer to a function, and P represents a function. If it is written like this: int p (void, void) indicates that P is a function that returns a pointer of type int.

Let's look at two statements:

int *f (); F is a function that returns a pointer to type int
Int (*PF) (); PF is a pointer to a function that returns an object of type int.

An array of C language cores and pointers

Related Article

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.