K & R's masterpiece: Small Program Summary 7 pointer

Source: Internet
Author: User

1. Valid pointer operations include: Value assignment between pointers of the same type; addition or subtraction between pointers and integers; subtraction or comparison between two pointers pointing to elements in the same array. assign a pointer to 0 or a comparison operation between the pointer and 0.

Example 1:

# Define allocsize 10000

Static char allocbuf [allocsize];

Static char * allocp = allocbuf;

 

Char * alloc (int n)

{

If (allocbuf + allocsize-allocp> = N ){

Allocp + = N;

Return allocp-N;

}

Else

Return 0;

}

 

Void afree (char * P)

{

If (P> = allocbuf & P <allocbuf + allocsize)

Allocp = P;

}

 

Void swap (char * V [], int I, intj)

{

Char * temp;

Temp = V [I];

V [I] = V [J];

V [J] = temp;

}

2. character array

The character array ends with the null character '\ 0'. Therefore, the program can check the null character to find the end of the character array.

In array format:
Void strcpy (char * s, char * t)

{

Int I;

I = 0;

While (S [I] = T [I])! = '\ 0 ')

I ++;

}
In pointer format:

Void strcpy (char * s, char * t)

{

While (* s ++ = * t ++ )! = '\ 0 ')
;
}
3. pointer array and pointer

Char * PTR [maxlines]; // PTR is a one-dimensional array with maxlines elements. Each element is a pointer to a character type.

Char ** PTR; // pointer to the element pointer to which the Pointer Points. The element char data pointed to by the pointer

When the pointer array is output, printf ("% s \ n", * PTR ++) can be used );

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.