Introduction to C language pointers and C language pointers

Source: Internet
Author: User

Introduction to C language pointers and C language pointers

Recently, my mind was not very good, but I still tried to brush my own algorithm questions. But I watched the appearance of "Segmentation fault" many times, and my mind exploded again.

I just want to say:"I also thought it was necessary to write something. It has been two weeks since March 18, and the forgotten savior is about to come. I am writing something necessary ."-- Remembering Liu and zhen jun

C language pointer
Differences:
  • Pointer variables store the addresses of certain types of variables.
  • Common variables store a certain type of data.

Function: You can use pointer variables to access data pointing to variables.

Introduction: as shown in figure 1, we can conclude that pointer variables are basically the same as common variables.

1 # include <stdio. h> 2 # include <stdlib. h> 3 int main () {4 char * p; 5 char a = 'H'; 6 p = & a; 7 return 0; 8}Pointer variable

 

  • Each pointer corresponds to a data type.
  • The pointer stores the address pointing to the type object.
  • The & operator can be used to obtain the variable address and assign a value to the pointer variable of this type.
  • * The operator is used to access the content pointed to by the pointer address.
  • Pointers are closely related to arrays. a [3] is similar to * (a + 3 ).
Does C Support reference transfer?

Reference transfer is only available in C ++. C supports value transfer only. Therefore, the C language can only be used to modify non-function variables within the function by passing pointers. That is, swap (int & a, int & B) is wrong in C language. swap (int * a, int * B) is correct.

Value Transfer

A pointer passing parameter is essentially a value passing method, which transmits an address value. During the value transfer process, parameters in the form of the called function are processed as local variables of the called function, that is, the memory space is opened in the stack to store the values of the real parameters put in by the main function, it becomes a copy of the real parameter. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function.

Reference Transfer

Although the formal parameters of the call function are also used as local variables to open up memory space in the stack, they store the addresses of the real parameters put in by the main call function. Any operation of the called function on the form parameter is processed as indirect addressing. That is, the address stored in the stack is used to access the real variable in the main function.

Some pointer suspension Conditions

The Pointer Points to an invalid memory address, so this pointer is a hanging pointer, also called a wild pointer.

Incorrect Initialization
// Assign 352 to p3, the first is the type does not match, and if the assignment is successful (using coercion), then the address pointed to by p3 is 352, which may not be available.
int * p = 352;
 
// This code compiles correctly, puts the number 352 into the address pointed to by p3, but the value of p3 is not initialized, and the address pointed to by it is uncertain, so p3 is now a wild pointer, and the program may crash at runtime.
int * p;
* p = 352;
Function returns local variables
1 // After the pointer points to an object, when the object's life cycle has ended and the object has died, the pointer is still used to access the object, and a runtime error will occur.
2 int * retAddr () {
3 int num = 10;
4 return & num;
5}
Clear Data multiple times, access to clear data, etc.
// Empty multiple times
x = malloc (N * sizeof (int));
free (x);
y = malloc (M * sizeof (int));
free (x);

//Unauthorized access
x = malloc (N * sizeof (int));
free (x);
y = malloc (M * sizeof (int));
for (i = 0; i <M; i ++)
     y [i] = x [i] ++; 

 

Differences Between Reference transfer and pointer transfer in C ++ (further sorting)


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.