Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1120119
  • 博文数量: 91
  • 博客积分: 10053
  • 博客等级: 上将
  • 技术积分: 1335
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-01 12:46
文章分类
文章存档

2011年(4)

2010年(22)

2009年(22)

2008年(43)

分类: C/C++

2008-04-11 12:37:03

结构体的定义:

typedef struct
{   
     char ip[30];    
     char usb[30];
}xiaoshou_cmd;


结构体的初始化:
    

xiaoshou_cmd *conf,tem;   
strcpy(tem.ip,"xiaoshou");    
printf("ip= %s\n",tem.ip);     strcpy(tem.usb,"xiaoshou");    
printf("ip= %s\n",tem.usb);

结构体的指针初始化:
xiaoshou_cmd *conf,tem;   
conf=&tem;         //结构体指针必须实例化,不然会出现莫名的错误! strcpy(conf->ip,"xiaoshou");    
printf("ip= %s\n",conf->ip);
strcpy(conf->usb,"xiaoshou"); 
printf("ip= %s\n",conf->usb);

测试程序:
/××××××××将命令行输入的参数传递到主函数中×××××××××××××××××/
#include
#include
#include

typedef struct
{    char ip[30];
      char usb[30];
      int help_flag;
}xiaoshou_cmd;

xiaoshou_cmd * get_cmd_conf1(int argc,char  *argv[]);
int main(int argc,char  *argv[])
{
    printf("argc= %d \n",argc);
    xiaoshou_cmd *conf,tem;
    conf=&tem;
    opterr = 0;
    conf=get_cmd_conf1(argc,argv);
    if (conf->help_flag==1)
    {
        printf("-i:   server ip!\n");
        printf("-u:   device usb!\n");
        printf("-h:    help!\n");
    }
    else
    {
        printf("ip= %s\n",conf->ip);
        printf("usb= %s\n",conf->usb);
    }

    return 0;
}

xiaoshou_cmd * get_cmd_conf1(int argc,char  *argv[])
{
    int result;
    xiaoshou_cmd *res;
    opterr = 0;
    while((result=getopt(argc,argv,"i:u:h"))!=-1)
    {
        switch(result)
        {
            case 'u':
                strcpy(res->usb,optarg);
                break;
            case 'i':
                strcpy(res->ip,optarg);
                break;
            case 'h':
                res->help_flag=1;
                break;
        }
    }
    return res;
}


阅读(3020) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~