免费注册 查看新帖 |

Chinaunix

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

使用了无效的柔性数组成员 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-02 15:10 |只看该作者 |倒序浏览
    这是我写的查字典程序。但是编译的时候卡在invalid use of flexible array member这个问题上
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. #define MAX_SIZE 128
  5. #define BUFFER_SIZE 4096
  6. #define WORD_LEN 100

  7. typedef struct {

  8.         char* key_word;
  9.         int n_trans;
  10.         char* trans[];
  11. }word_t;

  12. word_t *dict = NULL;

  13. int search(word_t* dict, int start, int end, char* buf){

  14.         int mid = (start+end)/2;

  15.         if(start > end)

  16.                 return -1;
  17.         if(strcmp(dict[mid].key_word, buf) == 0)

  18.                 return mid;
  19.         else if(strcmp(dict[mid].key_word, buf) == -1)

  20.                 return search(dict, mid+1, end, buf);
  21.         else

  22.                 return search(dict, start, mid-1, buf);
  23. }

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

  25.         int num = 0;
  26.         int len,n,i,j,len_trans;
  27.         char str[100];
  28.         char buf[MAX_SIZE];
  29.         char* p=NULL;
  30.         int id;
  31.         FILE* dict_fp=NULL, *bin=NULL;
  32.        
  33.        
  34.         if((dict_fp = fopen("dict.txt","r")) == NULL){

  35.                 perror("fopen dict");
  36.                 return -1;
  37.         }

  38.         if((bin = fopen("dict.dat","wb+")) == NULL){

  39.                 perror("fopen dict.dat");
  40.                 return -1;
  41.         }

  42.         while(fgets(buf, MAX_SIZE, dict_fp) != NULL){

  43.                 if(buf[0] == '#'){

  44.                         num++;       
  45.                 }
  46.         }
  47.        
  48.         printf("there are %d words\n", num);
  49.         fwrite(&num, sizeof(int), 1, bin);
  50.         rewind(dict_fp);

  51.         while(fgets(buf, MAX_SIZE, dict_fp)!=NULL){

  52.                 if(buf[0] == '#'){
  53.                        
  54.                         len = strlen(buf)-2;
  55.                         fwrite(&len, sizeof(int), 1, bin);
  56.                         fwrite(&buf[1], len, 1, bin);               
  57.                 }else{
  58.        
  59.                         n = 0;
  60.                         buf[len-1]='@';

  61.                         for(i=0; buf[i]!='\0'; i++){

  62.                                 if(buf[i]=='@')
  63.                                         n++;
  64.                         }
  65.                         fwrite(&n, sizeof(int), 1, bin);
  66.                         p = &buf[6];
  67.                        
  68.                         for(i=0; buf[i]!='\0'; i++){

  69.                                 if(buf[i]=='@')
  70.                                         buf[i] = '\0';
  71.                         }
  72.                        
  73.                         for(i=0; i<n; i++){

  74.                                 len_trans = strlen(p);
  75.                                 fwrite(&len_trans, sizeof(int), 1, bin);       
  76.                                 fwrite(p, len_trans, 1, bin);       
  77.                                 p = p+strlen(p)+1;
  78.                                
  79.                         }
  80.                 }
  81. }
  82.                 close(dict_fp);
  83.                 printf("init dict.dat is done....\n");

  84.                 rewind(bin);

  85.                 fread(&num, sizeof(int), 1, bin);
  86.                 dict = (word_t*)malloc(sizeof(word_t) * num);

  87.                 for(i=0; i<num; i++){

  88.                         fread(&len, sizeof(int), 1, bin);
  89.                         dict[i].key_word = (char*)malloc(sizeof(char)*(len + 1));

  90.                         fread(dict[i].key_word, len, 1, bin);
  91.                         dict[i].key_word[len] = '\0';

  92.                         fread(&dict[i].n_trans, sizeof(int), 1, bin);
  93.                         [color=Red]dict[i].trans = (char**)malloc(sizeof(char*) * dict[i].n_trans);//出现问题的地方[/color]

  94.                         for(j=0; j<dict[i].n_trans; j++){

  95.                                 fread(&len_trans, sizeof(int), 1, bin);
  96.                                 dict[i].trans[j] = (char*)malloc(sizeof(char) * (len_trans+1));
  97.        
  98.                                 fread(dict[i].trans[j], len_trans, 1, bin);
  99.                                 dict[i].trans[j][len_trans]='\0';
  100.                         }       
  101.                        
  102.                 }

  103.                 printf("init dict....\n\n");

  104.                 while(1){

  105.                         printf("please input the word you want to find out, input '_quit' to quit\n");

  106.                         fgets(str, WORD_LEN, stdin);
  107.                        
  108.                         str[strlen(str)-1] = '\0';

  109.                         if(strcmp(str,"_quit") == 0)
  110.                                
  111.                                 return 0;
  112.                         else

  113.                         id = search(dict, 0, num-1, str);

  114.                         if(id == -1)
  115.                        
  116.                                 printf("fail to find out\n");

  117.                         else
  118.                         {
  119.                                 printf("%s\n",dict[id].key_word);
  120.                                 printf("Trans:");
  121.                                 for(i=1; i<=dict[id].n_trans; i++){
  122.        
  123.                                         printf(" %d %s",i , dict[id].trans[i]);

  124.                                 }
  125.                                 putchar('\n');
  126.                         }
  127.                 }

  128. //                close(bin);       
  129.                 return 0;
  130.        
  131. }
复制代码

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
2 [报告]
发表于 2012-07-02 15:25 |只看该作者
  1. dict[i].trans = (char*)malloc(sizeof(char*) * dict[i].n_trans)
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-07-02 16:59 |只看该作者
不对吧, 这个trans是个二维数组,存储多个解释回复 2# cdtits


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP