Structures in C and their transfer

- , . , . , . .

, .

What is an array




, , . - , . - , .

: int a[4] = {1, 2, 3, 4}.

, a - , int - , { } , [ ] , . , 4. , , . , , .

. , , 5 -, 3 : int a[5][3]. , .





. - , , . , , . .

Class or structure?




, . ? , . , -, . , .

:

class /*class name*/

{

private:

/* private , */

public:

/* */

protected:

/* */

}.

. , , :

  • ;
  • ;
  • ;
  • .

.





int date[20], int number[20], char title[80], int price[20].

, . 3 : cout << “ : ” date[3] “, : ” number[3] “, : ” title[3] “, : “ price[3]).

, :

struct book {

int date;

int number;

char title[20];

int price[20];

}.

How to create a structure




- . - , .

Structures in C




- .

.

struct name

{

type member;

}

Struct - , , name - , type - , member - .

:

name name2, name - , name2 - .

.

struct name

{

type member;

} name2;

.

, .

struct name

{

type member;

} name2, name3, name4.

.

struct name

{;

char member

};

name name2;

name2.member=”a”;

.

struct name

{

char member = “a”;

} name2;

, . , . , , .

- . private .

Array of Structures




- . , . , , .

n-e . :

int a[16];

:

struct MyStruct

{

int a;

};

MyStruct obj1[10];

MyStruct "". obj1[] - , 10 .

MyStruct obj1[10], obj2[5], . :

struct MyStruct

{

int a;

} obj1[10];

, . .

- , , , - . , , . , ptr = &var1 , , . var1 - ptr.

* , . , *ptr , , ptr.

, new.

struct MyStruct

{

string a;

}

, MyStruct * point = new MyStruct[1];

delete. , delete p[];

, . , .

myStruct b.

struct myStruct {

string fio;

long num;

} b;

fio:

b.fio

. , b.fio = “Ivanov”.

.

struct myStruct {

string fio;

long num;

} tel[2]={

{ "", 456756 },

{ "", 632345 }

};

. , :

cout << myStruct tel[1].fio;

456756, cout << myStruct tel[1].num.

structure and function




, .

struct myStruct {

char text[100];

int value;

};

value, text 100 . menu myStruct: myStruct menu. , .

void item(myStruct menu)

{

sprintf(menu.text,"One item");

menu.value=50;

}.

- , , . , , , public.

struct, { } .

struct name

{

int member;

};

.

struct name

{

int member;

} a;

or

struct name

{

int member;

} a;

struct name a.




All Articles