C/C+ÓïÑÔstructÉî²ã̽Ë÷ (ת)
C/C+ÓïÑÔstructÉî²ã̽Ë÷
×÷ÕߣºËᦻª ³ö´¦£ºPConline
1. structµÄ¾Þ´ó×÷ÓÃ
¡¡¡¡Ãæ¶ÔÒ»¸öÈ˵ĴóÐÍC/C++³ÌÐòʱ£¬Ö»¿´Æä¶ÔstructµÄʹÓÃÇé¿öÎÒÃǾͿÉÒÔ¶ÔÆä±àдÕߵıà³Ì¾Ñé½øÐÐÆÀ¹À¡£ÒòΪһ¸ö´óÐ͵ÄC/C++³ÌÐò£¬ÊƱØÒªÉ漰һЩ(ÉõÖÁ´óÁ¿)½øÐÐÊý¾Ý×éºÏµÄ½á¹¹Ì壬ÕâЩ½á¹¹Ìå¿ÉÒÔ½«Ô±¾ÒâÒåÊôÓÚÒ»¸öÕûÌåµÄÊý¾Ý×éºÏÔÚÒ»Æð¡£´ÓijÖ̶ֳÈÉÏÀ´Ëµ£¬»á²»»áÓÃstruct£¬ÔõÑùÓà structÊÇÇø±ðÒ»¸ö¿ª·¢ÈËÔ±ÊÇ·ñ¾ß±¸·á¸»¿ª·¢¾ÀúµÄ±êÖ¾¡£
¡¡¡¡ÔÚÍøÂçÐÒ顢ͨÐÅ¿ØÖÆ¡¢Ç¶ÈëʽϵͳµÄC/C++±à³ÌÖУ¬ÎÒÃǾ³£Òª´«Ë͵IJ»ÊǼòµ¥µÄ×Ö½ÚÁ÷£¨charÐÍÊý×飩£¬¶øÊǶàÖÖÊý¾Ý×éºÏÆðÀ´µÄÒ»¸öÕûÌ壬Æä±íÏÖÐÎʽÊÇÒ»¸ö½á¹¹Ìå¡£
¡¡¡¡¾Ñé²»×ãµÄ¿ª·¢ÈËÔ±ÍùÍù½«ËùÓÐÐèÒª´«Ë͵ÄÄÚÈÝÒÀ˳Ðò±£´æÔÚcharÐÍÊý×éÖУ¬Í¨¹ýÖ¸ÕëÆ«ÒƵķ½·¨´«ËÍÍøÂ籨ÎĵÈÐÅÏ¢¡£ÕâÑù×ö±à³Ì¸´ÔÓ£¬Ò׳ö´í£¬¶øÇÒÒ»µ©¿ØÖÆ·½Ê½¼°Í¨ÐÅÐÒéÓÐËù±ä»¯£¬³ÌÐò¾ÍÒª½øÐзdz£Ï¸ÖµÄÐ޸ġ£
¡¡¡¡Ò»¸öÓоÑéµÄ¿ª·¢ÕßÔòÁé»îÔËÓýṹÌ壬¾ÙÒ»¸öÀý×Ó£¬¼ÙÉèÍøÂç»ò¿ØÖÆÐÒéÖÐÐèÒª´«ËÍÈýÖÖ±¨ÎÄ£¬Æä¸ñʽ·Ö±ðΪpacketA¡¢packetB¡¢packetC£º
struct structA
{
int a;
char b;
};
struct structB
{
char a;
short b;
};
struct structC
{
int a;
char b;
float c;
}
¡¡¡¡ÓÅÐãµÄ³ÌÐòÉè¼ÆÕßÕâÑùÉè¼Æ´«Ë͵ı¨ÎÄ£º
struct CommuPacket
{
int iPacketType;¡¡¡¡//±¨ÎÄÀàÐͱêÖ¾
union¡¡¡¡¡¡¡¡¡¡¡¡//ÿ´Î´«Ë͵ÄÊÇÈýÖÖ±¨ÎÄÖеÄÒ»ÖÖ£¬Ê¹ÓÃunion
{
&nbs
Ïà¹ØÎĵµ£º
#include <stdio.h>
struct Foo1
{
char a;
int b;
char c;
int d;
};
#pragma pack (2)
struct Foo2
{
char a;
int b;
char c;
int d; ......
ÑϸñÀ´Ëµ£¬ÎÞ·¨´Óº¯Êý·µ»ØÒ»¸öÊý×飬µ«¿ÉÒÔ´Óº¯Êý·µ»ØÒ»¸öÖ¸ÏòÈκÎÊý¾Ý½á¹¹µÄÖ¸Õ룬°üÀ¨Ò»¸öÖ¸ÏòÊý×éµÄÖ¸Õë¡£
Ò»ÖÖ·½Ê½ÈçÏ£º
#include <stdio.h>
#include <stdlib.h>
int (*func())[20];//funcÊÇÒ»¸öº¯Êý£¬Ëü·µ»ØÒ»¸öÖ¸Ïò°üÀ¨20¸öintÔªËصÄÊý×éµÄÖ¸Õë
int main(void)
{
  ......
ʹÓà gcc ÔÚ ubuntu Ï ±àÒë Ò»¸ö C³ÌÐò £º
µÚÒ»²½Ö裺´ò¿ª ÖÐ¶Ë £¨Õâ¸ö ÖªµÀ°É£¿£©
ÊäÈë sudo gedit no.c (no.cΪCÎļþÃû×Ö)
sudo ±íʾÄãÊäÈë ......
ÒýÑÔ
¡¡¡¡Ö¸ÕëÊÇC/C++ÓïÑÔµÄÌØÉ«£¬¶øÊý×éÃûÓëÖ¸ÕëÓÐÌ«¶àµÄÏàËÆ£¬ÉõÖÁºÜ¶àʱºò£¬Êý×éÃû¿ÉÒÔ×÷ΪָÕëʹÓá£ÓÚÊǺõ£¬ºÜ¶à³ÌÐòÉè¼ÆÕ߾ͱ»¸ãºýÍ¿ÁË¡£¶øÐí¶àµÄ´óѧÀÏʦ£¬ËûÃÇÔÚCÓïÑԵĽÌѧ¹ý³ÌÖÐÒ²´íÎóµÃ¸øѧÉú½²½â£º"Êý×éÃû¾ÍÊÇÖ¸Õë"¡£ºÜÐÒÔË£¬ÎҵĴóѧÀÏʦ¾ÍÊÇÆäÖÐÖ®Ò»¡£Ê±ÖÁ½ñÈÕ£¬ÎÒÈÕ¸´Ò»ÈյؽøÐÐ×ÅC/C++ÏîÄ¿µÄ¿ª·¢£¬¶øÉí±ß» ......
CÖÐÈçºÎµ÷ÓÃC++º¯Êý?
Ç°Õó×Ó±»Îʼ°Ò»¸öÔÚCÖÐÈçºÎµ÷ÓÃC++º¯ÊýµÄÎÊÌ⣬µ±Ê±¼òµ¥»Ø´ðÊǽ«º¯ÊýÓÃextern "C"ÉùÃ÷£¬µ±±»Îʼ°ÈçºÎ½«ÀàÄÚ³ÉÔ±º¯ÊýÉùÃ÷ʱ£¬Ò»Ê±ÓïÈû£¬ºóÀ´ÍøÉϲéÁËÏ£¬ÍøÉÏÓÐÒ»·ÒëC++Ö®¸¸µÄÎÄÕ¿ÉÒÔ×÷Ϊ½â´ð£¬ËìÄÃÀ´Markһϡ£
½« C++ º¯ÊýÉùÃ÷Ϊ``extern "C"''£¨ÔÚÄãµÄ C++ ´úÂëÀï×öÕâ¸öÉùÃ÷£©£¬ ......