当前位置:网站首页>Embedded-c language-7
Embedded-c language-7
2022-07-03 17:31:00 【Orange peel does not stop school】
One 、 The pointer (2)
1.0 No data type pointer :void *
a) No data type pointer (void *) Concept : It also Is a pointer variable , Also save an address , Again Occupy 4 byte Memory space , Just the memory area it points to The data type is unknowable , Call this pointer No data type pointer
How to write it :void *
for example :void *p = &a;
b) Typeless pointer characteristics :
1. Through this pointer variable is Cannot know the data type of the data stored in the memory area pointed to
2. You cannot dereference a typeless pointer directly "*", Because you don't know the data type of the memory area pointed to , I don't know how many bytes of data I will get in the future , If you want to get memory data through a typeless pointer , Data type conversion is required ( For the readability of the code, it is recommended to use cast )
3. No type pointer void * Add and subtract a few , The actual address is plus or minus
void *p = 0;
p++; //p=1
p++; //p=2
4. Case code for all cases
for example :
int a = 0x12345678;
void *p = &a; // although p Point to a, But by p Can't get a Data type of variable
*p = 300; //gcc Compiler error , I don't know how many of my own data , I can't help but report you an error ask : How to get through a pointer a What about the data? ?
answer : The way 1:
int a = 0x12345678;
int *p = &a;
printf("%#x\n", *p);Although it can be obtained at one time 4 Bytes of data , But now I don't want to get it all at once 4 Bytes of data , Want to get one 1 Bytes of data or 2 Byte data or 4 Bytes of data , Obviously, this method cannot be done , To achieve this function , Just do the following code :
The way 2: Get through a typed pointer variable 1 byte ,2 byte
obtain 1 byte :
int a = 0x12345678;
//char *p = &a; // Implicit conversion , Code readability is not high ,gcc And a warning
char *p = (char *)&a; // Coercive transformation , Improve the readability of the code , take &a Of int Type conversion to char*
printf("%#x\n", *p++); //0x78
printf("%#x\n", *p++); //0x56
printf("%#x\n", *p++); //0x34
printf("%#x\n", *p++); //0x12obtain 2 byte :
int a = 0x12345678;
//short *p = &a; // Implicit conversion , Code readability is not high gcc And a warning
short *p = ( short *)&a; // Coercive transformation , take &a Of int Type conversion to short*
printf("%#x\n", *p++); //0x5678
printf("%#x\n", *p++); //0x1234Conclusion : Although this method meets the requirements , But if you do an implicit conversion ,gcc Always give a warning
ask : Can you remove the warning ? There is also the ability to remove pointer variables ++ Time is related to type ?
answer : Through typeless pointers void * Realization
Method 3: Through typeless pointers void * To get 1 byte ,2 byte ,4 Bytes of data
obtain 1 byte :
int a = 0x12345678;
void *p = &a; // Define a typeless pointer to a, No forced rotation is required , The key is gcc No warning
char *p1 = (char *)p; // Force the typeless pointer to char*
printf("%#x\n", *p1++); //0x78
printf("%#x\n", *p1++); //0x56
printf("%#x\n", *p1++); //0x34
printf("%#x\n", *p1++); //0x12perhaps :
printf("%#x\n", *(char *)(p+0)); //0x78, First the p Forced to char * The pointer , And then take
printf("%#x\n", *(char *)(p+1)); //0x56
printf("%#x\n", *(char *)(p+2)); //0x34
printf("%#x\n", *(char *)(p+3)); //0x12obtain 1 byte :
int a = 0x12345678;
void *p = &a; // Define a typeless pointer to a, No forced rotation is required , The key is gcc No warning
short *p1 = (short *)p; // Force the typeless pointer to char*
printf("%#x\n", *p1++); //0x5678
printf("%#x\n", *p1++); //0x1234perhaps :
printf("%#x\n", *(short *)(p+0)); //0x5678
printf("%#x\n", *(short *)(p+2)); //0x12341.1. Pointers and functions
a) The pointer is used as the formal parameter of the function
result : The function can access the memory pointed to by the operation through the pointer
Reference code : for example :
void A(int *px, int *py, int *pm, int *pn)
{ // Read view
printf("%d %d %d %d\n", *px, *py, *pm, *pn);
// Write and modify
*px = ....
*py = ...
*pm = ...
*pn = ...
}b) Pointer as the return value of the function
result : This kind of function is also called Pointer function , That is to say The return value of the function is a pointer , In the future, the function will return an address , It is related to the memory life cycle of the returned variable , It has nothing to do with the scope of use of variables
Bear in mind : Be careful not to return a wild pointer
Grammar format : Return value data type * Function name ( Formal parameter table ){ Function body statement }
for example :
// Defining pointer functions
int g_a = 250; // Define global non static variables
static int g_b = 520; // Define global static variables
int *A(void)
{
int a = 200; // Define local nonstatic variables
static int b = 100; // Define local static variables
return &a; // Returns a wild pointer , Because of the variable a Memory of disappears with the return of the function
// perhaps return &b; // Sure , Because this memory is allocated once and used for life
// perhaps return &g_a; // Sure , Because this memory is allocated once and used for life
// perhaps return &g_b; // Sure , Because this memory is allocated once and used for life
}
int main(void)
{
int *p = A();
*p = 30000; // Change the value of the variable
printf("%d\n", *p); // View the data of the memory pointed to
return 0;
}c) function , The pointer , Array
Function to access the formula of the array :
void A(int a[], int len); // among a Is the address of the array
Equivalent to
void A(int *p, int len);

Two 、C Language string related content
2.1. Review character constants : Enclose... In single quotation marks , for example :'A','B','1' etc.
The actual memory stores the corresponding integer number ,ASCII code
Place holder :%c
2.2. String definition : Consisting of a continuous set of characters , And use "" Include , also The last character must be '\0'
this '\0' Represents the end of a string , this '\0' Of ASCII Code is 0
'0' Of ASCII by 48
Be careful : Studying strings ultimately studies the characters inside one by one
for example :"abcefg\0"
2.3. String characteristics
a) Placeholder for string :%s
printf("%s\n", "abc\0"); // Directly follow the string
perhaps
printf("%s\n", The first address of the string );
b) The memory space occupied by the string is continuous , And each byte stores one character
Be careful :'\0' If the end character of the string is followed by something , Then these contents are invalid
for example :"abc\0efg\0"
printf("%s\n", "abc\0efg"); //abc
c) Multiple parallel strings will be represented by gcc Help you merge into a string
"abc""efg" Merge into "abcefg"
printf("abc""efg\n"); //abcefg
2.4. Strings and pointers
a) Define a character pointer variable and point to a string , Essentially, it points to the first address of this string , That is, it points to the... In the string 0 First address of characters ( That's the character a The first address )
Define and initialize the string pointer variable form :
char *p = "abcefg";
b) If you let a character pointer variable point to a string , This string does not need to be followed by '\0',gcc I will help you add '\0'
d) Bear in mind : You cannot modify the value of a string through a character pointer variable , View only
// Read view
printf("%s\n", p);
*(p + 2) = 'C'; // Target will be 2 Characters c become C, Can't do , Report errors
2.5. Strings and arrays , Two ways of writing :
a) How to write it 1:
char a[] = {'a', 'b', 'c', '\0'};
Be careful : If you want to put the a As a string , You need to manually add the last '\0',( Is an array , String again )
If you do not add a It's just an array of three elements , Instead of a valid string
b) How to write it 2:
char a[] = "abc";
Be careful : No need to add '\0',gcc The compiler will add one for you in the future '\0'
c) Bear in mind : No matter which way it is written , The elements in the character array can be modified
for example : Will be the first 2 Elements 'c' It is amended as follows 'C':
a[2] = 'C';
perhaps
*(a + 2) = 'C';

2.6. Write a string comparison function my_strcmp

2.7. standard C String operation functions provided by the
If you want to use the following written string operation function , Need to add header file :#include <string.h>
a)strlen function : Function to obtain the effective length of the string ( barring '\0')
for example :printf(" length :%d\n", strlen("abc")); //3
perhaps
char *p = "abc";
printf(" length :%d\n", strlen(p)); //3
b)strcat function : The function is string splicing
for example :
char a[10] = "abc";
char *p = NULL;
p = strcat(a, "xyz"); // hold xyz Spliced in abc Save the back of to the array
printf("%s %s\n", p, a); //abcxyz abcxyz

c)strcmp function : The function is a character comparison function
for example :
char *p1 = "abc";
char *p2 = "xyz";
int ret = strcmp(p1, p2);
int ret = strcmp("abc", "xyz"); // In essence, what is ultimately passed is the first address of the string
d)strcpy: String copy function , Will overwrite the original string
for example :
char a[10] = "abc";
char *p = NULL;
p = strcpy(a, "xyzmn");
printf("%s %s\n", p, a);
e)sprintf : Format output function , The function turns numbers (250) Convert to string "250" Save to array
for example :
char a[50] = {0};
sprintf(a, "%d %g, %c", 250, 250.2, 'A');
printf("%s", a);
边栏推荐
- SQL injection database operation foundation
- [UE4] brush Arctic pack high quality Arctic terrain pack
- vs code 插件 koroFileHeader
- How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
- Tensorboard quick start (pytoch uses tensorboard)
- Qt调节Win屏幕亮度和声音大小
- How SVN views modified file records
- The difference between i++ and ++i: tell their differences easily
- [set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela
- 【JokerのZYNQ7020】DDS_ Compiler。
猜你喜欢

kubernetes资源对象介绍及常用命令(三)

鸿蒙第三次培训
![[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.](/img/a0/4fc0e0741aad2885873e60f2af3387.jpg)
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
![[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th](/img/f1/c96c4a6d34e1ae971f492f4aed5a93.jpg)
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th

QT learning diary 9 - dialog box

【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用

Test your trained model

Select 3 fcpx plug-ins. Come and see if you like them

Notes on problems -- watching videos on edge will make the screen green

【JokerのZYNQ7020】DDS_ Compiler。
随机推荐
How SVN views modified file records
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
How to read the source code [debug and observe the source code]
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
Y is always discrete and can't understand, how to solve it? Answer: read it several times
SSH连接远程主机等待时间过长的解决方法
Luogu: p2685 [tjoi2012] Bridge
The difference between get and post
Kotlin learning quick start (7) -- wonderful use of expansion
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
Squid 服务启动脚本
Apache服务挂起Asynchronous AcceptEx failed.
Installation and configuration of network hard disk NFS
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
[combinatorics] recursive equation (special solution example 1 Hannover tower complete solution process | special solution example 2 special solution processing when the characteristic root is 1)
Javescript variable declaration -- VaR, let, const
Kubernetes resource object introduction and common commands (III)
自动渗透测试工具核心功能简述
Play with fancy special effects. This AE super kit is for you