标签:style blog http color os io 使用 ar 2014
#include <stdio.h>
#include <string.h>
struct STU
{
int id;
char sex;
};
int main()
{
struct STU s1;
memset(&s1 , 0 , sizeof(s1));
}
#include <stdio.h>
#include <string.h>
struct STU
{
char sex;
int id;
};
int main()
{
struct STU s1;
memset(&s1, 0, sizeof(s1)); //把结构体内存清零
s1.id = 0x12345678;
s1.sex = 0x88;
}
struct STU
{
int id;
// 4字节对齐char name[10];
};
struct STU
{
char id; //最长字节就是char 1个字节
char name[10];
};
struct STU
{
short id;
char name[10];
};
struct STU
{
char * id; // 4
char name[10]; // 12 4字节对齐
};
struct STU
{
char id : 2; // 代表 id 是两个 bit 长
};
#include <stdio.h>
#include <string.h>
struct STU
{
char id : 2; //两个bit位 如果存4的话就会是0 如果存5的话就会是1
};
int main()
{
struct STU s1;
s1.id = 4;
printf("%d\n" , s1.id);
}
struct STU
{
char id : 2;
char name : 2;
};
struct STU
{
int id : 2;
};
struct STU
{
int id : 2;
int age : 2;
};
#include <stdio.h>
#include <string.h>
struct A
{
int i;
char c;
};
struct B
{
int i;
struct A a;
};
int main()
{
printf("%d\n" , sizeof(struct B));
}
#include <stdio.h>
#include <string.h>
struct STU
{
char name[1024];
};
int main()
{
struct STU s1 = { "liuwei" };
struct STU s2;
s2 = s1; // memcpy( &s2 , &s1 , sizeof(s1)); 和这句话效果一样
printf("%s\n",s2.name);
}
#include <stdio.h>
#include <string.h>
struct STU
{
char *name;
};
int main()
{
struct STU s1 = { "liuwei" };
struct STU s2;
s2 = s1;
s1.name = "xuanyuan";
printf("%s\n",s2.name);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct STU
{
char *name;
};
int main()
{
struct STU s1 , s2;
s1.name = malloc(10);
strcpy(s1.name , "liuwei");
s2 = s1;
strcpy(s1.name , "xuanyuan");
printf("%s\n",s2.name);
free(s1.name);
}
#include <stdio.h>
#include <string.h>
struct str
{
char buf[1024];
};
struct str get_str()
{
struct str s;
strcpy( s.buf , "hello world");
return s;
}
int main()
{
struct str tmp = get_str(); //返回的是一个结构体变量,结构体变量之间的赋值,是内存拷贝
printf("%s\n" , tmp.buf);
return 0;
}
#include <stdio.h>
#include <string.h>
char *get_str()
{
char buf[100];
strcpy( buf , "hello world" );
return buf;
}
int main()
{
printf("%s\n" , get_str() ); //输出乱码,因为buf的地址已经被函数释放了
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct str
{
char buf[1024];
};
struct str *get_str()
{
struct str *s = malloc(sizeof( struct str ));
strcpy( s->buf , "hello world");
return s;
}
int main()
{
struct str *tmp = get_str();
printf("%s\n" , tmp->buf);
free(tmp);
return 0;
}
#include <stdio.h>
#include <string.h>
struct str
{
char buf[1024];
};
struct str *get_str()
{
struct str s;
strcpy( s.buf , "hello world");
return &s; //虽然返回的是结构体的地址,但是结构体的内存是在栈上,函数结束被释放
}
int main()
{
struct str *tmp = get_str();
printf("%s\n" , tmp->buf);
return 0;
}
#include <stdio.h>
int main()
{
enum color{ red = 11 , yellow = 10 , blue};
printf("%d\n",blue);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//这个函数是连接两个字符串
const char *func( const char *str1 , const char *str2 )
{
char *p = malloc( strlen(str1) + strlen(str2) + 1 );
strcpy( p , str1);
strcat( p , str2);
return p;
}
//如果要定义这个函数的 函数指针 如下
const char *(*p)( const char *str1 , const char *str2 );
int main()
{
p = func;
char * s = p("hello","world");
printf("%s\n",s);
return 0;
}
typedef const char *(*FUNC)( const char *str1 , const char *str2 );
标签:style blog http color os io 使用 ar 2014
原文地址:http://www.cnblogs.com/l6241425/p/3955053.html