C-language struct (nested struct-structure array)

Source: Internet
Author: User
Tags array definition sprintf

//structs--nested structures and struct arrays#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<string.h>typedefstruct_parent{intnum; Charname[ -]; //struct internally defines a struct, and if a nested struct variable is not defined, then the property of the nested struct is treated as a property of the parent structure body    structson{intAge ; Charsname[ -]; };} Parent;typedefstruct_parent2{intnum; Charname[ -]; //struct internally defines a struct, if a nested struct variable is defined, the property of the nested struct cannot be called by the parent struct//But the parent struct can call the nested struct-body variable    structson2{intAge ; Charsname[ -]; }S1;} Parent2;structparent3{intnum; Charname[ -];} parr2[2] = { {5,"Father 5"}, {6,"Father 6"} };//struct Array definition initialization twovoidMain () {/*Nested structure Body*/Parent p1; P1.num=1; sprintf (P1.name,"Father"); P1.age= A; sprintf (P1.sname,"son"); printf ("num=%d;name=%s;age=%d;sname=%s\n", P1.num,p1.name,p1.age,p1.sname);    Parent2 P2; P2.num=2; sprintf (P2.name,"Father 2"); P2.s1.age= -; sprintf (P2.s1.sname,"son 2"); printf ("num=%d;name=%s;age=%d;sname=%s\n", P2.num, P2.name, P2.s1.age, p2.s1.sname); /*definition of an array of structural bodies*/Parent2 parr1[2] = { {3,"Father 3", A,"son 3"}, {4,"Father 4", the,"son 4"} };//The struct array defines the initialization of a//Parent2 PA1, PA2; //Parent2 ptd[2] = {PA1, PA2};     /*Error : Error C2440: "Initialize": Cannot convert from "Parent2" to "int" rror C2440: "Initialize": Cannot convert from "Parent2" to "char"*/    //There is no such structure initialization, the C language compiler treats PA1 as the first parameter of the first struct element, and//PA2 as the second parameter of the first struct element//so the errorParent2 ptdd[2]; memset (PTDD,0,sizeof(PTDD)); memset (&PTDD, 0, sizeof (PTDD)); //Two types of initialization are correct, the second is recommended because the second is more consistent with the use of the Memset function, the second pointer is a pointer to the array, the first pointer is a pointer to the first element of the array
The purpose of using memset () Here is to initialize the entire array, so the second one is more consistent.
System"Pause");}

C-language struct (nested struct-structure array)

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.