所以我填充了数组,我需要将它们分配给各自的struct字段。
char array1[MaxLine] = "bob";
char array2[MaxLine] = "rick";
char array3[MaxLine] = "dan";
所以,让我们假装他们说的是什么。我有一个在头文件中声明的结构,这个函数在第二个源中,与main分开。我如何从这个单独的函数访问结构,以填充这些数组的值。
将其添加到:
我已经尝试声明一个名为filler的变量..
struct structname filler;
然后使用它来访问结构并以这种方式填充..
strcpy(filler->firstfield, input);
strcpy(filler->secondfield, input);
但它不断抛出访问冲突。
答案 0 :(得分:0)
您必须将其作为参数传递。例如:
#include "MyStruct.h" //declares the struct
#include "MyFunctions.h" // where the function is
int main()
{
struct MyStruct instance;
assignFields(&instance);
//instance is now assigned
}
//In MyFunctions.h you declare your function
#include "MyStruct.h" //Needs to know about the struct
void assignField(MyStruct*);
//Then you implement it in the .c file
void assignFields(MyStruct* localInstance)
{
//call strcpy here
//assuming the input strings are available her
//otherwise you have to pass them in as well
}
运行编译器时,它将使用.h文件找出签名并单独编译每个文件,然后链接器将两个.c文件放在一个可执行程序中。