C language learning notes [struct 02] function parameters of struct pointer variables and struct Variables

Source: Internet
Author: User

C Language Study Notes Struct pointer variable

A pointer is the core of the C language. How many students have bowed to the pointer's foot. It is not difficult to simply say pointers, but if they are combined with other structures, they will die. For example, arrays are not difficult at all, but they are often difficult to use with pointers. Struct is also a structure. How does it work with pointers? Let's take a look!

1: struct pointer variable

Like the pointer pointing to the first address of the array, the pointer can also point to the starting address of the struct.

Format: struct student * p;

The preceding statement defines a pointer Variable p pointing to any data of the struct student type. When a pointer is used to access a member of a struct variable, there are two methods:

(* P). score or p-> score(This is a common method. -> Yes to the operator)

For example:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201G14K7-0.jpg "title =" 1.jpg"/>

When accessing: (* p). num = 11031, or p-> num = 11031.

2. program instance


# Include <stdio. h>/* define struct */struct student {int num; char name [20]; char sex; int age; float score ;}; /* initialize a struct instance */struct student stu [3] = {11302, "Wang", 'F', 20,486.69}, {11303, "Zhao ", 'F', 25,466.59 },{ 11304, "Xue", 'M', 26,483.59 }}; main () {/* initialize a student1 instance */struct student1 = {11305, "Li", 'F', 19, 59.59}; struct student * p, * q; // define the pointer of the struct student type int I; p = & student1; // assign the first address of the struct of student 1 to p, that is, p points to the first address of student 1/* output: You can see that there are three methods to access the struct */printf ("% s, % c, % f \ n ", student1.name, (* p ). sex, p-> score); q = stu; // assign the first address of the array stu to q;/* for loop output array member */for (I = 0; I <3; I ++, q ++) {printf ("% s, % c, % f \ n", q-> name, (* q ). sex, stu [I]. score );}}


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201G153Q-1.png "title =" 2.png"/>

3. pointer symbol->)

P-> num

Member in the struct to which P points

P-> num ++

First obtain the struct member num pointed to by p, and then add the value of this num to 1

++ P-> num

Add 1 to the value of the member num pointed to by p, and then reference this new value.

(P ++)-> num

Reference the value of p-> num first, and then add 1 to p after use.

(++ P)-> num

Add p to 1 first, and then reference the value of p-> num.

4. struct variables and struct pointer variables are used as function parameters.

Struct variables and struct pointer variables can be used as function parameters like int type.


# Include <stdio. h>/* define struct */struct student {int num; char name [20]; char sex; int age; float score ;}; /* initialize a struct instance */struct student stu [3] = {11302, "Wang", 'F', 20,486.69}, {11303, "Zhao ", 'F', 25,466.59 },{ 11304, "Xue", 'M', 18,483.59 }};/* output function, struct variable s as function parameter */void print (struct student s) {printf ("% s, % d, % f \ n", s. name, s. age, s. score);}/* adds the score function. The struct pointer q is used as the function parameter */void add (struct student * q) {if (q-> age <= 19) q-> score = q-> score + 10;} main () {struct student * p; int I; for (I = 0; I <3; I ++) {print (stu [I]); // stu [I] As the form parameter} for (I = 0, p = stu; I <3; I ++, p ++) {add (p); // the pointer Variable p pointing to stu as the form parameter} printf ("\ n"); for (I = 0, p = stu; I <3; I ++, p ++) {print (* p); // * p as the form parameter, equivalent to stu [I]}


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/201G15b9-2.png "title =" 3.png"/>

Note that the print and add call functions are void functions with no return value type. If there is a return value, it should be of the struct student type, that is, it should be

Language

struct student add(struct student *q){  if(q->age<=19)  q->score=q->score+10;  return *q}



This article from the "Zhao Yuqiang blog" blog, please be sure to keep this source http://zhaoyuqiang.blog.51cto.com/6328846/1292014

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.