C Language---Data structures (built-in, arrays, custom)

Source: Internet
Author: User
Tags array length

An array is a set of ordered data, each of which belongs to the same data type.

Definition of a one-dimensional array:

Type-character array-name [constant-expression]

In a constant expression, you can include constants and symbolic constants, and int a[3+5] is legal. But cannot contain the Int a[n],c language does not allow dynamic arrays.

An int a[2*n] can exist in a child function, but cannot be a static local variable, because the data in the child function is executed, n is confirmed, and can be

The address space in the stack where int n[2*n] is allocated.

When defined, initializes:

1) int a[10] = {0,1,2,..., 9}; Use {}l= brackets

2) int a[10] = {0,1,2,3,4}; Partial assignment, unassigned elements are automatically assigned a value of 0

3) int a[] ={1,2,3,4,5}; You do not need to specify the array length

Reference to one-dimensional data, array name [subscript]

Two-dimensional data definition:

Type-character array-name [constant-expression] [constant-expression]

Reference array names for two-dimensional arrays [subscript] [subscript]

Initialization of two-dimensional arrays

1) int A[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; Each row element is assigned a separate value

2) int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; All elements are written together to assign values

3) int A[3][4] = {{1},{5},{9}}; For each row element, the value is partially assigned

4) int A[][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; C language can automatically infer the number of travel elements

An array of characters used to hold characters. Each of these elements holds one character. Finally automatically add empty characters ' \ s '

char [10];

Initialization of the character array, char c[10] = {' I ', ' ', ' a ',.... ' Y '}; ' Denotes a space

The string in C is handled in the form of an array of characters, ending with the flag ' \ S ', which represents 0 in ASCII.

You can assign a string to an array of characters.

Char[] = "I am Happy";

If the defined character data length is longer than characters, the system automatically complements '

Char c[10] = {"China"};  printf ("%s", c); Output prints to 5th characters only

The C-language function provides a function for string handling. Must contain #include<string.h>

1) puts (character array);

Char str[] = {"string"};  Puts (str); When the output is converted to \ n,

2) gets (character array);

Char str[10];  Gets (str); Input characters, automatically add ' + '

The puts and get functions can handle only one character array.

3) strcat (string 1, string 2);

Char str1[30] = {""};

Char str2[] = {"China"};

printf ("%s", strcat (STR1,STR2)); STR1 must be large enough to link str2 to str1

4) strcpy (character number 1, string 2);

Char str1[10],str2[] = "China";

strcpy (STR1,STR2); str1 can only be in the form of an array name , the character array 1 must be defined large enough,

When assigning a value, only 6 elements are placed in the first 6 units of the str1 (including ' \ S '), followed by 4 cells, which are still str1 characters .

strncpy (Str1,str2,n); The first n characters of the str2 are copied to the first n characters of the str1 , not including '/'. ‘’

The string cannot be used directly between str1 = STR2

5) strcmp (string 1, string 2);

strcmp (STR1,STR2); Compare str1 and str2 until a different character and ' s ' are present.

STR1 = = str2, return value 0,str1 > str2, return positive integer, str1 < str2, return negative integer

Strings cannot be used directly between STR1 > STR2

6) strlen (character array);

Char str[10] = "China";

printf ("%d", strlen (str)); The output is 5, indicating the actual number of characters, not including '/'

7) strlwr (string); Converts the uppercase of a string to a lowercase letter

8) STRUPR (string);//Convert the lowercase of a string to uppercase

User-defined structure type:

struct struct name {member table column};

struct Student {int num;  Char name[20];} Student1;

Define struct-Body variables:

struct Body name struct variable

struct Student student1; struct type name must be added to the structure

You can also specify struct variables directly without specifying a type name

struct {int num;  Char name[20];} Student1;

1) When defining a struct variable, initialize:

struct Student b = {. Name = "Zhang"}; A reference to a struct member must use the member operator "."

2) refer to members of struct variables by struct variable name. Member Name

Student1.num = 10010;

3) If the member itself is a struct, you can use the "." multiple times. To quote:

Student1.birthday.month

4) struct variables of the same type can be assigned to each other:

Student1 = Student2;

5) You can reference the address of a struct variable, or you can refer to the address of a struct variable member:

&student1.num &student1

To define an array of struct bodies:

struct type array name [array length]

struct person leader[3];

To define a struct-body pointer:

struct Student *pt; If p points to a struct variable stu, struct variable. Member name (stu.num)

(*p). Member name ((*p). Num)

P-> member name (p->num) equivalent

Pointer to an array of struct bodies

struct Student {int num; char name[];};

struct Student stu[3];

struct Student *p;

for (p=stu; p <stu +3; p++);

Using an array or pointer of a struct variable to do function arguments

void input (struct Student stu[]);

The common body type, in the same storage space, stores different data structures and can only select one at a time.

To define a shared body variable:

Union common body Name {member table column} variable table column;

Union Data {int i; char ch; float F;} a,b,c;

Referencing shared body variables: A.I, a.ch, A.F

When using the shared body type data, only one data structure can be stored in the same memory segment for a certain time period

The element that works in the common body is the last assigned value.

a.ch = ' a ';  A.F = 1.5;   a.i=40; The value in a is 40

If the value of a variable can be only a few possible values, it may be defined as an enum (enum) type

declaring enum type: enum [enum name] {enumeration element list}

Enum Weekend {Sun, Mon, Tue, Wed, Thu, Fri, Sat};

Declaring an Enumeration variable: enumeration type name Variable name

Enum Weekend Workday;

The C compiler handles enumeration elements as constants. This is called an enumeration constant, so you cannot assign a value to an enumeration variable.

C language in order, assigning them 0,1,2,,,

printf ("%d", workday); Output the corresponding value

Use a typedef to re-declare the type.

typedef int integer;

typedef struct {int month; int day; int year;}  Date; Declares a new type name date

typedef int NUM[100]; Declares num as an integer array type name num A; A is an integer array name, with 100 elements

typedef char * String; String p, which defines a string as a character type pointer

typedef int (*pointer) (); Declaring pointer as a pointer type to a function

C Language---Data structures (built-in, arrays, custom)

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.