免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5382 | 回复: 0
打印 上一主题 下一主题

把(void *)的指针格式化成结构体指针数组? [复制链接]

论坛徽章:
0
1 [报告]
发表于 2010-06-14 00:25 |显示全部楼层
本帖最后由 Magic_LP 于 2010-06-14 00:27 编辑

代码本身有问题。你看这样行不行
  1. #include <stdio.h>

  2. #include <string.h>

  3. #define USER_LEN 33

  4. #define PASSWORD_LEN 33

  5. #define ACOUNT_NUM 10

  6. typedef struct _a{

  7.         char        user[USER_LEN];

  8.         char        password[PASSWORD_LEN];

  9.         int         authority;

  10. }Acount_t;

  11. static Acount_t acounts[ACOUNT_NUM];



  12. void fun(void *s)

  13. {

  14.         Acount_t *u;

  15.         u = (Acount_t *)s;

  16.         strncpy(u[0].user, "admin1", USER_LEN);

  17.         strncpy(u[1].user, "admin2", USER_LEN);

  18.         /*或者
  19.      strncpy(u->user, "admin1", USER_LEN);

  20.         strncpy((u+1)->user, "admin2", USER_LEN);
  21.     */

  22. }



  23. int main(int argc, char *argv[])

  24. {

  25.         memset(&acounts, 0, sizeof(Acount_t)*ACOUNT_NUM);

  26.         fun((void *)&acounts);



  27.         printf("acounts[0].user = |%s|,acounts[1].user = |%s|\n",

  28.                         acounts[0].user, acounts[1].user);

  29.         return 0;

  30. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP