当前位置:网站首页>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
边栏推荐
- Record the bug of unity 2020.3.31f1 once
- DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
- Common sense of cloud server security settings
- Binary tree problem solving (1)
- Major domestic quantitative trading platforms
- FAQ | FAQ for building applications for large screen devices
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- One click generation and conversion of markdown directory to word format
- June book news | 9 new books are listed, with a strong lineup and eyes closed!
- Mysql表insert中文变?号的问题解决办法
猜你喜欢

CorelDRAW Graphics Suite2022免费图形设计软件

Pytorch---使用Pytorch进行图像定位

Pytoch --- use pytoch for image positioning

DC-1靶场搭建及渗透实战详细过程(DC靶场系列)

Let genuine SMS pressure measurement open source code

Record the bug of unity 2020.3.31f1 once

LeetCode-对链表进行插入排序

Realize the function of data uploading

Several methods of capturing packets under CS framework

Vmware安装win10报错:operating system not found
随机推荐
Markdown编辑语法
Read "the way to clean code" - function names should express their behavior
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Mysql中常见的锁
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
Wechat applet pull-down loading more waterfall flow loading
二叉树解题(一)
oracle 存储过程与job任务设置
Wechat applet calculates the distance between the two places
C language practice - number guessing game
LeetCode-对链表进行插入排序
Let genuine SMS pressure measurement open source code
Use a mask to restrict the input of the qlineedit control
office_ Delete the last page of word (the seemingly blank page)
Play with concurrency: what's the use of interruptedexception?
IDEA xml中sql没提示,且方言设置没用。
Arbre binaire pour résoudre le problème (2)
win11安装pytorch-gpu遇到的坑
Major domestic quantitative trading platforms