【问题标题】:2D array using pointers使用指针的二维数组
【发布时间】:2010-10-12 10:07:06
【问题描述】:

我正在尝试读取格式为“ADAM”、“MARK”、“JESSIE”的随机名称的文件.....

我有一些限制,文件应该在函数中读取,但应该可以从没有全局变量的主函数中访问。 文件大小和文件中的名称数量未知。 这就是我到目前为止所做的。我对动态二维数组有一些困难,因为我没有经常使用它们。

/* Function to read from the file */
int read_names(FILE *input, char ***names, int *name_count)
{
    int f1,size,count,i,j=0;
    char **name_array,*text,pos=0;
    /* get the file size */
    f1=open("names.txt",O_RDONLY);
    size=lseek(f1,0,SEEK_END);
    close(f1);
    /* Reading all the characters of the file into memory */
    //Since file size is known we can use block transfer
    text=(char *) malloc(size * sizeof(char) );
    fscanf(input,"%s",text);

    /* Finding the no of names in the file */
    for(i=0;i<size;i++)
    {
        if(text[i]==',')
            count++;
    }
    printf("No. of names determined\n");

    /* Assigning the Name count to the pointer */
    name_count=(int*)malloc(sizeof(int));
    *name_count=count;


    name_array=(char **) malloc(count * sizeof(char *));
    for(i=0;i<count;i++)
    {
        name_array[i]=(char*) malloc(10 *sizeof(char ));
    }
    for(i=0;i<size;i++)
    {
        if(text[i]!='"')
            if(text[i]==',')
            {
                **name_array[pos][j]='\0'; //error here
                pos++;
                j=0;
            }
            else
                name_array[pos][j++]=text[i];
    }
    printf("Names Counted\n");
    printf("Total no of names: %d\n",*name_count);
    names=(char ***) malloc(sizeof(char **);
    names=&name_array;
    return 1;
}

/* Main Function */
int main(int argc, char *argv[])
{
    FILE *fp;
    char ***names;
    int *name_count;
    int status;
    // Opening the file
    fp = fopen("names.txt","r");
    // Now read from file
    status = read_names(fp,names,name_count);
    printf("From Main\n");
    fclose(fp);
    system("PAUSE");
    return 0;
}

当我尝试分配空字符时,我使用 WxDev 并收到错误“'unary *' 的类型参数无效。

关于如何做到这一点的任何指示?

【问题讨论】:

  • 您可能想尝试格式化您的代码,因为其他人很难以现在的形式阅读。
  • 您打开“names.txt”两次的事实应该向您表明存在某种问题。
  • 函数read_names中char*** name有什么用?
  • @Paul:我会改进 cmets...@sje397:嗯... din 真的考虑一下,因为我没有收到警告或错误,而且我以只读方式打开它们认为我没有收到任何错误。 @Manoj R:我现在编辑了代码以指示使用三重指针。我以为我可以通过这种方式在主函数中访问这个二维数组。

标签: c pointers dynamic multidimensional-array 2d


【解决方案1】:

从产生错误的那一行仔细观察这个表达式,并思考它在做什么:

**name_array[pos][j]

请记住,方括号的 precedence 比一元 * 更高,这相当于 *(*((name_array[pos])[j])),即 2 个下标后跟 2 个取消引用。总共有 4 次取消引用,因为 foo[i] 等同于 *(foo+i)name_array 的声明类型是char **,这意味着您只能取消引用它两次。将类型声明 char **name_array 视为意味着 **name_array 具有类型 char,因为这是类型声明语法的基础(参见 History of C,“Embryonic C”)。

题外话

线上又出现了一个问题:

name_array[i]=(char*) malloc(sizeof(char ));

在这里,您只需为 name_array 的每个元素中的单个字符分配足够的空间。

【讨论】:

  • 感谢您指出引用和内存分配中的错误。我会在这个主题上做更多的阅读。
【解决方案2】:
            **name_array[pos][j]='\0'; \\error here

我看到name_array 被声明为

char **name_array

问题是

**name_array[pos][j] 尝试取消引用(两次!!)一个字符

**   (name_array[pos]) [j];    /* name_array[pos] is of type (char*) */
** ( (name_array[pos]) [j] );  /* name_array[pos][j] is of type (char) */

你不能取消引用一个字符。

建议:简化你的代码。
当代码没有“表现”时,通常不是因为它太简单了 ;)

【讨论】:

  • 感谢您的解释。我会在这个主题上做更多的阅读。
【解决方案3】:

这可能看起来很迂腐,但它很重要。您没有使用二维数组。事实上,您不会在代码中创建 ANY 数组。

要理解的重要一点是数组“衰减”为指针。

link text

C 中围绕数组和指针的许多混淆可以追溯到对这句话的误解。说数组和指针是 equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it'spointer 算术和数组索引 [that] 在 C 中是等价的,指针和数组是不同的。'')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多