当前位置:网站首页>Embedded-c language-8-character pointer array / large program implementation
Embedded-c language-8-character pointer array / large program implementation
2022-07-02 04:35:00 【Orange peel does not stop school】
One 、 Character pointer array
1.1. clear :[] Operator calculation steps :
1. Address first
2. Post value
char a[] = {'a', 'b'};
char *p = &a;
p[1]: Address first :p+1, And then take :*(p+1)
1.2. Pointer array ( Actual development is very common )
a) Pointer array concept : Each element in an array is a pointer ( Address ), An element can only be an address , It cannot be ordinary data
b) Syntax format defined by pointer array :
data type * Array name [ Element number / The length of the array ] = { Address list };
for example :
int a = 10, b = 20, c = 30;
int *p[3] = {&a, &b, &c};
result :p[0] = &a, p[1] = &b, p[2] = &c;
Equivalent to :*(p+0) = &a, *(p+1) = &b, *(p+2) = &c;
Value :*p[0] = 10, *p[1]=20, *p[2] = 30;
Equivalent to :**(p+0) = 10, **(p+1) = 20, **(p+2) = 30;
Element number = sizeof( Array name )/sizeof( Array name [0])
c) Application scenarios : If you need to define a large number of pointer variables in the future , Instead, it makes the code look extremely cumbersome , Use pointer array to unify , Like an array to replace a large number of variables , Now? Replace a large number of pointer variables with pointer arrays
for example :
int a = 10, b = 20, c = 30, d = 40, e = 50; ...
int *pa = &a, *pb = &b, *pc = &c, *pd = &d, *pe = &e; ..
Pointer array to optimize :
int *p = {&a, &b, &c, &d, &e ...};
1.3. Character pointer array
a) Concept : Character pointer arrays are special pointer arrays , Each element is the first address of a string
for example :
char *p[] = {"abc", "efg"}; // The first 0 The first element is a string "abc" The first address
The first 1 The first element is a string 'efg' The first address
Equivalent to
char *p1 = "abc";
char *p2 = "efg";
char *p[] = {p1, p2};
result :
p[0] = p1 = "abc"
p[1] = p2 = "efg";
b) Application scenarios : A large number of string pointer variables are required !
Two 、 Preprocessing
2.1. review C Three steps of program compilation
a) Preprocessing :gcc -E -o xxx.i xxx.c
b) Just compile, not link :gcc -c -o xxx.o xxx.i/xxx.c
c) link :gcc -o xxx xxx.o
2.2. Preprocessing instruction ( With # start , No semicolon ;) classification :
a) The header file contains instructions :#include, There are two types :
#include <stdio.h> // tell gcc Direct to /usr/include Under the table of contents look for stdio.h And the stdio.h Copy all the contents of
#include "stdio.h" // tell gcc Come first Under current directory look for stdio.h, If not found Go again /usr/include Under the table of contents look for stdio.h
ask : If the included header file is not in the current directory , Not at all /usr/include Under the table of contents , How to find the included header file ?
answer : Can pass -I Option specifies the path where the header file is located ( The core , Development commonly used ),
Compile command format :gcc -E -o xxx.i xxx.c -I /home/xxxxxxxx
b) Macro definition instruction :#define
1. Suggest : The name of the macro is capitalized , For continuation \
2. effect : Improve code portability , Make the code easy to change in the future
3. Macro definition instructions fall into two categories : Constant macros and parameter macros ( Macro functions )
3.1. Constant macros
Grammar format :#define Macro name ( value )
for example :#define PI (3.14)
effect : future gcc All macros in the program PI Replace with 3.14
3.2. Parameter macro ( Also called macro function )
a) Grammar format :#define Macro name ( Macro parameters ) ( Macro value )
Be careful : The macro parameter in the macro value should not be short of parentheses
If there are multiple macro parameters , Partition with commas
for example :#define SQUARE(x) ((x)*(x))
#define SUB(x, y) ((x) - (y))
advantage : The execution efficiency of code is higher than that of function No need to pass parameters , Copy and so on
b)gcc Replacement process :gcc The compiler first replaces macro parameters with actual values , Then replace the macro names in the code with macro values
c)#( To string instruction ) and ##( Paste command )
1.# effect : Convert the following macro parameter to a string
for example :#N replaced "N"
2.## effect : Replace the following macro parameters with the previous ones adhesion together , Finally, it is replaced as a macro value
d) Macros that the compiler has defined ( Use it directly , There is no need to #define), Actual development is very common !
Be careful : Both are underlined
__FILE__: Represents the current filename ,%s
__LINE__: Indicates the current line number ,%d
__FUNCTION__: surface Show the current function name ,%s, Equivalent to __func__
__DATE__: Date the file was created ,%s
__TIME__: File creation time ,%s
Actual development use :
printf(" There's something wrong here :%s, %s, %s, %s, %d, A wild pointer access error has occurred .\n",
__DATE__, __TIME__, __FILE__, __FUNCTION__, __LINE__);e) User predefined macros : adopt gcc Of -D Option to predefine a macro
effect : The program can pass a value to the program when compiling
Be careful : If the macro is a string , that -D The following macro values need to use \" explain
for example :gcc -DSIZE=50 -DWELCOME=\" Xiao Chen \"
The result code can be used directly SIZE and WELCOME macro , And their values are 50 and " Xiao Chen "
printf("%d %s\n", SIZE, WELCOME);
f) Conditional compilation command ( The actual development uses a lot )
#if // If , for example :#if A==1
#ifdef // If you define ....
#ifndef // If there is no definition ....
#elif // Otherwise, if
#else // otherwise
#endif // and #if,#ifdef,#ifndef Pairs using
#undef // Undefine , and #define A dead foe , for example :#define PI (3.14) #undef PI
Demonstration code actually developed :
Use conditional compilation to make a set of code support different CPU(X86,ARM,POWERPC,DSP)
//vim
A(void)
{
#if ARCH==X86
// Compile only X86 Related codes
#elif ARCH==ARM
// Compile only ARM Related codes
//...
}3、 ... and 、 Large program implementation
3.1. master : Head file guard
a) Conclusion : The coding framework of any header file written in the future must be as follows :
vim A.h add to
#ifndef __A_H // Head file guard , The macro name usually has the same name as the header file name
#define __A_H
Code area of header file
#endif // and #ifndef pairing
Header file guard function : Prevent repeated definition of header file contents
b) ask : How does the header file guard protect it ?
for example :
// There is no header guard :
vim a.h add to
int a = 250; // Defining variables
Save and exit
vim b.h add to
#include "a.h"
Save and exit
vim main.c add to
#include <stdio.h>
#include "a.h"
#include "b.h"
int main(void)
{
printf("%d\n", a);
return 0;
}Save and exit
gcc -E -o main.i main.c
vim main.i
result :


int a = 250; // Repeat the definition
int a = 250; // Repeat the definition
ask : How to solve ?
answer : Add header file guard
// Add header file guard situation :
vim a.h add to
#ifndef __A_H
#define __A_H
int a = 250; // Defining variables
#endif
Save and exit
vim b.h add to
#ifndef __B_H
#define __B_H
#include "a.h"
#endif
Save and exit
vim main.c add to
#include <stdio.h>
#include "a.h"
#include "b.h"
int main(void)
{
printf("%d\n", a);
return 0;
}Save and exit
gcc -E -o main.i main.c
vim main.i
result : There is no redefined mistake
3.2. Actually develop product code components : In the third part of
a) Code development principles : Don't use a source file , The actual product code cannot be a source file , Otherwise, the maintenance of the code is extremely cumbersome , The actual product code should be implemented by dividing a source file into multiple source files according to functions , Improved code maintainability .
b) The actual product code is divided into three parts :
1. Header file section : Responsible for the declaration of variables , Custom data type declaration and function declaration
effect : In the future, other source files only need to contain header files to access variables and functions
for example :dog.h,duck.h,chick.h
2. Source file section : Be responsible for the definition of variables , Customize the definition of data type variables , Definition of function
for example :dog.c Describe a dog ,duck.c Describe a duck ,chick.c Describe a chicken
3. Main file section : Take charge of the header file and source file , There's only... In it main function , It is responsible for calling variables and functions of other source files
for example :main.c
4. Suggest : A source file corresponds to a header file , And the source file contains its own corresponding header file
5. Suggest : If there is the same content in the header file , It is recommended to put it in a public header file after picking it , Other header files only need to contain header files of functions
vim common.h
Two eyes
vim dog.h
#include "common.h"
vim duck.h
#include "common.h"
6. Be careful :static
Case study : Realize plus , reduce , clear 0, Set up 1
analysis :
The header file :add.h sub.h set.h clear.h
Source file :add.c sub.c set.c clear.c
Master file :main.c
Step by step compilation :
gcc -E -o add.i add.c
gcc -E -o sub.i sub.c
gcc -E -o set.i set.c
gcc -E -o clear.i clear.c
gcc -E -o main.i main.c
gcc -c -o add.o add.i
gcc -c -o sub.o sub.i
gcc -c -o set.o set.i
gcc -c -o clear.o clear.i
gcc -E -o main.o main.o
gcc -o main main.o set.o clear.o sub.o add.o
./main
One step in place :
gcc -o main main.c sub.c add.c set.c clear.c
边栏推荐
- Feature Engineering: summary of common feature transformation methods
- oracle 存储过程与job任务设置
- 记录一次Unity 2020.3.31f1的bug
- 二叉樹解題(二)
- UNET deployment based on deepstream
- Markdown编辑语法
- Spring moves are coming. Watch the gods fight
- Record the bug of unity 2020.3.31f1 once
- 缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
- office_ Delete the last page of word (the seemingly blank page)
猜你喜欢

记录一次Unity 2020.3.31f1的bug

Learn what definitelytyped is through the typescript development environment of SAP ui5

Unit testing classic three questions: what, why, and how?

DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

Introduction to vmware workstation and vSphere

One step implementation of yolox helmet detection (combined with oak intelligent depth camera)

How to write a client-side technical solution

云服务器的安全设置常识

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
随机推荐
Play with concurrency: draw a thread state transition diagram
正大美欧4的主账户关注什么数据?
Vmware安装win10报错:operating system not found
Lei Jun wrote a blog when he was a programmer. It's awesome
正大留4的主账户信息汇总
Pytoch yolov5 runs bug solution from 0:
Bitmap principle code record
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
Websites that it people often visit
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
Wechat applet pull-down loading more waterfall flow loading
云服务器的安全设置常识
C - derived classes and constructors
Ognl和EL表达式以及内存马的安全研究
Leetcode merge sort linked list
How to write a client-side technical solution
Common locks in MySQL
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
BGP experiment the next day