您的位置:首页 > 编程语言 > C语言/C++

C语言_结构体

2015-11-30 17:21 513 查看
//
//  main.c
//  C_4-1
//
//  Created by ibokan on 15/11/30.
//  Copyright © 2015年 aishuo. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[100];
int age;
float score;
};
typedef struct
{
char *name;//这个是字符指针
int age;
float score;
}NewStudent;
int main(int argc, const char * argv[]) {
/*
结构体:
C语言提供来发者来自定义数据类型
结构体的定义:
struct 结构体名称(最好大写)
{
类型 变量1;
类型 变量2;
....
};
注意:结构体定义,在花括号后面加分号;
*/
struct Student st=
{
"Rick",25,99.9
};

NewStudent student={"Rick",25,99.9};
/*
结构体变量初始化 struct 结构体名 变量名={值1,值2,....};
*/
//取结构体元素'.'
printf("%s\n",st.name);
printf("%d\n",st.age);
printf("%f\n",st.score);
//求结构体占内存字节数
int length =sizeof(char)*100+sizeof(int)+sizeof(float);
printf("length=%d\n",length);

/*
用户自己开辟内存 使用malloc函数 ,#include<stdlib.h>
*/
char *theName=(char *)malloc(sizeof(char)*100);
//在内存中开辟了100个字节
char *tmp="张三";
theName=tmp;
student.name=theName;
printf("%s\n",student.name);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: