码迷,mamicode.com
首页 > 编程语言 > 详细

c语言中的指针,数组和结构体结合的一个经典案例

时间:2020-04-20 13:34:32      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:pre   函数   std   unsigned   span   遇到   宏定义   函数指针   fine   

一 你真正懂了C语言了吗?

   很多人刚把c语言用了两年,就以为很懂,等遇到稍微深层次一点的问题,就卡住了。这里,有一个问题,可以考察你对这三者理解如何。

二 一个例子:

 

#include <stdio.h>

typedef unsigned char uint8_t;

typedef struct {
    uint8_t ssid[32];           /**< SSID of ESP8266 soft-AP */
    uint8_t password[64];       /**< Password of ESP8266 soft-AP */
} wifi_ap_config_t;

typedef struct {
    uint8_t ssid[32];      /**< SSID of target AP*/
    uint8_t password[64];  /**< password of target AP*/
} wifi_sta_config_t;


typedef union {
    wifi_ap_config_t  ap;  /**< configuration of AP */
    wifi_sta_config_t sta; /**< configuration of STA */
} wifi_config_t;

#define SSID "test"
#define PASSWD "1234567890"

int main()
{
        wifi_config_t wifi_config = { 
        .sta = { 
            .ssid = SSID,
            .password = PASSWD
        },
    };  

    printf("hello world");
}

 

 

  在你没有运行代码之前,能够看出来这个例子能够运行正确吗?有啥语法问题吗?

 

 

三 拓展问题

 

   1 把这个结构体换成一个从数组中获取ssid和passwd,这个该怎么解决呢?写出你的代码。

 

   2 把这个宏定义换成一个函数指针,从函数指针中获取ssid和passwd,这个代码该怎么改呢?写出你的代码。

   假如你这两个都弄对了,恭喜你,说明你的c语言基础还真是不错的。

 

 

   

c语言中的指针,数组和结构体结合的一个经典案例

标签:pre   函数   std   unsigned   span   遇到   宏定义   函数指针   fine   

原文地址:https://www.cnblogs.com/dylancao/p/12736989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!