C在堆数组中搜索字符串

时间:2014-11-06 12:48:33

标签: c arrays char malloc heap

我使用以下代码在堆上加载大型哈希表。 但是我不知道在加载后搜索整个数组的正确语法。

我想我可以在最后一个J循环中添加一个strcmp ??

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int lines_allocated = 128;
    int max_line_len = 100;

    /* Allocate lines of text */
    char **words = (char **)malloc(sizeof(char*)*lines_allocated);
    if (words==NULL)
    {
        fprintf(stderr,"Out of memory (1).\n");
        exit(1);
    }

    FILE *fp = fopen("hashtable.txt", "r");

    if (fp == NULL)
    {
        fprintf(stderr,"Error opening file.\n");
        exit(2);
    }

    int i;

    for (i = 0; 1; i++)
    {
        int j;

        /* Have we gone over our line allocation? */
        if (i >= lines_allocated)
        {
            int new_size;

            /* Double our allocation and re-allocate */
            new_size = lines_allocated*2;
            words = (char **)realloc(words,sizeof(char*)*new_size);

            if (words == NULL)
            {
                fprintf(stderr,"Out of memory.\n");
                exit(3);
            }

            lines_allocated = new_size;
        }

        /* Allocate space for the next line */
        words[i] = malloc(max_line_len);

        if (words[i] == NULL)
        {
            fprintf(stderr,"Out of memory (3).\n");
            exit(4);
        }

        if (fgets(words[i], max_line_len-1,fp) == NULL)
            break;

        /* Get rid of CR or LF at end of line */
        for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r')j--);
            words[i][j] = '\0';
        }

    int j;
    for(j = 0; j < i; j++)
    printf("%s\n", words[j]);
    // Search for a string e.g "ffffffffff999999999922222222227777777777" in words[]
    //
    //strcmp ( string, words[j])????
    //
    //
    //
    /* Good practice to free memory */

    for (;i>=0;i--)
        free(words[i]);

    free(words);

    return 0;
}

我试图在循环中实现strcmp但是然后程序段错误。 使用此示例:

/* what is i? the number of items used in the array? */
for(x = 0; x < i; x++) {
    if ( strcmp( new_name, names[x] ) == 0 ){
        /* match, x is the index */
        return x;
    }
}
/* here with no match */
return -1;

2 个答案:

答案 0 :(得分:1)

当我缩进你的代码时,我看到了:

for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r')j--);

我认为你的意思是:

for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r'); j--)

----^^^^^^^

那时虽然永远不会执行大括号之间的内容。

答案 1 :(得分:1)

您在此处遇到问题:

for (j=strlen(words[i]) - 1; j>=0 && (words[i][j]=='\n' || words[i][j]=='\r'); j--);
words[i][j]='\0';

j一个人关闭。你应该在循环之后递增j

for (j=strlen(words[i])-1; j>=0 && (words[i][j]=='\n' || words[i][j]=='\r'); j--)
{
}

words[i][j + 1] = '\0';

额外{ }仅用于可读性目的。否则,如果您忘记;之后的for,您的代码将正确编译,但words[i][j +1 ]='\0';成为循环的一部分。

其他一个问题:

您必须在此处递减i

/* Good practice to free memory */
i-- ;  // <<<< decrement i here
for (;i>=0;i--)
  free(words[i]);

strcmp问题:

关于您的strcmp问题,您可能需要这样做:

int j;
for(j = 0; j < i; j++)
{
  printf("%s\n", words[j]);

  if (strcmp (words[j], "word we are lookong for") == 0)
  {
     // found it
  }
}