当前位置:网站首页>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
边栏推荐
- How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
- oracle 存储过程与job任务设置
- Feature Engineering: summary of common feature transformation methods
- 云服务器的安全设置常识
- A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
- Binary tree problem solving (2)
- Sword finger offer II 006 Sort the sum of two numbers in the array
- Typescript practice for SAP ui5
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- [C language] Dynamic Planning --- from entry to standing up
猜你喜欢
Idea autoguide package and autodelete package Settings
How to write a client-side technical solution
Markdown edit syntax
Federal learning: dividing non IID samples according to Dirichlet distribution
Spring moves are coming. Watch the gods fight
Common sense of cloud server security settings
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
10 minutes to understand CMS garbage collector in JVM
Realize the function of data uploading
How much is the tuition fee of SCM training class? How long is the study time?
随机推荐
Idea automatic package import and automatic package deletion settings
C language practice - number guessing game
Spring moves are coming. Watch the gods fight
Social media search engine optimization and its importance
Learn AI safety monitoring project from zero [attach detailed code]
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
Arbre binaire pour résoudre le problème (2)
Introduction to vmware workstation and vSphere
MySQL table insert Chinese change? Solution to the problem of No
Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
Realize the function of data uploading
Mysql表insert中文变?号的问题解决办法
Mysql中常见的锁
Federal learning: dividing non IID samples according to Dirichlet distribution
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Websites that it people often visit
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
cookie、session、tooken
Wechat applet calculates the distance between the two places
Common locks in MySQL