C Language---Pointers

Source: Internet
Author: User
Tags byte sizes function definition

There are two ways to access variables in the C language:

1) direct access, through variable names;

2) indirect access, through the address to access a variable, the address of the variable is the content of another variable.

In fact, after the program has been compiled, access to the variable will become an access from the address.

printf ("%d \ n", i);

scanf ("%d \ n", &i); The scanf statement uses &i to access

The address of a variable is called a pointer to the variable, and the address is called a pointer to the image, and if a variable is specifically used to hold the address of another variable, it is called

"Pointer variable", the value of the pointer variable---is the address of another variable, the address of the pointer variable---is the address of its own variable.

To define a pointer variable:

Type name * pointer variable name;

int *point_1;

int *point_2 = &b; When defined, initializes the

The "*" before the pointer variable name indicates that the variable is a pointer variable, the value of point_1 represents the address of another variable, and *point_1 represents the value of another variable

&point_1 represents the address of the pointer variable

When you define a pointer variable, you must make a base type, because accessing a variable requires not only knowing the first address, but also knowing the number of bytes accessed.

& Fetch address operator; * pointer operator

When the pointer is assigned a value on another variable when the pointer operator is called, the value of the pointer must first be initialized, otherwise the pointer to the address is unknown and error prone.

int * TEMP;

*temp = *P1; Easy to generate errors

When a pointer variable is used as a function parameter, it means that the address of one variable is passed to another variable.

void swap (int *ptr1, int *ptr2) {

int *temp;

temp = *PTR1; The input pointer interacts with its contents, and temp can be defined as a normal int variable

*PTR1 = *PTR2;

*PTR2 = temp;

}

void swap (int *ptr1, int *ptr2) {

int *temp;

temp = PTR1; The input pointers point to the address exchange respectively

PTR1 = PTR2;

PTR2 = temp;

}

When an array name is passed as an argument to a function, it is also a pass that represents the pointer.

void Main () {

Fun (array,10);

}

void fun (int arr[], int n) {

}

An argument is represented by a pointer, an array, and a formal parameter is equivalent to an array or pointer representation.

The array is referenced by a pointer. The array itself is also a special kind of pointer:

int a[10];

int *ptr;

PTR = &a[0]; The address of the array a[10] in C, which can be accessed using &a[0] or a

ptr = A; Equivalent to &a[0]

References to array elements:

Subscript: A[i];

Pointer: * (A+I)

* (p++), indicating that the value of *p is first taken, and then P is added 1;

* (++P), indicating that p is self-adding 1, and then take the value of *p;

In the C language, strings are stored in character arrays, and there are two ways to represent them:

1) storing a string in a character array, referencing a character by an array subscript,%s+ the entire string with the array name, and outputting a character with the%c+ array subscript

Char string[] = "I am a big man"

printf ("%s\n", string);

printf ("%c\n", string[4]); The last character holds "\"

2) Use pointer variables to point to a string constant

Char *string = "I am a big man"

equivalent to Char *string; String = "I am a big man"//access to the character pointer does not need to be "*"

Pointer to the function, when the system compiles, the compilation system allocates a piece of storage space for the child function code, the starting address of the storage space is called the function pointer.

Int (*p) (Int,int) represents a function that points to a return value of int and has two int parameters.

So a call to a function can be called directly using the function name, or it can be called through a pointer to the functions.

int main () {

int max (int, int); function declaration

Int (*p) (int, int); function Pointer Declaration

int A, B;

p = max; Make P point to the Max function

c = (*p) (A, b); Calling a function from a function pointer

}

function definition for return pointer: type name * Function name (parameter list)

Float *search (float *point, int b)//return value is float * Type pointer

Array of pointers: an array in which each value is a pointer variable, type name * array name [array length]

int *p[4]; [] priority is higher than *, so first form p[4] form, after adding *p

int (*p) [4]; Represents a variable that points to a one-dimensional array

Dynamically allocating memory space,

void *malloc (unsigned int size); When assigning a continuous space of size bytes, it is best to cast the pointer type when assigning a value.

void *calloc (unsigned n, unsigned int size); allocating n contiguous spaces of size byte sizes, it is best to force type conversions when assigning a value.

void *free (void *p); Releases the allocated dynamic space that the pointer p points to.

void *realloc (void *p, unsigned int size); The assigned address and re-size it. Change the address pointed to p to size.

When you assign a pointer of type void to a pointer variable of a different base type, the user is not required to force the type conversion, and the compilation system automatically transforms.

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.