调用函数(用C链接)

调用函数(用C链接),c,C,我试图从一个.h和另一个.c文件调用一个函数 尝试在以下行中调用函数: image=getImage(文件名,&dim) 我将主函数顶部的图像定义为 QTnode* image; 从结构 struct imgnode{ int numb; //number of the image int dim; //dimension of the image char filename[20]; //name of file QTnode *qt; Img

我试图从一个.h和另一个.c文件调用一个函数

尝试在以下行中调用函数: image=getImage(文件名,&dim)

我将主函数顶部的图像定义为

QTnode* image;
从结构

struct imgnode{
    int numb;  //number of the image
    int dim;   //dimension of the image
    char filename[20];  //name of file
    QTnode *qt;
    Imgnode *next;
    Imgnode *prev;
};
该函数在另一个.h中调用

QTnode *getImage(char *filename, int *dim );

但编译器告诉我文件名未声明。有什么帮助吗?

你通常做的事情是这样的

所以在foo.c中有一个函数定义为

void foo()
{
    /* do nothing */
}
您将这一行放在头文件中-由include-guards保护

void foo();

现在,将该头文件包含在另一个c文件中,该函数应该能够在编译时解析

image = getImage(filename,&dim);
              // ^^^^^^^^ Check where this variable declared.
              // If declared check whether it is accessible from this scope.
所以在foo.c中有一个函数定义为

void foo()
{
    /* do nothing */
}
您将这一行放在头文件中-由include-guards保护

void foo();
现在,将该头文件包含在另一个c文件中,该函数应该能够在编译时解析

image = getImage(filename,&dim);
              // ^^^^^^^^ Check where this variable declared.
              // If declared check whether it is accessible from this scope.
编译器不知道当前翻译单元中传递参数的变量
filename
<代码>文件名应该是声明的文件名,并且应该可以在函数调用时访问。检查一下

如果您试图访问结构成员,那么您应该有实例来访问它们

 image = getImage(strucInstance.filename,&(strucInstance.dim) );
编译器不知道当前翻译单元中传递参数的变量
filename
<代码>文件名应该是声明的文件名,并且应该可以在函数调用时访问。检查一下

如果您试图访问结构成员,那么您应该有实例来访问它们

 image = getImage(strucInstance.filename,&(strucInstance.dim) );

哈哈,我昨天帮我朋友做了这件事。1917年的作业,嗯

在此赋值中,您应该传入从stdin读取的文件名,而不是结构中的文件名

回到C,定义一个结构并不意味着你有这个结构,并且在结构中有那些成员。您仍然可以通过执行以下操作来声明和创建结构实例:

struct imgnode node;
node.filename
然后,您可以通过以下操作访问该成员:

struct imgnode node;
node.filename
或者更常见的是:

struct imgnode* nodePtr;
nodePtr->filename

哈哈,我昨天帮我朋友做了这件事。1917年的作业,嗯

在此赋值中,您应该传入从stdin读取的文件名,而不是结构中的文件名

回到C,定义一个结构并不意味着你有这个结构,并且在结构中有那些成员。您仍然可以通过执行以下操作来声明和创建结构实例:

struct imgnode node;
node.filename
然后,您可以通过以下操作访问该成员:

struct imgnode node;
node.filename
或者更常见的是:

struct imgnode* nodePtr;
nodePtr->filename

编译器的错误是什么?你说“filname未声明”。我把它改为“filename未声明”(还有一些其他的清理)。编译器错误是什么?你说的是“filname未声明”。我把它改为“文件名未声明”(进行了一些其他清理)。未解析的不是函数,而是标识符
filename
。未解析的不是函数,而是标识符
filename
。是的,comp1917 assignemnt,我很困惑。我的结构看起来像这个结构imgnode{int numb;//图像的编号int dim;//图像字符的维数fname[20];//文件名QTnode*qt;imgnode*next;imgnode*prev;};你能为像我这样的n00b提供一些更基本的指导吗?我不明白你是干什么的。是的,我很困惑。我的结构看起来像这个结构imgnode{int numb;//图像的编号int dim;//图像字符的维数fname[20];//文件名QTnode*qt;imgnode*next;imgnode*prev;};你能为像我这样的n00b提供一些更基本的说明吗?我不明白你在做什么