C language Pointer {learning NOTE}

Source: Internet
Author: User

The way of accessing variable value by variable address is called direct access mode;

Define a special variable to hold the address, the value of I_pointer is the starting address of the unit that the variable I occupies, and access the value of the variable i. Find the variable that holds the address of I, take out the address, and then go to the address to fetch the value;

i_pointer=&i;

The address of a variable is called a pointer to the variable;

If there is a variable that is specifically used to hold the address (pointer) of another variable, it is called a pointer variable;

The difference: The pointer is an address, and the pointer variable is the variable that holds the address;

Define pointer variables: such as: int *pointer_1; An int is a base type that must be established when a pointer variable is defined, and the base type of a pointer variable is used to specify the type of variable that the pointer variable can point to;

The general form of defining pointer variables is: base type * pointer variable name;

The parameter of a function can also be a pointer type, transferring the address of one variable to another function;

Compare variable names and array names as function parameters:

1. If the type of the parameter is a variable name, the value of the variable is passed, and the value of the argument cannot be changed by a function call

2. If the type of the parameter is an array name or pointer variable, the address of the first element of the real parameter group is passed, and the value of the argument can be changed by the function call;

Pointer variables do arguments, you must first make the pointer variable has a definite value, pointing to a defined unit;

When using the array-famous parameter, if the value of each element in the parameter group changes, the value of the element of the real parameter group changes;

If the argument is an array name (that is, the address representing the first element of the array), the parameter needs to define the pointer variable (only the pointer variable can hold the address), which is commonly used to change the value of the real parameter group by invoking a function;

The real parameter group name represents a fixed address, or pointer constant, but the shape parameter group is not a fixed address value, but as a pointer variable, at the beginning of a function call, its value equals the address of the first element of the real parameter group, it can be assigned again during function execution;

Induction:

If there is a real parameter group, in order to change the value of the elements in this array in the function, there are 4 situations in which the actual participating formal parameters correspond to the following relationships:

A, formal parameter, and argument are all used with the array name

Array names do formal parameters and arguments;

#include <studio.h>

void Main ()

{

void Inv (int x[],int n);

int i,a[10]={3,7,9,11,0,6,7,5,4,2};

printf ("The original array:\n");

for (int i = 0; i < 10;i++)

{

/* Code */printf ("%d\n", A[i]);

printf ("\ n");

}

INV (a,10);

printf ("The array has been inverted:\n");

for (int i = 0; I <10; ++i)

{

/* Code */printf ("%d\n", A[i]);

printf ("\ n");

}

}

void Inv (int x[],int N)

{

int temp,i,j,m= (n-1)/2;

for (int i = 0; I <= m; i++)

{

/* Code */j=n-1-i;

Temp=x[i];

X[I]=X[J];

X[j]=temp;

}

Return

}

B, argument with array name, parameter with pointer variable

Pointer variables do formal parameters, array famous arguments;

#include <studio.h>

void Main ()

{

void Inv (int *x,int n);

int i,a[10]={3,7,9,11,0,6,7,5,4,2};

printf ("The original arrag:\n");

for (int i = 0; i <; i++)

{

/* Code */printf ("%d\n", A[i]);

printf ("\ n");

}

INV (a,10);

printf ("The array has been inverted:\n");

for (int i = 0; i <; i++)

{

/* Code */printf ("%d\n", A[i]);

printf ("\ n");

}

}

void Inv (int *x,int N)

{

int *p,temp,*i,*j,m= (n-1)/2;

I=x;j=x+n-1;p=x+m;

for (; I <=p; i++,j--)

{

/* Code */temp=*i;

*i=*j;

*j=temp;

}

return;

}

C, argument parameters all use pointer variables

Pointer variables do arguments and formal parameters

#include <studio.h>

void Main ()

{

void Inv (int *x,int n);

int i,arr[10];

int *p=arr;

printf ("The original array:\n");

for (int i = 0; i < 10;p++)

{

scanf ("%d", p);

printf ("\ n");

}

P=arr;

INV (p,10);

printf ("The array has been inverted:\n");

for (int p=arr; p<arr+10; p++)

{

printf ("%d\n", *p);

printf ("\ n");

}

}

void Inv (int *x,int N)

{

int *p,temp,*i,*j,m= (n-1)/2;

I=x;j=x+n-1;p=x+m;

for (; I <= p; i++;j--)

{

/* Code */j=n-1-i;

Temp=*i;

*i=*j;

*j=temp;

}

Return

}

d, argument is pointer variable, parameter is array name

C language Pointer {learning NOTE}

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.