在C结构类型NULL结尾数组 [英] NULL-terminated array of struct types in C
问题描述
我想建stucts的空值终止阵
I'm trying to built a NULL-terminate array of stucts
下面是code: lzdata.c
#include <stdlib.h>
#include <stdio.h>
#include "nist.h"
int main(int argc,char *argv[])
{
nist_t *nist; /* NIST data */
nist=readnist();
}
文件 nist.c
#include <stdlib.h>
#include <stdio.h>
#include "nist.h"
nist_t *readnist()
{
nist_t *nist; /* NIST data */
char line[50];
int len=50;
int i=0;
nist=(nist_t*)malloc(sizeof(nist_t));
while(fgets(line,len,stdin))
{
nist=(nist_t*)realloc(nist,sizeof(nist_t)*(i+1));
sscanf(line,"%s %s %f %lf",nist[i].config,nist[i].term,&(nist[i].j),&(nist[i].level));
++i;
}
nist=(nist_t*)realloc(nist,sizeof(nist_t)*(i+1));
nist[i]=(nist_t)NULL;
return nist;
}
头文件 nist.h
:
#ifndef NIST_H
#define NIST_H
typedef struct
{
char config[3];
char term[4];
float j;
double level;
} nist_t;
nist_t *readnist();
#endif
的数据文件,该文件将通过STDIN被馈送到应用
The data file, which will be fed to the application via STDIN:
2s ¹S 0.0 0.000000
2p ³P° 1.0 142075.333333
2p ¹P° 0.0 271687.000000
2p ³P 1.0 367448.333333
2p ¹D 0.0 405100.000000
2p ¹S 0.0 499633.000000
3s ³S 0.0 1532450.000000
3s ¹S 0.0 1558080.000000
3p ¹P° 0.0 1593600.000000
3p ³P° 1.0 1597500.000000
3d ³D 1.0 1631176.666667
3d ¹D 0.0 1654580.000000
3s ³P° 1.0 1711763.333333
3s ¹P° 0.0 1743040.000000
3p ³D 1.0 1756970.000000
3p ³S 0.0 1770380.000000
3p ³P 0.5 1779340.000000
3p ¹D 0.0 1795870.000000
3d ³P° 1.0 1816053.333333
3d ¹F° 0.0 1834690.000000
3d ¹P° 0.0 1841560.000000
...
...
当我编译:
$ CC -O2 -o lzdata lzdata.c nist.c
nist.c:在函数'readnist:
nist.c:24:2:错误:转换要求的非标量型
我试图改变行 NIST [I] =(nist_t)NULL;
到 NIST [I] =(nist_t *)NULL ;
和我:
$ CC -O2 -o lzdata lzdata.c nist.c
nist.c:在函数'readnist:
nist.c:24:9:错误:分配给从类型键入'nist_t时,不兼容的类型'结构nist_t *'
我试图改变行 NIST [I] =(nist_t)NULL;
到 NIST [我] = NULL;
和我:
$ CC -O2 -o lzdata lzdata.c nist.c
nist.c:在函数'readnist:
nist.c:24:9:错误:分配给从类型键入'nist_t时,不兼容的类型'无效*'
有可能是不同的数目在不同的数据文件的行。我正在寻求建立 nist_t
数据的NULL结尾数组,这样我就可以处理它,直到我到了NULL元素。这可能吗?
There could be a different number of lines in different data files. I'm seeking to build a NULL-terminated array of nist_t
data, so I can process it until I get to the NULL element. Is this possible?
推荐答案
有关编译器宏 NULL
似乎被定义为((无效*)0)
,这是一个普通的指针到零。由于 NIST [I]
(为任何有效的值i
)是的不的指针你得到的错误。
For your compiler the macro NULL
seems to be defined as ((void *) 0)
, that is a generic pointer to zero. Since nist[i]
(for any valid value of i
) is not a pointer you get the errors.
要解决这个问题,最好的办法可能是通过引用传递或者从指针主
进入的功能和使用,并返回大小。或通过引用传递一个整数,并将其设置的大小。
The best way to solve this might be to either pass the pointer from main
by reference into the function and use that, and return the size. Or by passing an integer by reference, and set it to the size.
有另一种解决方案过于,那就是有指针数组。然后,你需要单独分配每个 nist_t
结构,释放她们太。然后你可以使用 NULL
来表示数组的末尾。这实际上是如何的argv
的作品,它是由一个 NULL
指针(让的argv [ARGC终止]
总是等于 NULL
)。
There is another solution too, and that is to have an array of pointers. Then you need to allocate each nist_t
structure separately, and free them all too. Then you can use NULL
to indicate the end of the array. This is actually how argv
works, it's terminated by a NULL
pointer (so argv[argc]
is always equal to NULL
).
这篇关于在C结构类型NULL结尾数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!