C (7), i75775c

Source: Internet
Author: User

C (7), i75775c

C pointer advanced

This chapter introduces:

In the previous section 5th, we made a preliminary understanding of the C language pointer. As the soul of the C language,

The C pointer is certainly not that simple. In this section, we will further learn the pointer, such as the second-level pointer,

Pointer array, memory allocation, and const modifier pointer constants! Next, please follow the steps of the author

The mysterious side of the C pointer should be further parsed!



This section describes the road map:




Function and pointer: ① the pointer is used as the parameter of the function:




② Pointer to the function:




③ Pointer function:





④ Main functions with Parameters




Ps: run the code: compile and generate the exe file, and then go to the directory where the exe is located: input: File Name one two three

Output: one two three




Pointer array:


Two questions: how to save a string? What if more than one is saved?

First question:

① Char name [20] = "~~~ "② Char * name = "~~~ "// The former defines the length and cannot be assigned directly. Obviously, the latter is more flexible.

Second question: see Figure





Level 2 pointer:






Dynamic Memory Allocation:




Sample Code:

Dynamically allocate the space of 10 integer types. If the size is greater than this value, it is dynamically allocated. Otherwise, realloc is used to re-allocate the memory.

Ps: the code is correct. vc6 always reports a running error. If an error occurs, use the C-Free or Visual studio compiler to try it!


  1. <Span style = "font-family: Microsoft YaHei;" >#include <stdio. h>
  2. # Include <stdlib. h>
  3. # Define N 10
  4. Int main ()
  5. {
  6. Int * p = 0;
  7. Int I, num, * q = 0;
  8. P = (int *) malloc (N * sizeof (int )));
  9. If (p = 0)
  10. {
  11. Printf ("Memory Allocation Error! \ N ");
  12. Exit (0 );
  13. }
  14. Printf ("Enter the number of elements to store: \ n ");
  15. Scanf ("% d", & num );
  16. If (num <= N)
  17. {
  18. For (I = 0; I <N; I ++)
  19. Scanf ("% d", p + I );
  20. }
  21. Else
  22. {
  23. For (I = 0; I <num; I ++)
  24. Q = (int *) realloc (p, (N + N) * sizeof (int ));
  25. If (q = 0) exit (0 );
  26. For (I = 0; I <num; I ++)
  27. Scanf ("% d", q + I );
  28. P = q;
  29. }
  30. For (I = 0; I <num; I ++)
  31. Printf ("% 3d", * (p + I ));
  32. Printf ("\ n ");
  33. Free (p );
  34. Return 0;
  35. } </Span>



Use const to modify pointer Variables


We all know that using const to modify a variable of the basic data type can change the value stored in the variable from start to end.

Our program cannot be modified! However, modifying pointer variables is a little different:

For example:

Int a = 3, B = 4; const int * p = &;

If we add another code: p = & B;, an error will be reported.

If you add: a = 5; the Code does not have an error, * p = a = 5;


Analysis result:

When the const is used to modify the pointer, it indicates that the pointer is a constant pointer, and the value of the variable it points to can be modified;

However, it is wrong to modify the address pointed to by the pointer !!!

Many standard library functions in C add const before some pointer parameter types to protect parameters!


Ps: int * const p = & a; const int * const p = & a; is equivalent to the above!




Summary of this chapter:

① Functions and pointers:

1. pointer as the function parameter: The real parameter corresponding to the pointer parameter must be the variable address

2. pointer to the function: the first address of the function is called the pointer of the function. The function name can be used to locate and execute the function.

3. pointer function: the return value type is a pointer.

4. main Function with parameters: int main (int argc, char * argv []): the parameters are in sequence: the number of record parameters and the string array storing the parameter content


② Pointer Array

Comparison between two-dimensional array string storage and pointer array string Storage


③ Second-level pointer

Pointer to pointer variable


④ Dynamic memory allocation:

1. Memory Distribution chart

2. Use of four memory allocation functions provided by C: malloc (), calloc (), realloc (), and free ()


⑤ Use const to modify pointer Variables

The variable value pointing to the address that can be modified. What cannot be modified is the address pointed to by the pointer!




Reference: http://blog.csdn.net/coder_pig/article/details/37915063

Copyright Disclaimer: you are welcome to reprint it. I hope you can add the original article address while reprinting it. Thank you for your cooperation.

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.