C language: structures and structure pointers

Publisher:冰山火影1977Latest update time:2015-01-19 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Pointer variable pointing to the structure:

In C language, -> is a whole, which is used to point to a structure. Suppose we define a structure in the program, and then declare a pointer variable to point to this structure. If we want to use the pointer to retrieve the data in the structure, we need to use the pointing operator "->".
 
 
for example:
 
 
struct SunLL
{
  int a;
 
 
  int b;
 
 
  int c;
};         
 
struct SunLL * p; //define structure pointer
struct SunLL A = {1,2,3}; //define a variable A of type SunLL
 
 
int x; //define a variable x
p = &A ; //Let p point to A
x = p->a; // equivalent to x=(*p).a (*p) indicates the structure variable pointed to by p
 
 
                                      //This sentence means to take out the data item a contained in the structure pointed to by p and assign it to x
 
                                     //Since p points to A at this time, p->a == Aa, which is 1
 
 
2. Pointer variable pointing to structure array:
 
 
A pointer variable pointing to a structure can also point to an array of structures and their elements.
 
 
If the procedure is as follows:
 
 
struct SunLL *p,sun[3];
 
 
p = sun;
 
 
If we assume that the address of sun[0] is 1000, the pointer variable p points to the first address of the structure array sun. Since the value of size of (struct SunLL) is 6, each structure element occupies 6 bytes of memory space, so p+1 points to address 1006, and p+2 points to address 1012.
 
 
When using pointer variables to point to structure variables or structure arrays, you should pay attention to the priority of operators. In C language, "()", "[ ]", "->", and "." have the same priority, which has the highest priority, followed by "*", "++", "--", and "&". For example, the expression ++P->a is equivalent to ++(p->a)
 
 
(++p)->a first calculates ++p, and p points to sun[1];
 
 
P++->a; then the expression is equivalent to (p++)->a;
 
 
The expression p->a++ is equivalent to (p->a)++;
 
 
3. Structure as function parameter and structure pointer as function parameter
 
 
Example 1
 
 
struct st
 
 
{
 
 
int a;
 
 
char b;
 
 
};
 
 
fun(struct st bc)
 
 
{
 
 
bc.a+=5;
 
 
bc.b='A';
 
 
printf("%d,%c\n",bc.a,bc.b);
 
 
}
 
 
main()
 
 
{
 
 
struct st bl;
 
 
bl.a=3;
 
 
bl.b="c";
 
 
fun(bl);
 
 
printf("%d,%c\n",bl.a,bl.b);
 
 
}
 
 
The result is: 8, A 3, c
 
 
Example 2:
 
 
struct st
 
 
{
 
 
int a;
 
 
char b;
 
 
};
 
 
fun(struct st *bp)
 
 
{
 
 
bp->a+=5;
 
 
bp->b='A';
 
 
printf("%d,%c\n",bc.a,bc.b);
 
 
}
 
 
 
main()
 
 
{
 
 
struct st bl;
 
 
bl.a=3;
 
 
bl.b="c";
 
 
fun(&bl);
 
 
printf("%d,%c\n",bl.a,bl.b);
 
 
}
 
 
The running result is: 8, A 8, A
 
 
Summarize:
 
 
When a structure variable is used as a function parameter, the actual parameter value is passed to the formal parameter, which is a value transfer. When all the parameters of the structure are passed to the formal parameter for use, the value of the structure itself will not change. When a pointer to a structure variable is passed as an actual parameter, the address of the structure variable is passed to the formal parameter, which is an address transfer and will change the member values ​​of the actual structure variable.
Reference address:C language: structures and structure pointers

Previous article:Application of Single Chip Microcomputer in Sewage Treatment System
Next article:C language: pointer function and function pointer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号