Dark Horse Programmer-use of C language pointers

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Pointer

The pointer variable is used to store the address, taking up 8 bytes

1. Definition

Class name identifier * pointer variable name

int *p

2. Define the Post assignment first

int a = 10;

int *p;

p = &a; p==10;

Simply modify the value of P

*p=9;

or assign the value int *p = &a; at the same time as the definition

3. Role

Access to storage space based on an address value

The type surface pointer defined before the pointer can only point to data of that type

above example

----------------------------------------------------------------------------------------

A = 10; Define a pointer variable

Suppose that the address of A is FFC1 P's address is FFC5

p=&a; is to give P,P=FFC1 A's address.

A ==10 p = 10; *p find space and re-assign value

Attention to the use of pointers

    1. int *p can only point to data of type int
    2. Pointer variables can only store addresses
    3. The pointer is not initialized do not take direct access to other storage spaces

int *p;

printf ("%d", *p); The wrong wording

int *p = &a; When defining a variable, it's just a symbol. No practical meaning can be understood as (int *) p

p = 20; The function of this time is to access the storage space that the variable p points to

/*the exchange of two values can be done between the learning by pointers*/#include<stdio.h>voidSwapint*V1,int*v2); intMain () {intA2 = -; intB2 = the; Swap (&AMP;A2, &B2); printf ("a2=%d, b2=%d\n", a2, B2); return 0;}//complete the interchange of two integer variable valuesvoidSwapint*V1,int*v2) {    inttemp = *v1; *V1 = *v2; *v2 =temp;}

Pointer to pointer

int a = 10;

int *p = &a;

If you point to *p int**pp=&p; Another layer of int***ppp=&pp;.

Pointers and Arrays

You can iterate through the array by pointers.

int *p;

p = &a[0]; P points to the first element of the array

or P = A; The array name is the address of the array and the address of the first element.

Pointers and strings

1. Constant area

Storing some constant strings

2. Heap

Object

3. Stack

Store local Variables

Master:

2 ways to define a string

1> using arrays

Char name[] = "Itcast";

* Features: Characters inside the string can be modified

* Application: The contents of the string need to be changed frequently

2> using pointers

Char *name = "Itcast";

* Features: string is actually a constant string, inside the character is cannot be modified

* Usage: The contents of the string do not need to be modified, and this string is often used

Pointers to functions

//(*p) is a fixed notation, which means that the pointer variable p will definitely be pointing to the function#include<stdio.h>intSumintAintb) {    returnA +b;} intMain () {//define pointer variables to point to the SUM function//int on the left: the function that the pointer variable p points to returns data of type int//Right (int, int): The function that the pointer variable p points to has 2 parameters of type int    int(*p) (int,int); P=sum; intc = SUM (Ten,9); printf ("C is%d\n", c); return 0;}

Dark Horse Programmer-use of C language pointers

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.