C

语法

结构体的定义说明了它的组成成员,以及每个成员的数据类型。定义一般形式如下:

struct 结构类型名 
{ 
数据类型 成员名 1; 
数据类型 成员名 2; 
...... 
数据类型 成员名 n; 
};

内存使用

struct Record { int i; double x; char str[16]; };
  • 只有结构体变量才分配地址,而结构体的定义是不分配空间的。
  • 结构体中各成员的定义和之前的变量定义一样,但在定义时也不分配空间。
  • 结构体变量的声明需要在主函数之上或者主函数中声明,如果在主函数之下则会报错
  • c语言中的结构体不能直接进行强制转换,只有结构体指针才能进行强制转换
  • 相同类型的成员是可以定义在同一类型下的
  • 定义结构体变量以后,系统就会为其分配内存单元

&ref(): File not found: "struct_memory.jpg" at page "+C+结构体";

定义结构变量的三种方式

1.先定义结构,再说明结构变量

struct stu
{
int num;
char name[20];
int age;
};
struct stu boy1,boy2;
 
例如:
#define STU struct stu
STU
{
int num;
char name[20];
int age;
};
STU boy1,boy2;

2.在定义结构类型的同时说明结构变量。

struct stu
{
int num;
char name[20];
int age;
}boy1,boy2;

3.直接说明结构变量

struct
{
int num;
char name[20];
int age;
}boy1,boy2; 

结构体嵌套

struct date 
{ 
int month; 
int day; 
int year; 
};
 
struct {
int num 
char name[20];
char sex;
struct date birthday;
float score;
}boy1,boy2;

高级的用法

struct address
{
        char name[30];
        /*姓名,字符数组作为结构体中的成员 */
        char street[40]; /*街道*/
        unsigned long tel; /*电话,无符号长整型作为结构体中的成员 */
        unsigned long zip; /*邮政编码*/
}student[3]={
{"Zhang","Road NO.1",111111,4444},
{"Wang"," Road NO.2",222222,5555},
{"Li"," Road NO.3",333333,6666}
}

示例

#include<stdio.h>
  
#include<string.h>
  
struct _INFO
{
        int num
        char str[256];
};
  
int main()
{
        struct _INFO A;
        A.num = 2014;
        strcpy(A.str,"Welcome to dotcpp.com");
        printf("This year is %d %s\n",A.num,A.str);
        return 0;
}

memset的使用

使用memset来初始化,一个结构体

#include<stdio.h>
#include <string.h>
typedef struct ss
{
    int num;
    int dir[5][3];
}tent;
//tent a; 
int main()
{
    tent a;
    memset(&a,0,sizeof(a));
    return 0;
}


★月額105円~/容量最大30GB/機能満載! ロリポップ!レンタルサーバー ★

コメント:



(画像の文字列を入力して下さい)

トップ   編集 凍結 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:32:59 (1628d)

G|Cg|C@Amazon Yahoo yV

z[y[W yVoC[UNLIMITȂ1~] COiq COsیI