我定义了一个结构包括三个元素,char *a,char *b,char *c;
我需要写的函数是对其中的元素进行判断。如果符合条件就复制一份,然后返回新的结构指针。这个函数怎么写啊?

解决方案 »

  1.   

    typedef struct {char *a; char *b; char *c;} my_struct;my_struct* my_function (const my_struct *param)
    {
        if (....) {
            my_struct *temp = (my_struct *)malloc(sizeof(my_struct));
            if (!temp) return NULL;
            // TODO: copy the struct
            return temp;
        } else {
            return NULL;
        }
    }
      

  2.   

    怎么写都行吧。
    typedef struct tagTFRAME
    {
    IP tag;
    char type;
    char cparam;
    short len;
    char data;
    }TFRAME;
    typedef union tagFRAME
    {
    TFRAME frame;
    char bytes[ sizeof( TFRAME ) ];
    }FRAME;FRAME CLakerDlg::GetFrame(char * buf)
    {
    FRAME temp_frame;
    memcpy( temp_frame.bytes, ( const void * )( buf + 12 ), sizeof( FRAME ) );
    temp_frame.frame.tag.longs = ( int )ntohl( temp_frame.frame.tag.longs );
    temp_frame.frame.len = ntohs( temp_frame.frame.len ); return temp_frame;
    }上边的是返回结构体的
    楼上的是返回结构体指针的
    typedef可以不用