当前位置:网站首页>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++); //0x12
obtain 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++); //0x1234
Conclusion : 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++); //0x12
perhaps :
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)); //0x12
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
short *p1 = (short *)p; // Force the typeless pointer to char*
printf("%#x\n", *p1++); //0x5678
printf("%#x\n", *p1++); //0x1234
perhaps :
printf("%#x\n", *(short *)(p+0)); //0x5678
printf("%#x\n", *(short *)(p+2)); //0x1234
1.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);
边栏推荐
- Y is always discrete and can't understand, how to solve it? Answer: read it several times
- Squid 服务启动脚本
- Redis: operation commands for list type data
- Life is still confused? Maybe these subscription numbers have the answers you need!
- QT学习日记9——对话框
- Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
- Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
- Simple use of unity pen XR grab
- Loop through JSON object list
- Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
猜你喜欢
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
设计电商秒杀
QT learning diary 9 - dialog box
Simple use of unity pen XR grab
kubernetes资源对象介绍及常用命令(三)
Unity notes unityxr simple to use
One brush 149 force deduction hot question-10 regular expression matching (H)
QT学习日记9——对话框
How to read the source code [debug and observe the source code]
Automata and automatic line of non-standard design
随机推荐
在iptables防火墙下开启vsftpd的端口
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
Swm32 series Tutorial 4 port mapping and serial port application
网络硬盘NFS的安装与配置
SWM32系列教程4-端口映射及串口应用
SVN如何查看修改的文件记录
A day's work list of an ordinary programmer
基于主机的入侵系统IDS
c# . Net tool ecosystem
vs code 插件 koroFileHeader
Leetcode13. Roman numeral to integer (three solutions)
毕业总结
Graduation summary
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
POM in idea XML graying solution
Apache service suspended asynchronous acceptex failed
Great changes! National housing prices fell below the 10000 yuan mark
Qt调节Win屏幕亮度和声音大小
When absolutely positioned, the element is horizontally and vertically centered