数组指针无法正常工作

时间:2014-05-12 22:12:56

标签: c

所以我有一个96k字的txt文件,全部按顺序排列。我想构建一个函数,用一个字母的最后一个索引构建一个数组。例如,如果" a"从索引0到索引100和" b"在txt文件中从101到150,数组存储{0,100,101,150,...}

问题我没有输出,我认为我的代码是对的。

你能发现它的问题是什么吗?

这就是我所拥有的:

#define PALAVRAS 96067
#define TAMANHO 25

typedef struct dicionario{
  char matrix[PALAVRAS][TAMANHO];
}*DICIONARIO;

void index_dic(DICIONARIO dic,int *lista){    
  int i=0,j;
  char c='a';
  lista[i]=0;

  for (j=0; j<PALAVRAS;j++)    //PALAVRAS is the number of words the txt has
    if (dic->matrix[j][0]!=c){ //if new letter is detected
      i++;                     //increments to next position in array
      lista [i]=j-1;           //stores last position of the last letter
      i++;                     //increments array again
      lista[i]=j;              //stores first position of the new letter
      c++;                     //increments to next letter
    }
}

int main(){
  int i,lista[35], *ponta=lista;
  struct dicionario dic;
  DICIONARIO x = &dic;
  FILE *fp;

  fp=fopen("dicio.txt", "r");
  for (i=0;i<PALAVRAS;i++)
    fgets (dic.matrix[i], TAMANHO,fp); //reads txt to matrix
  index_dic(x,ponta); //runs function with pointers as parameters
  for (i=0;lista[i]!='\0';i++)
    printf ("%d\n", lista[i]); //to print all the elements of the array
  fclose(fp);
}

1 个答案:

答案 0 :(得分:2)

此for循环不会打印任何内容。

for (i=0;lista[i]!='\0';i++)
  printf ("%d\n", lista[i]);

由于您要在list[0]的以下行中将0设为index_dic

lista[i]=0;

<强>更新

list[i] != '\0'list[i] != 0相同。

使用list[i] != '\0'形式检查空终止的C字符串何时结束。 list不是那样的。它是一个整数数组。您必须找到一种不同的机制来检查整数列表何时结束。