当前位置:网站首页>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);
边栏推荐
- Servlet specification Part II
- 绝对定位时元素水平垂直居中
- Apache service suspended asynchronous acceptex failed
- [combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
- 鸿蒙第三次培训
- Kotlin learning quick start (7) -- wonderful use of expansion
- Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
- Web-ui automated testing - the most complete element positioning method
- 网络硬盘NFS的安装与配置
- ArrayList analysis 3: delete elements
猜你喜欢
Kubernetes resource object introduction and common commands (III)
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
Life is still confused? Maybe these subscription numbers have the answers you need!
kubernetes资源对象介绍及常用命令(三)
设计电商秒杀
How do large consumer enterprises make digital transformation?
Wechat applet for the first time
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
Hongmeng fourth training
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
随机推荐
[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)
SQL injection database operation foundation
How do large consumer enterprises make digital transformation?
自动渗透测试工具核心功能简述
新库上线 | CnOpenData中国观鸟记录数据
How to purchase Google colab members in China
设计电商秒杀
QT adjust win screen brightness and sound size
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
[error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
[RT thread] NXP rt10xx device driver framework -- pin construction and use
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
毕业总结
Servlet specification Part II
Leetcode Valentine's Day Special - looking for a single dog
鸿蒙第四次培训
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
SVN完全备份svnadmin hotcopy