Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将结构数组传递给另一个函数_C_Pointers_Struct - Fatal编程技术网

C 如何将结构数组传递给另一个函数

C 如何将结构数组传递给另一个函数,c,pointers,struct,C,Pointers,Struct,我试图将结构数组作为指针传递给另一个函数。但是编译器拒绝了 我建立了这个代码 #include <stdio.h> #include <stdlib.h> #include <string.h> struct Values{ char timestamp[21]; char temperature[2]; int tmp; }; char *readString(char out[], FILE *fp){// Reading an

我试图将结构数组作为指针传递给另一个函数。但是编译器拒绝了

我建立了这个代码

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

struct Values{
    char timestamp[21];
    char temperature[2];
    int tmp;
};

char *readString(char out[], FILE *fp){// Reading and storing the input values, out = the string that this func returns
    int ch, i;
    while(EOF!=(ch=fgetc(fp)))
        if(ch == '"') break;

    for(i=0;EOF!=(ch=fgetc(fp));++i){
        if(ch == '"') break;
        out[i] = ch;
    }

    out[i]='\0';
    return out;
}

void printValues(struct Values * v, int i){ //just a printing method, for printing the values, i = the amount of values I have
    int j;
    for(j=0; j<i; j++){
        printf("%s \t : \t %s \t :\t %d \n\n", v[j]->timestamp, v[j]->temperature, v[j]->tmp);
    }
}

void makeTmpIntegers(struct Values values[], int i){ //making temperatures integers so I can use them in sorts, i = the amount of values I have
    int j;
    for(j=0; j<i;j++){
        values[j].tmp = atoi(values[j].temperature);
    }
}

int main(void){ //The beginning of the programm, what did you expect?
    struct Values values[8223];
    FILE *file = fopen("hum.txt", "r" );
    int i=0; //the number of every stored value (for the timestamps)
    int k=0; //the number of every stored value (for the temperatures)

    if (file != NULL ){
        char tempString [21];
        int flag = 1;
        while(*readString(tempString, file)){ //if the readStrinf outputs "/0" == "" (end of FILE)
            if(flag == 1){strcpy(values[i].timestamp, tempString); flag++; i++;}
            else if(flag == 2){strcpy(values[k].temperature, tempString); flag--; k++;}
        }
        fclose(file);
    }

    makeTmpIntegers(values, i);

    printValues(&values, i);

    return 0;
}
另外,如果我像这样初始化函数:void printValues struct Values*v[],int I 它确实编译,但根本不打印值

我知道从txt文件中读取整数的正确方法不是这样的,但我想不出其他方法

这没关系:

struct Values{
    char timestamp[21];
    char temperature[2];
    int tmp; };
这两个函数签名也是:

void printValues(struct Values * v, int i) { ... }

void makeTmpIntegers(struct Values values[], int i) { ... }
这是错误的:

v[j]->timestamp, v[j]->temperature, ...
替换:

v[j].timestamp, v[j].temperature, ...
此外:

更改打印值和打印值,i;要打印值值,i

我没有仔细检查任何其他错误,但这应该会让你朝着正确的方向前进

增编:

在上面的两个示例中,printValuesStructValues*v,inti和maketMPintegersStructValues[],inti,您已经通过指针传递了。您只需要修改语法,如我的示例中所示

正如奥本所说:


另外,请注意,在C中传递数组参数在功能上是不正确的 相当于传递一个指针,不涉及任何副本,所以我不确定 是什么内存原因阻止您只传递结构数组 直接


是的,我知道。我知道我可以通过这种方式将结构传递给另一个函数。我想将特定结构的指针传递给函数printValues。这是我的问题,为什么它不适用于指针。另外,请注意,在C中传递数组参数在功能上等同于传递指针,不涉及副本,所以我不确定是什么内存原因阻止您直接传递结构数组。哦,好的,我不知道这一点。谢谢你提供的信息
v[j].timestamp, v[j].temperature, ...