1

从下面的C代码我无法理解

struct word *nowhead = head[string[start - 'a']];  

请解释一下。

struct word **head=NULL;
char string[16384];
start = 0;
...

void insert(char *string, int end, int start)
{
    struct word *nowhead = head[string[start - 'a']]; //this!!
    ...
}

谢谢。

更新(来自评论)

void insert(char *string, int end, int start)
{
struct word *nowhead = head[string[start- 'a']];
int i, j=0, on=0;
char *wtemp;
struct word *temp1, *temp2;

wtemp= calloc(PAROLA, sizeof(char));
if(wtemp==NULL) printf("Error \n");
for(i=start; i<end; i++) {
  wtemp[j]=string[i]; j++;
  }

if(nowhead != NULL) {
  temp1=nowhead ;
  while(temp1!=NULL) {
    if(strncmp(wtemp, temp1->parol, (PAROLA-1))== 0) {
      temp1->occorrenz++; on=1; break;
    }
    else {
      if(temp1->next == NULL) {
        temp2=temp1; }
      } temp1=temp1->next;
    }
  if(on!=1) {
    temp1=malloc(sizeof(struct word));
    strncpy((temp1->parola), wtemp, (PAROLA-1));
    temp1->next = NULL;
    temp1->occorrenze=1;
    temp2->next=temp1; }
  } else {
    nowhead= malloc(sizeof(struct word));
    strncpy((nowhead->parola), wtemp, PAROLA);
    nowhead->next=NULL; nowhead->occorrenz=1;
  }
 free(wtemp);
 }
4

2 回答 2

2

您没有为上下文显示足够的代码,但标记的行只是使用在另一个数组中找到的索引访问一个数组。您可以将其分解为两个操作:

char headIndex = string[start - 'a'];
struct word *nowhead = head[headIndex];
于 2013-09-11T23:31:53.720 回答
0

这意味着将指针 nowhead 设置为嵌套数组中的值减去 char 'a' 的值

于 2013-09-11T23:33:38.497 回答