C language Learning--structural body

Source: Internet
Author: User

The structure is defined in three ways:

      1#include <stdio.h>2 //The first method of definition      3 structPoint {4         intx; 5         inty; 6 }; 7 structPoint p1,p2; 8       9 //The second method of definition     Ten struct {      One         intx;  A         inty;  -} p1,p2;  -       the //The third method of definition      - struct {      -         intx;  -         inty;  +} P1,P2;

These three methods, for the first and third types, declare the structure point. But the second one doesn't, only two variables are declared.

The following small example illustrates two types of initialization methods for a struct type.

      1#include <stdio.h>2 structDate {3         intmonth; 4         intDay ; 5         intYear ; 6 }; 7 intMain ()8 {      9         structDate today = {2, -, -};     The initialization of the structure, Method 1, and initialization of the same array. Ten         structDate Thismonth = {. Month =3,. Year = -};     Initialization of the structure, method 2, specifying the corresponding struct member initialization.  Oneprintf"todays date is%i-%i-%i\n", Today.day,today.month,today.year) Aprintf"This month date is%i-%i-%i\n", Thismonth.month =2, Thismonth.year = -}      -         return 0;  -};

struct Members:

Structs and arrays are a bit like, access methods are different. The array is "" access to the elements inside, and the structure is used to access the struct members.

Structure Operation:

To access the entire structure, you can directly access the name of the struct variable.

For the entire structure can be assigned value, take address, can be passed to the function.

P1 = struct point{5,10}; Equivalent to p1.x = 5,P1.Y = 10

P1 = p2;

Structure Pointers:

Structs and arrays are not the same, the array is a pointer, you can take the address directly. To take an address to a struct member, you must use &.

struct Date *pdate = &today;

Structure as a parametric function:

int numofdays (struct date D);

The entire structure can pass in the function as the value of the parameter.

This is the time to create a new struct variable inside the function and assign the value of the caller's structure.

You can also return a structure. is different from the array.

Input structure:

The scanf function can only read into a variable. How to read into a structure?

You can call a function to implement it. Note: The C-language function call is value-passed.

So the structure variable p in the function and the structure variable p in the main function are different.

  No value is returned to main after the function reads p, so the value of the main function structure is unchanged.

Solution:

Create a temporary struct variable p inside the function and return a struct variable.

Pointer to structure:

      1#include <stdio.h>2 structDate {3         intmonth; 4         intDay ; 5         intYear ; 6} myday; 7       8 structDate *p = &Myday; 9      Ten(*p). Month = A;  OneP->month = A;

Represents a member of a struct variable that the pointer refers to.

  

C language Learning--structural body

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.