Pointers and arrays summary of the C language

Questions and pointers related formulas 1 :

1. The address of the variable must address, address points to who was who

And related issues pointer to draw: the contents of variables house painting, painting pointer arrow

----> mouth

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

Two special operators and associated pointer:

A "&" address operator can fetch address by common variable & operator;

Second, the "*" has two meanings:

   1. The pointer flag: whether the pointer flag to see whether the front of the main type, where there is an int

   2. pointer operator:

     In the right side of the equal sign is the value. * The value of common variable may be taken pointer variable points.

     The left side of the equal sign is assigned. * May be an ordinary variable pointer variable points values, changed to another.

        2 formulas : There * is the value of the content, not the read is to write. The left side of the equal sign is the assignment, the rest are for the value.

   3 is a multiplication operator. If and only if the left and right are variable. slightly.

Such as

int a, b = 20, c = 30, d = 40, * p; (correctly, * pointer flag, play only approximately defined herein, and does not evaluate the role assigned to see whether the primary pointer flag. Are there types front, the front has an int here)

p = & d; (right, p points to the address of d)

a = * p; (right, where * is the value of the final result value becomes a value of 40 d.)

* P = c; (. Correct, an assignment where the * value becomes the final result of the value of d is 30 C)

* P = & b; (run error, the left value for the content, the right address, inequivalent)

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

And a pointer associated equivalent expression

If the pointer variable p points to variable a, addresses will be assigned to the variable a pointer variable p.

如:int a=20, int *p=&a;

Then the following results:

A、*p <=> a

B、 p <=> &a

C、 &*p <=> &a <=> p

D、*&a <=> *p <=> a

And (p *) ++ a ++ 

     (*p)--  a--    

     ++(*p)  ++a   ++*p   

      --(*p)  --a   --*p

* It can be seen and are & reciprocal two operators

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

All pointer variables allocated in memory the same number of bytes sizeof (pointer) is always two bytes, regardless of when the pointer is defined int *, float * or double *. More detail below

int * p1; it must point after p1 variable of type int. However, the length of the pointer itself, sizeof (p1) of 2 bytes (2 * 8bit -16bit)

float * p2; then later p2 must point type float variable. However, the length of the pointer itself, sizeof (p2) of 2 bytes (2 * 8bit -16bit)

double * p3; it must point after p3 variable of type double. However, the length of the pointer itself, sizeof (p3) of 2 bytes (2 * 8bit -16bit)

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

Four examples:

Example 1.

void fun (int *x , int *y) {
  printf("%d, %d", *x, *y) ;
  *x = 3;
  *y = 4;
}

main()
{
int x = 1, y = 2
fun(&y, &x);
printf("%d, %d", x, y);
}


Results
2 1
4 3

Note main fun when you call the function, y and x intentionally written upside down.

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

Example 2. 

#include <stdio.h>
void swap(int *p1, int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}

main()
{
int a, b;
int * p1 = &a, *p2 = &b;
scanf(%d %d, p1, p2);
swap(p1, p2);
prinf("%d, %d", *p1, *p2);
}

If you enter the console   2 and 5

The output is 

5, 2

Cause: title when calling swap function, so changes to p1, p2 value content referenced in the internal swap function will affect the value outside of a and b.

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

Example 3:

#include <stdio.h>
void swap(int *p1, int *p2)
{
int *temp;
temp = p1;
p1 = p2;
p2 = temp;
}

main()
{
int a, b;
int * p1 = &a, *p2 = &b;
scanf(%d %d, p1, p2);
swap(p1, p2);
prinf("%d, %d", *p1, *p2);
}

Different and 2, swap the function pointer temp, temp = p1 such that the point temp 2, p1 = p2 such that p1 points to 5, p2 = temp 5 such that the point p2.

But the final result is still printing
2,5


The reason: While using the function main calls swap pointer in the transfer, but they are all operating in the swap function: to modify the pointer itself, without using the * operator again to modify the "content value pointer"

3 formulas: no * pointer operations for the address assignment means that the address of the change point.

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

Example 4:

#include <stdio.h>
void swap(int *p1, int *p2)
{
int *temp;
*temp = *p1;
*p1 = *p2;
*p2 = *temp;
}

main()
{
int a, b;
int * p1 = &a, *p2 = &b;
scanf(%d %d, p1, p2);
swap(p1, p2);
prinf("%d, %d", *p1, *p2);
}

2 and like. But the only difference is, temp is defined as a pointer rather than ordinary variables. It seems to be the same and the output 2, is still
2,5.

However, the actual run-time compiler will complain: illegal memory writes.

The reason is: temp as a field guide, and where there is no point booking. If the points system area, it may cause the operating system to freeze or crash.

If the temp is defined immediately give an initial value, there would be no problem.

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

Equivalent symbol pointer, array

If an array as a parameter, then the array name as a pointer variable processing.

int fun(int a[10]) <=> int fun(int *a) <=> int fun(int a[])

As can be seen, formulas 4: & reciprocal and *. * And [] is equivalent to, and & reciprocal []

 

Example 5:

int s[10], a, b;
in d[3][6];

 

* P int;
P = & A; // points to an integer P A
P & s = [2]; // P points to the second element of the array s
p = & d [2] [ 4]; //// p points d second row of the array, the elements of the fourth column.

Therefore, the above statements are correct.

 

The above examples:

1. Define an int s [10]; s is equal to & s [0] That is, to see an array, immediately I think he represents the address of the first element of the array.

    In the C language provides that the first address data representative of the array, and is a constant address

2. Further, if a pointer is defined int * ppp = s; are priced at ppp s, while equivalent to & s [0].

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

Pointer subtraction:


When the pointer points to an address with a variable array, the pointer variable to point to the next element in the array after adding 1,
the pointer variable to point before the Save 1 element in the array.

 Example 6:

 

A a float [10]; a float * P;
P = A & [. 4]; the p-3 points?

First drawing:

a[0] | a[1] | a[2] | a[3] | a[4] | ...

The initial position p points to a [4], the p-3 is a front three, points to a [1]

 5 formulas: subtraction pointer is moved forward or backward n elements

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

If the

int a[N], *p=a;

There are:

Address three are equivalent:

The presence of equivalent expression:

p + i <=> a + i <=> & a [i] represents the address of the i-th element

Equivalent to four elements:

The presence of equivalent expression:

1. * (p + i) <=> * (a + i) <=> a [i] represents the value of the i th element

2. In addition, p [i] <=> a [i]. P [i] are rare usage, without any error, the value of i-th element is represented. The reason is that p and a fully equivalent.

Sorted out the equivalent formula is as follows:

A) * (P + I) <=> * (A + I) <=> A [I] <=> P [I]
B) P P ++ ++ <=> <=> = P + P. 1 <=> P +. 1 =
C) p-<=> the P-- <=> -P = P = P <=>. 1. 1-
D) * P * <=> + (P +)
   Description: * and the operation priority ++ the same level, so in accordance with the principle of combining from right to left, the first execution ++, after performing *
   ++ p in the back, and after execution plus . I.e., the value of p is taken first pointer plus 1.
E) * p ++ <=> * (p ++)
   ++ p in front of, and then to add the first execution . I.e. first pointer by 1, and then move the value pointed to by p removed,
F) (p *) <=> ++ ++ (* p) * p ++ <=>
    the contents of the self-energizing p
g ) (* p) - <=> - (* p) <=> - * p
    contents p decrementing

 

Reproduced in: https: //www.cnblogs.com/kungfupanda/p/3748064.html

Guess you like

Origin blog.csdn.net/weixin_33753845/article/details/94493403