1219 views|0 replies

3836

Posts

19

Resources
The OP
 

Analysis of Embedded C Language Pointers [Copy link]

As the basic language for embedded development, C language has increasingly let engineers know its power: registers can be directly manipulated to facilitate the function setting of CPU; physical addresses can be directly manipulated, and bit operations can be performed to achieve hardware operations, etc. If you use 8-bit or 16-bit microcontrollers for development and learning, I believe that some programming techniques can be used to complete the design: loops, selections, bit operations, conditional judgments, arrays, and program nesting, etc. If you are operating an operating system and pay attention to the kernel of the operating system (taking Linux as an example), you will find that these C languages are a small part of it. For operating systems, pointers are more used. One of the important reasons is the fast processing speed. So, today I will summarize the pointers of C language.
  For students who have studied pointers, they know that constants cannot be directly given to pointers. If you want to give pointers constants, you need to do the following: int num=8;int *pnum=#At this time, we need to pay attention to the address where 8 is stored, and *pnum is the data 8 stored at this address.
 

  Pointers and arrays have a natural connection. In fact, an array is a continuous address that stores constants. Let's look at the following example: int arry[3]={1,3,5}; then arry is the first address of the array, *arry is the data 1 stored at the first address of the array, and *(arry+1) is the data 3 stored at the second position of the array.
  From the above analysis, we can see that *arry and arry[1] also point to the first element of the array.
  Then we can extend it further as follows: a. int *data[3] is a pointer array. Each element in the array is a pointer to int data. The values are assigned as follows: int arry[3]={1,3,5}; int *data[3]={arry,arry+1,arry+2}; data[0] points to the first address of arry, and *data[0] is the data 1 stored at this address. data is the first address of the data array, and *data and data[0] are both the first address of the array arry. In fact, we can also say that three arrays data[0], data[1] and data[2] are defined.
  Analysis of embedded C language pointers
  b. int(*data)[3] is an array pointer, which is an array. Each element stores an address. The assignment is as follows: int arry[3]={1,3,5}; int (*data)[3]=&arry; Then *data is the address of the first element, and **data is the element 1 where the first address is located.
  Then person.pername[20]="jack" cannot be passed. The reason is that the structure is an abstract data type and does not allocate storage units, so the array assignment cannot be passed. If pointer assignment is used, this kind of problem can be solved.
  Pointers to functions are very common in the Linux operating system. Let's take a simple example to illustrate: int print(int a, int b, char *p)
  { printf("input number sum is %d,input string is %s\n",a+b,p); //Three parameters, int int and char*
  From the above example, we can see that a function is actually a special function program starting from an address, and its function name is the first address of the program, so we can assign a value to a pointer function like this: pprint=print; //Assign a value to a pointer function

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list