Diary of C-struct variable and struct-body array

Source: Internet
Author: User

"Struct"

Two ways to define struct bodies
<1> struct student{};
struct student a={10001, "in the Cloud", ' M ', "Beijing"};
<2> struct student{
}a={10001, "In the Cloud", ' M ', "Beijing"};
Define struct-body array A to a[],struct student stu[3]={{..},{..},{...}; The value in [] can not be specified


"Struct-Body pointer"

[pointer to struct variable]
    <1>struct student *pt;         //The pointer variable base type must be the same as the type of the struct variable
    <2>struct student Stu;
        struct student *p;
        p=&stu;
        printf ("%d", (*p) .num;    //p=&stu,*p=stu
     <3>printf ("%d", P->num);        //-> pointer operator represents a member of the struct that the pointer variable p points to
     Top: Three ways to express structure members: <1>stu.num <2> (*p). Num <3>p->num.
        #:p->num++: Gets the value of member n in the struct variable that p points to, [after use] plus 1;
            ++p->num: Gets the value +1 of the member N of the struct variable that the P points to, and then starts using it.


[Pointer is a base type]
Pointer variables cannot be arbitrarily assigned, because different pointers have different base types, such as


<1> pointer variable p with the base type int
int a=2;
int *p=&a;
printf ("%d", * (*p+1)); Address value of address +1 (that is, 2 sub-section after output a) of output a


<2> pointer variable p for one-dimensional array of base type
Char a[]={a,b,c};
int *p=a;
printf ("%c", * (p+1)); Outputs the first address of the current array plus the value corresponding to the number of bytes in the array that the char type occupies.


<3> One-dimensional pointer variable p with base type two-bit array
Char A[][]={{a,b,c}{d,e,f}{g,h,i}};
int *p=a+1; Assigns the first address of the second row array to P
printf ("%c", * (* (p+1))); Outputs the value of the current array's first address plus the number of bytes that the row array occupies.


<4> Two-dimensional pointer variable p with base type two-bit array
Char A[][]={{a,b,c}{d,e,f}{g,h,i}};
int *p=* (a+1);//a[1]//Assign the No. 0 column address of the second row to P
printf ("%c", * (p+1)); Arrays of the same dimension


      <5> pointer variable p
  base type is struct variable         struct student{
             int num;
             int name[20];
         }stu{10002, "test2"};
         struct student *p;
         p=&stu;                    //Here the Stu is equivalent to the normal variable, which is to take the address of the
         printf ("%d\n,%20s\n", p->num,p->name);//Output the member value corresponding to the current address


      <6> pointer variable p
  The base type is a struct array         struct student{
             int num;
             int name[20];
         }stu[2]{{10001, "Test1"},{10002, "Test2"}};
         struct student *p;
         p=stu;                     //assigns the first address of the struct to P
         printf ("%d\n,% 20s\n ", (p+1)->num, (p+1)->name);//outputs the first address of the current struct plus the corresponding value of the byte number of a struct.


      <7> normal pointer variable type is converted to a pointer variable with a base type of struct"
         struct student{
              int num;
             int name[20];
         }stu[2]{{10001, "Test1"},{10002, "Test2"}};
         struct student *p;
         p= (struct student *) stu[0].name;            //assigns the address of name of the first struct in the struct array to P
         printf ("%20s\ N ", * (p+1));//Output The current structure of the first address plus the number of bytes of the structure of the corresponding value, that is, the output of test2.


<8> pointer variable p with the base type void
void *p;
int *t= (int *) p; Want to use a must type conversion


[Note:] structure and structure of the larger difference is that the structure of the variable name only represents the alias of the memory area, the array name of the struct array represents the first address of the struct, so in the use of the struct variable needs to take the address, and the struct array does not.


[Note:]struct declaration is not allocated memory space, and only allocate space to &AMP;A (fetch address), such as:
<1> struct student{
int num;
int name[20];
}; This is not allocated memory space, only the declaration is not defined
<2> struct student{
int num;
int name[20];
}//str{10001, "Test"}; First: Allocate memory space, after which you can use struct int *p = &str;
struct Student str; The second type: memory space is still allocated, but not assigned, you can use struct int *p = &str;





Diary of C-struct variable and struct-body array

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.