Basic knowledge of C language _ structure access, community, enumeration type

Source: Internet
Author: User
Reference of C-language struct variables: for common variables of the defined struct type, visit its members using the dot operator ("."). The standard access method is as follows:
Struct variable name. member name
For pointer variables defined as pointing to the struct, use the arrow operator ("->") to access. The standard ACCESS format is as follows:
Pointer variable name to struct> member name
When a struct is nested, access the struct members:
Pointer variable name pointing to the struct
-> Secondary struct name. member name For example, define typedef struct mystruct {int I; char * P; struct * temp {int A; int * B ;}} mydev; mydev mydev1; mydev * mydev2; There are several reference methods: 1) group mydev1. I and mydev1.pmydev2-> I and mydev2-> P2) group mydev1.temp-> A and mydev1.temp-> bmydev2-> temp-> A and mydev2-> temp-> B

Instance:

# Include "stdafx. H"
# Include <windows. h>
// # Include "log. H"
# Include "msglog. H"
# Include "stdlib. H"

Int B;
Typedef struct _ atmp
{
Int D;
Int * E;
} Atmp;
Typedef struct _ bdata
{
Int;
Char * s;
Struct CTMP
{
Int B;
Int C;
} Ff;
Atmp * atmp;
Atmp EE;
 
} Dataa, * datab;

Int _ tmain (INT argc, _ tchar * argv [])
{
Int x = 3, y = 5;
Dataa * A = (dataa *) malloc (sizeof (dataa ));
Memset (A, 0, sizeof ());
A-> A = 0;
A-> S = "Test! ";
// A. tmp. B = X;
// A. tmp. c = y;
A-> atmp = (atmp *) malloc (sizeof (atmp ));
A-> atmp-> d = X;

A-> atmp-> E = & Y;
A-> ff. B = X;
A-> ee. d = X;
A-> ee. E = & Y;

Dataa B;
B. A = X;
B. S = "Test B ";
B. ff. B = X;
B. ff. c = X;
B. ee. d = X;
B. ee. E = & Y;
B. atmp = (atmp *) malloc (sizeof (atmp ));
B. atmp-> d = X;
B. atmp-> E = & Y;

Datab c = (datab) malloc (sizeof (datab ));
Memset (C, 0, sizeof (datab ));
C-> A = X;
C-> S = "Test C ";
C-> ff. B = X;
C-> ff. c = X;

C-> atmp = (atmp *) malloc (sizeof (atmp ));
C-> atmp-> d = X;

C-> atmp-> E = & Y;
C-> ee. d = X;
C-> ee. E = & Y;
Return 0;
}

Enumeration type:

# Include "stdafx. H"
# Include "msglog. H"
# Define a_test_a

Typedef Enum _ char_type
{
A_test,
B _test,
C_test,
D_test
} Char_type;

Int _ tmain (INT argc, _ tchar * argv [])
{
Int A, B;
A = 3; B = 4;
 

# Ifdef a_test
A = 5;
# Endif

Enum _ char_type AA = a_test ;;

Switch (aa)
{
Case a_test: A = 10; break;

}

Log (level_info, "AA = % d \ n", );

Char_type BB = B _test;

Switch (bb)
{
Case B _test: A = 15; break;

Case c_test: A = 20;

Default: A = 30;

}

Log (level_info, "BB = % d \ n", );

Int AB = 3;

Switch (AB)
{
Case d_test: A = 40; break;
}

Log (level_info, "AB = % d \ n", );

Int c = 20;
Char_type cc = (char_type) 20;
// Values can be assigned only when they are forcibly converted to enumeration types.

Return 0;

Result:

AA = 10
BB = 15
AB = 40

 

Pointer conversion for structural weight

# Include "stdio. H"
# Include "string. H"
# Define numsize 100
Typedef struct _ Test
{
Int;

} Test;

Typedef struct _ Testa
{

Int C;
Int D;
Char A [numsize];
} Testa;

Int main ()
{
 
Testa BB, DD;

Int cc;
BB. c = 10; // BB. d = 20;
BB. A [1] = 'a ';
// BB. A [] = "ABC"; // Error

Test * AA;
AA = (test *) & BB; // remember that the address needs to be converted

Printf ("AA-> A = % d \ n", AA-> );

Static char C [] = "basic_dbase ";

Char B [80] = "study C programme! \ N ";

Printf ("C [] = % s \ n", C );

Printf ("B [100] = % s \ n", B );
 
Memset (BB. A, 0, sizeof (char) * numsize );
Memcpy (BB. A, B, sizeof (char) * strlen (B ));
Printf ("BB. A = % s \ n", BB. );
// Dd. A = B; // Error

Char * ff;
FF = "ABC ";
Printf ("FF = % s \ n", ff );

Int * gg, KK;
Gg = (int *) & BB;
Kk = * (int *) & BB;
Printf ("GG = % d, KK = % d \ n", * gg, KK );

 

Int * gg, KK;
Gg = (int *) & BB; // pointer Conversion
Kk = * (int *) & BB;
Printf ("GG = % d, KK = % d \ n", * gg, KK );

Char * s = "Keil is WV \ n ";
// S [1] = 'Z'; // s refers to the pointer in the stack and points to the constant storage area. Therefore, the value of the constant storage area cannot be changed.
Printf ("s = % s \ n", S );
Char SZ [] = "Keil is WV \ n ";
SZ [2] = 's'; // SZ indicates the inside of the stack and the string to which it points is also in the stack. Therefore, the variable value can be changed in this way.
Printf ("SZ = % s \ n", SZ)

Return 0;
Return 0;
 
}

Result:

Aa-> A = 10
C [] = basic_dbase
B [1, 100] = study C programme!
BB. A = study C programme!

FF = ABC;

 

Gg = 10, KK = 10

 

S = Keil is WV

SZ = kesl is WV

 

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.