用C创建数组并传递指针所述阵列的功能 [英] Creating Array in C and passing pointer to said array to function

查看:162
本文介绍了用C创建数组并传递指针所述阵列的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读C.这的确帮助我减少失误我与我的问题,几个职位。不过,我仍然具有其他职位不能为我解决问题。基本上,这就是我要做的。

定义主数组。我在一个指针传递给该数组的功能。此功能将打开一个文件,解析文件,并把信息从该文件到其指针我通过在数组中。那么,它会失败。

我得到的错误是:

  work.c:12:错误:数组类型具有不完整的元素类型
work.c:在函数'主':
work.c:20:错误:类型形式参数1不完整
work.c:在顶层:
work.c:25:错误:数组类型具有不完整的元素类型

整个code如下所示。但我认为你只需要专注于我如何定义我的数组和指针,等等。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;//定义preprocessed功能
字符ReadFile的(char数组[] [],INT,INT);
//无效displayStudentInfo为(int *);//实现学生Instuctions的步骤1和2
INT主(INT ARGC,CHAR *的argv [])
{
    INT X = 256;
    INT Y = 256;
    字符数组[X] [Y]
    ReadFile的(安培;阵列,X,Y);
    // displayStudentInfo(安培;数组);
    返回0;
}字符ReadFile的(char数组[] [],诠释的x,int y)对
{
    焦线[256]; //三列0,1,2对应的名字,姓氏,得分。
    字符*串;
    INT列= 3;    X = 0;
    // INTÿ; //定义二维数组的行和列以存储数据
    //字符数组[X] [Y] //定义存储名字,姓氏和得分数组    FILE *文件;
    文件= FOPEN(input.txt的,R);    //测试,以确保文件可以打开    如果(文件== NULL)
    {
        的printf(错误:无法打开文件\\ n);
        出口(1);
    }
    其他
    {
        而(!的feof(文件))
        {
          / *
            如果(与fgets(行,256文件))//读取与fgets高达NUM-1从线路流,并将它们存储字符
            {
                的printf(%S,行);
            }
            * /
            如果(与fgets(线,256,文件)!= NULL)
            {
                对于(Y = 0; Y<列; Y ++)
                {
                    数组[X] [Y] = strtok的(与fgets(行,256,文件),);
                }
                X ++;
            }
        }
    }
    FCLOSE(文件);
}


解决方案

您有几个问题。前两个是相似的:

首先,您使用的是无限的数组中的函数声明:编译器需要知道更多有关的说法,即尺寸。在这种情况下,它是足以提供的尺寸之一:

 字符READFILE(char数组[] [NUM_Y],INT,INT);

现在的编译器有足够的信息来处理数组。你可以离开了这样一个层面,但它通常最好是明确的,并且声明该函数为:

 字符READFILE(char数组[NUM_X] [NUM_Y],INT,INT);

接下来,当你声明你阵列阵主,你需要更具体的了解维度 - 类似于函数的参数列表:

 字符数组[X] [NUM_Y];

选择 NUM_Y 比大更适合足够,你会期望的数据量。

接下来,你没有初始化 X ,然后你去上宣布使用这些变量数组。这是不好的,因为这些变量可以包含任何价值的垃圾,包括 0 ,所以你会意想不到的尺寸/大小的数组结束。

最后,当你的数组传递给你的函数,不要去引用它,只是沿着变量传递:

  READFILE(数组,X,Y);

在C,当你传递一个数组给一个函数,什么是真正传递是一个指向第一个元素。这意味着,该阵列不被复制,因此该函数可访问的存储器,它预期更改的区域。我猜,你去引用,因为这是你已经学会通过要在功能改变简单的类型,如整数或方式结构,但与数组,你不必这样做。

I have read several posts related to my question on C. This indeed help me cut down on my errors. However, I'm still having problems that other posts couldn't solve for me. Basically, this is what I am trying to do.

Define an array in main. I pass in a pointer to this array to a function. This function will open up a file, parse that file, and put information from that file into the array whose pointer I passed in. Well, it fails.

The errors I get are:

work.c:12: error: array type has incomplete element type
work.c: In function ‘main’:
work.c:20: error: type of formal parameter 1 is incomplete
work.c: At top level:
work.c:25: error: array type has incomplete element type

The entire code follows below. But I think you only need to focus on how I defined my array, and pointer, and so on.

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

//Defining Preprocessed Functions 
char readFile(char array[][], int, int);
//void displayStudentInfo(int*);

//Implements Step 1 and 2 of Student Instuctions
int main(int argc, char* argv[])
{
    int x = 256;
    int y = 256;
    char arrays[x][y]; 
    readFile(&arrays,x,y);
    //displayStudentInfo(&array);
    return 0;
}

char readFile(char array[][], int x, int y)
{
    char line[256]; //Three columns 0, 1, 2 corresponds to firstname, lastname, score. 
    char* string;
    int columns = 3;

    x = 0;
    //int y; //Defines rows and columns of 2D array to store data
    //char array[x][y]; //Defines the array which stores firstname, lastname, and score



    FILE *file;
    file = fopen("input.txt", "r");

    //Test to make sure file can open 

    if(file  == NULL)
    {
        printf("Error: Cannot open file.\n");
        exit(1);
    }
    else
    {
        while(!feof(file))
        {
          /* 
            if(fgets(line, 256, file))//fgets read up to num-1 characters from stream and stores them in line
            {
                printf("%s", line);
            }
            */
            if(fgets(line,256,file)!=NULL)
            {
                for(y = 0; y < columns; y++)
                {
                    array[x][y]=strtok(fgets(line,256,file), " ");
                }
                x++;
            } 
        }
    }
    fclose(file);
}

解决方案

You have a few problems. The first two are similar:

First, you're using an unbounded array in your function declaration: the compiler needs to know more about the argument, i.e. the dimensions. In this case it's sufficient to provide one of the dimensions:

char readFile(char array[][NUM_Y], int, int);

Now the compiler has enough information to handle the array. You can leave off a dimension like this, but it's usually better to be explicit, and declare the function as:

char readFile(char array[NUM_X][NUM_Y], int, int);

Next, when you're declaring your arrays array in main, you'll need to be more specific about dimensions - similar to the argument list for the function:

char arrays[x][NUM_Y];

Choose NUM_Y to be more than large enough fit the amount of data that you'll expect.

Next, you're not initializing x and y in main, then you go on to declare an array using those variables. This is bad because these variables can contain any garbage value, including 0, and so you'll end up with an array of unexpected dimensions/size.

Finally, when you pass the array to your function, don't de-reference it, just pass along the variable:

readFile(arrays, x, y);

In C, when you pass an array to a function, what is actually passed is a pointer to the first element. This means that the array isn't copied, and so the function has access to the area of memory that it expects to change. I'll guess that you're de-referencing because that's the way that you've learned to pass simpler types that you want to change in the function, like ints or structs, but with arrays you don't need to do this.

这篇关于用C创建数组并传递指针所述阵列的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆