struct stuff{
char job[20];
int age;
float height;
};
//直接带变量名Huqinwei
struct stuff{
char job[20];
int age;
float height;
}Huqinwei;
struct stuff{
char job[20];
int age;
float height;
};
struct stuff Huqinwei;
struct stuff yourname;
struct{
char job[20];
int age;
float height;
}Huqinwei;
struct stuff yourname;
struct stuff Huqinwei = {"manager",30,185};
也可以考虑结构体之间的赋值:
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
struct stuff faker = Huqinwei;
//或 struct stuff faker2;
// faker2 = faker;
Huqinwei.job[0] = 'M'; Huqinwei.job[1] = 'a'; Huqinwei.age = 27; nbsp;Huqinwei.height = 185;
int main()
{
struct stuff Huqinwei;
struct stuff &ref = Huqinwei;
ref.age = 100;
printf("Huqinwei.age is %d\n",Huqinwei.age);
printf("ref.age is %d\n",ref.age);
struct stuff *ptr = &Huqinwei;
ptr->age = 200;
printf("Huqinwei.age is %d\n",Huqinwei.age);
printf("ptr->age is %d\n",Huqinwei.age);
//既然都写了,把指针引用也加上吧
struct stuff *&refToPtr = ptr;
refToPtr->age = 300;
printf("Huqinwei.age is %d\n",Huqinwei.age);
printf("refToPtr->age is %d\n",refToPtr->age);
}
struct test{
int a[3];
int b;
};
//对于数组和变量同时存在的情况,有如下定义方法:
struct test student[3] = {{{66,77,55},0},
{{44,65,33},0},
{{46,99,77},0}};
//特别的,可以简化成:
struct test student[3] = {{66,77,55,0},
{44,65,33,0},
{46,99,77,0}};
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct changeable{
int iCnt;
char pc[0];
}schangeable;
main(){
printf("size of struct changeable : %d\n",sizeof(schangeable));
schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeable) + 10*sizeof(char));
printf("size of pchangeable : %d\n",sizeof(pchangeable));
schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeable) + 20*sizeof(char));
pchangeable2->iCnt = 20;
printf("pchangeable2->iCnt : %d\n",pchangeable2->iCnt);
strncpy(pchangeable2->pc,"hello world",11);
printf("%s\n",pchangeable2->pc);
printf("size of pchangeable2 : %d\n",sizeof(pchangeable2));
}
size of struct changeable : 4 size of pchangeable : 4 pchangeable2->iCnt : 20 hello world size of pchangeable2 : 4
struct s
{
int a;
char b[] ;
};
char b[] = "hell";也不行(C和C++都不行)
struct s2
{
// char a[] = "hasd" ;
// int c;
};
int main()
{
struct s2 s22;
struct s2 s23;
struct s2 s24;
struct s2 s25;
}
struct s
{
char b[] ;
};
struct s
{
// char b[] ;
};
//对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中A、B也可删,不过最好带着
struct A{
struct B{
int c;
}
b;
}
a;
//使用如下方式访问:
a.b.c = 10;
struct A{
struct B{
int c;
}b;
struct B sb;
}a;
a.b.c = 11;
printf("%d\n",a.b.c);
a.sb.c = 22;
printf("%d\n",a.sb.c);
void func(int); func(a.b.c);
//struct A定义同上
//设立了两个函数,分别传递struct A结构体和其指针。
void func1(struct A a){
printf("%d\n",a.b.c);
}
void func2(struct A* a){
printf("%d\n",a->b.c);
}
main(){
a.b.c = 112;
struct A * pa;
pa = &a;
func1(a);
func2(&a);
func2(pa);
}
printf("size of struct man:%d\n",sizeof(struct man));
printf("size:%d\n",sizeof(Huqinwei));
struct s
{
char a;
short b;
int c;
}
#include<stdio.h>
//直接带变量名Huqinwei
struct stuff{
// char job[20] = "Programmer";
// char job[];
// int age = 27;
// float height = 185;
}Huqinwei;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.ymwmall.com) 版权所有