C pointer and address pointer and Function

Source: Internet
Author: User

I. pointer and address description:
1. The address operator can only be applied to objects in memory, that is, variables and array elements. It cannot act on expressions, constants, or register-type variables.
2. The unary operator * is an indirect addressing or indirect reference operator. When used as a pointer, the object to which the access Pointer Points
3. pointers can only target certain types of objects. One exception is that a pointer to the void type can store a pointer to any type, but it cannot indirectly reference itself.

Ii. pointers and functions
C language passes the parameter value to the called function by passing the value. Therefore, the called function cannot directly modify the variable value in the main function.
However, you can use pointers to indirectly access the operations they direct.

[Html]
# Include <stdio. h>
 
Void swap (int * x, int * y );
 
Int main ()
{
Int x = 1;
Int y = 2;
Swap (& x, & y );
Printf ("x = % d y = % d \ n", x, y );
}
 
 
Void swap (int * x, int * y)
{
Int temp;
Temp = * x;
* X = * y;
* Y = temp;
}

The output is as follows:
[Html]
Pateo @ pateo-B86N53X :~ /Work/study $ cc main. c-o main
Pateo @ pateo-B86N53X :~ /Work/study $./main
X = 2 y = 1


For ease of understanding, I modified the following example, learning is improving in the attempt, OK?
[Html]
# Include <stdio. h>
 
Void swap (int * x, int * y );
 
Int main ()
{
Int m = 1;
Int n = 2;
Swap (& m, & n );
Printf ("m = % d n = % d \ n", m, n );
}
 
 
Void swap (int * x, int * y)
{
Int temp;
Temp = * x;
Printf ("temp = % d \ n", temp );
* X = * y;
* Y = temp;
}

Output:
[Html]
Pateo @ pateo-B86N53X :~ /Work/study $ cc main. c-o main
Pateo @ pateo-B86N53X :~ /Work/study $./main
Temp = 1
M = 2 n = 1


At this point, I have realized that the pointer parameters of the function are different from those of the java function parameters. Below I will analyze the above Code as follows:
[Html]
Int * x;
Int * y;
 
X = & m;
Y = & n
 
Transport substitution in Functions
 
Temp = * (& m)
 
* (& M) = * (& n)
 
* (& N) = temp
 
It should be better understood from above


 


Related Article

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.