C Quick Start series (7) and c Quick Start Series

Source: Internet
Author: User

C Quick Start series (7) and c Quick Start Series

C Quick Start series (7)


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!


<Span style = "font-family: Microsoft YaHei;" >#include <stdio. h> # include <stdlib. h> # define N 10int main () {int * p = 0; int I, num, * q = 0; p = (int *) malloc (N * sizeof (int); if (p = 0) {printf ("Memory Allocation Error! \ N "); exit (0) ;}printf (" Enter the number of elements to store: \ n "); scanf (" % d ", & num ); if (num <= N) {for (I = 0; I <N; I ++) scanf ("% d", p + I );} else {for (I = 0; I <num; I ++) q = (int *) realloc (p, (N + N) * sizeof (int )); if (q = 0) exit (0); for (I = 0; I <num; I ++) scanf ("% d", q + I ); p = q ;}for (I = 0; I <num; I ++) printf ("% 3d", * (p + I )); printf ("\ n"); free (p); return 0 ;}</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!





How to get started with C language

To get started, you just need to know the data type, several Syntax structures, and C language writing formats. Others mainly depend on your own thinking (that is, the algorithm), and user-defined functions.

How to get started with a C Language

Many people have no idea how to learn C language. They often ask me the same question: how to learn C language? I am a teacher and have been developing programs for many years. Like many people who have just started, the first computer language to learn is C language. After years of development, I have deeply realized how important C language is to a program designer. If you do not understand C language, it is almost ridiculous to write the underlying program, you want to write excellent and efficient programs. Why is C language so important? First, the C language syntax structure is very concise and exquisite, and the program written is also very efficient and easy to describe the algorithm. Most programmers are willing to use the C language to describe the algorithm itself, if you want to make some achievements in programming, you must learn it. Second, the C language allows you to go deep into the underlying system. Which of the operating systems you know is not written in the C language? All windows, Unix, Linux, Mac, OS/2 are inside and outside. If you do not understand C language, how can you go deep into these operating systems? Let alone write their kernel programs. Third: many new languages are derived from C, C ++, Java, C #, J #, perl... which one is not? After mastering the C language, you can master many languages. After simple learning, you can use these new languages for development, once again, the C language is an important foundation for programming. Another point: Even if you are recruiting programmers and taking tests in C language, you must master the C language if you want to join the it industry. So how to learn C language? 1: to learn how to do things well, we must first introduce several essential things for learning C language: A development environment, such as turbo C 2.0, which once occupied more than half of the development programs in the DOS era. However, in the windows era, it is difficult to use turbo C. It is difficult to edit the program and drag and drop the program. In addition, the function variable auto-sensing function is not available, and it is not convenient to query reference materials. We recommend that you use Visual C ++. Although this is a big part, once it is installed, it is very convenient to use. A learning tutorial, now the C language teaching materials such as Ox hair, but we recommend that you use "C language programming" Tan haoqiang editor of the second edition of Tsinghua University Press, this book is very suitable for beginners, And the content is also very refined. In addition, there are a lot of auxiliary learning software. After all, it is now in the Window era. Learning Software is as old as learning. I would like to recommend an integrated learning environment (C Language), which provides excellent knowledge points and routine explanations, as well as a test environment for question libraries. It is said that there are thousands of questions, there is even a windows trubo C. Beginners can practice programming without having to install other compilers. It is very suitable for beginners. There is also a "C Language Learning System" software, but it is just a question bank system. If you think the question is not enough, you can try it. 2: What is the best way to learn computer languages in Sunflower collections? Answer: Read the program. That's right. Reading a program is the fastest and best way to get started with C language. Like me, now I want to learn other languages such as J # And C #, instead of reading books one by one, but learning their routines. Of course, for beginners who have never learned any computer language, it is best to read the tutorial first. After learning each chapter, you must carefully understand all the concepts of this chapter, then let alone all the routines mentioned in this chapter, and then carefully study the program until each line understands, and then find a few programming questions, preferably similar or the same as the routine, if you try to write a program that you already understand, you can write a program that is the same as it. It is definitely not necessarily true. If you don't believe it, just give it a try, if you can't write it out, don't worry. Go back and study the routine again, think about why you can't write it out, and then write the program again and again until you get it done. Congratulations, you are getting started. 3: the highest level of writing programs at peak is to master various methods (data structures) for solving problems and methods (algorithms ). Is writing the underlying program a good programmer? Also, writing the underlying program is nothing more than mastering the hardware structure. Besides, the hardware and hardware are not the same. We need to write a driver for a chip ...... the rest is full>

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.