2415 views|5 replies

36

Posts

0

Resources
The OP
 

How to learn C language in depth [Copy link]

I have been working in the embedded industry for about 8 years. I have a lot of feelings and understanding about C language. This article will talk about the in-depth study of C language. Every time I think of the in-depth study of C language, I think of Wang Guowei's three realms of reading. "Last night, the west wind withered the green trees. I went up to the high building alone and looked at the end of the world." This is the first realm. "I will never regret that my clothes are getting looser and looser, and I am exhausted for her." This is the second realm. "I have searched for him thousands of times in the crowd, and suddenly I look back and he is there, in the dim lights." This is the third realm. In fact, there are also three realms when learning C language. The three realms of C language that I understand are: "Realm 1: Learning C language grammar. Realm 2: The formation of programming logic and programming thinking. Realm 3: How to operate memory", the above is my understanding of the three realms of C language. The first realm is the easiest to achieve, because we can understand and use the grammar of C language through books or teachers' explanations. It is not so easy for beginners to achieve the second level, because this process requires you to accumulate day after day, and you need to practice a lot to complete the process of quantitative change to qualitative change. The third level is not something that can be achieved by practicing day after day or repeating a lot. The main reason is that you need to understand the essence of C language, such as variable types to allocate memory, pointers to operate memory, and the division of memory intervals, etc. These are all part of the third level. In the third level, a pointer part can make many beginners far behind. Not to mention completing the third level. How to achieve these three levels? I personally think that the first level requires a complete C language book or a teacher to teach you. The latter may allow you to accumulate basic knowledge of C language more quickly. The second level can only be achieved by practicing more. There is no shortcut to success. If you insist on 500 lines of code every day, I believe that this level will be initially achieved in less than a year. The last level requires more personal understanding. The understanding of a knowledge point must be fully understood from the inside. There is no time limit for this part. Some people may reach the third level after reaching the second level, but some people still cannot master this part well after many years. The key to this level is to understand the internal principles of C language. The learning process is like practicing martial arts. The moves are tangible, but the way to make the moves is intangible. Learning is tangible, but the understanding of knowledge is intangible. So this article will introduce pointers from the tangible part.

To understand pointers, there will be more or less complex types, so I will first introduce how to fully understand a complex type. It is actually very simple to understand a complex type. There will be many operators in a type. They also have priorities like ordinary expressions. Their priorities are the same as the operation priorities, so I summarized the principles: starting from the variable name, according to the operator priority, step by step analysis. Let's start with the simple type and analyze it slowly:

int p;

This is a normal integer variable.

int *p;

First, we start from P and combine it with *, which means P is a pointer. Then we combine it with int, which means the type of the content pointed to by the pointer is int. So P is a pointer that returns integer data.

int p[3];

First, we start from P and combine it with [] to indicate that P is an array. Then, we combine it with int to indicate that the elements in the array are integers. Therefore, P is an array composed of integer data.

int *p[3];

First, start from P and combine it with [] because its priority is higher than *, so P is an array. Then combine it with *, indicating that the elements in the array are pointer types. Then combine it with int, indicating that the type of the content pointed to by the pointer is integer, so P is an array composed of pointers that return integer data.

int (*p)[3];

First, start from P, first combine with *, indicating that P is a pointer, then combine with [] (this step with "()" can be ignored, just to change the priority), indicating that the content pointed to by the pointer is an array, and then combine with int, indicating that the elements in the array are integers. So P is a pointer to an array composed of integer data.

int **p;

First, we start with P, and combine it with * to say that P is a pointer, and then combine it with * to say that the element pointed to by the pointer is a pointer, and then combine it with int to say that the element pointed to by the pointer is an integer data. Since secondary pointers and higher-level pointers are rarely used in complex types, we will not consider multi-level pointers for more complex types later, and only consider primary pointers at most.

int p(int);

Starting from P, it is first combined with (), indicating that P is a function, and then it is analyzed inside (), indicating that the function has an integer variable parameter, and then combined with the int outside, indicating that the return value of the function is an integer data

Int (*p)(int);

Starting from P, it is first combined with the pointer, indicating that P is a pointer, then combined with (), indicating that the pointer points to a function, then combined with the //int in (), indicating that the function has an int parameter, and then combined with the outermost int, indicating that the return type of the function is an integer, so P is a pointer to a function that has an integer parameter and returns an integer.

int *(*p(int))[3];

Starting from P, it is first combined with (), indicating that P is a function, then entering (), combined with int, indicating that the function has an integer variable parameter, and then combined with the outer *, indicating that the function returns a pointer, and then to the outermost layer, first combined with [], indicating that the returned pointer points to an array, and then combined with *, indicating that the elements in the array are pointers, and then combined with int, indicating that the content pointed to by the pointer is integer data. So P is a function that takes an integer as a parameter and returns a pointer variable pointing to an array composed of integer pointer variables.

That's about it. My task is just this much. After understanding these types, other types are also a piece of cake for us. However, we usually don't use too complex types, which will greatly reduce the readability of the program. Please use them with caution. The above types are enough for us. Finally, I hope that you who are reading this article can complete the triple realm of C language as soon as possible.

This post is from Programming Basics

Latest reply

The judgment of data types is explained very well. It is very good. I have collected it and read it. Thank you.   Details Published on 2023-11-20 18:59
 

7454

Posts

2

Resources
2
 

Thanks for sharing!

This post is from Programming Basics
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

1

Posts

0

Resources
3
 

Thanks for sharing. It's worth it.

This post is from Programming Basics

Comments

Thanks  Details Published on 2021-4-23 14:23
 
 
 

36

Posts

0

Resources
4
 
freebsder posted on 2021-4-22 17:16 Thank you for sharing!

This post is from Programming Basics
 
 
 

36

Posts

0

Resources
5
 
Wow Gaga posted on 2021-4-23 10:04 Thank you for sharing. It is worth it.

Thanks

This post is from Programming Basics
 
 
 

205

Posts

0

Resources
6
 

The judgment of data types is explained very well. It is very good. I have collected it and read it. Thank you.

This post is from Programming Basics
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list