如何将结构的数组变量传递给另一个函数?

时间:2021-09-20 22:56:13

I am pretty new to using typedef struct, so I hope you'll be able to give pretty basic explanation so I can understand better.

我是使用typedef结构的新手,所以我希望你能够给出非常基本的解释,这样我就能更好地理解。

I have declared an array variable called ExpNum[3] for my typedef struct. I want to be able to pass ExpNum[0].ValueofParamOne[0] and so on and so forth to another function called myfunction(). However, I am not able to do

我已经为我的typedef结构声明了一个名为ExpNum [3]的数组变量。我希望能够将ExpNum [0] .ValueofParamOne [0]等等传递给另一个名为myfunction()的函数。但是,我无法做到

pstInputs->ExpNum[0].ValueofParamOne[0]

When I run my codes as shown below, the values initialized here do not even get passed. I figure this out because I printf ExpNum[0].ValueofParamOne[0] in the main function and myfunction() and the values are different. The one in the main function is correct while the one in myfunction() printed random long numbers, an error. I know why there is an error. My question is how should I pass this array? Is it even possible in the first place?

当我运行如下所示的代码时,这里初始化的值甚至不会被传递。我想出来是因为我在main函数和myfunction()中的printf ExpNum [0] .ValueofParamOne [0]和值不同。主函数中的一个是正确的,而myfunction()中的一个打印随机长数,一个错误。我知道为什么会有错误。我的问题是我应该如何传递这个数组?它首先是否可能?

I know an easier method is to pass the structure without declaring ExpNum[3]. However this is crucial because my actual programme deals with more Experiments and I need ExpNum[3] to help users ensure that they do not mix up the ValueofParamOne, ValueofParamTwo and it's corresponding ExperimentResults

我知道一种更简单的方法是在不声明ExpNum [3]的情况下传递结构。然而这是至关重要的,因为我的实际程序涉及更多的实验,我需要ExpNum [3]来帮助用户确保他们不会混淆ValueofParamOne,ValueofParamTwo和它相应的ExperimentResults

Or maybe I should change my method entirely? My main focus is to make sure users assign their values accurately.

或许我应该完全改变我的方法?我的主要重点是确保用户准确地分配他们的价值。

myfunction() is highly technical and mathematical. Its purpose is to calculate the optimized parameter values. Of course, in my actual function there are more than 3 experiment data.

myfunction()技术性和数学性很强。其目的是计算优化的参数值。当然,在我的实际功能中有超过3个实验数据。

typedef struct{unsigned int     NumofParam;double     ExperimentResults[3];double     ValueofParamOne[3];double     ValueofParamTwo[3];}EXP_CONDITION;int main(){EXP_CONDITION stInputs;EXP_CONDITION* pstInputs;pstInputs = &stInputs;pstInputs->NumofParam = 2U;EXP_CONDITION ExpNum[3];/*assign values to Experiment 1*/ExpNum[0].ValueofParamOne[0]=200;ExpNum[0].ValueofParamTwo[0]=400;ExpNum[0].ExperimentResults[0]=1000;/*assign values to Experiment 2*/ExpNum[1].ValueofParamOne[1]=210;ExpNum[1].ValueofParamTwo[1]=440;ExpNum[1].ExperimentResults[1]=2000;/*assign values to Experiment 3*/ExpNum[2].ValueofParamOne[2]=220;ExpNum[2].ValueofParamTwo[2]=480;ExpNum[2].ExperimentResults[2]=3000;myfunction(&stInputs);return 0;}

Editted based on the comments from @MaxVollmer and @aschepler :)

根据@MaxVollmer和@aschepler的评论编辑:)

3 个解决方案

#1


1  

It is quite apparent that you are just a little bit confused on how to put the values together into a single struct to make use of all values at once.

很明显,您对如何将值组合到一个结构中以便一次性使用所有值感到困惑。

In your code. You assign a value to your stInputs.NumofParam -- but no other values are assigned.

在你的代码中。您为stInputs.NumofParam分配值 - 但不分配其他值。

You then declare an array of 3-struct in ExpNum, but then inexplicably assign only one row of values in each struct?

然后在ExpNum中声明一个3结构数组,但是然后莫名其妙地在每个结构中只分配一行值?

From the gist of your question, it looks like you are trying to fill one struct with all values so you can pass that struct to myfunction (which we will presume does something like output all values in the struct)

从你的问题的要点,看起来你正在尝试用所有值填充一个结构,所以你可以将该结构传递给myfunction(我们将假设它像输出结构中的所有值)

Before looking at fixes, let's look at some general coding issues.

在查看修复之前,让我们看看一些常见的编码问题。

First, don't use magic numbers in your code (except where absolutely required like with the scanf field-width modifier). Your 3 is a magic number. Instead, If you need a constant, #define one (or more), or use a global enum to do the same thing. That way you have one single place at the top of your code to change things if needed and you don't have to go picking through your declarations or loop limits to change things, e.g.

首先,不要在代码中使用幻数(除非像scanf field-width修饰符那样绝对需要)。你的3是一个神奇的数字。相反,如果你需要一个常量,#define一个(或更多),或使用全局枚举来做同样的事情。这样,如果需要,您可以在代码顶部有一个单独的位置来更改内容,而无需通过声明或循环限制来更改内容,例如:

#include <stdio.h>#define MAXV 3  /* if you need a constant, #define one (or more) */typedef struct {    unsigned int     NumofParam;    double     ExperimentResults[MAXV];    double     ValueofParamOne[MAXV];    double     ValueofParamTwo[MAXV];} EXP_CONDITION;

Next, C avoids the use of camelCase or MixedCase variable names in favor of all lower-case while reserving upper-case names for use with macros and constants. It is a matter of style -- so it is completely up to you, but failing to follow it can lead to the wrong first impression in some circles.

接下来,C避免使用camelCase或MixedCase变量名来支持所有小写,同时保留用于宏和常量的大写名称。这是一个风格问题 - 所以它完全取决于你,但如果不遵循它可能会在某些圈子中产生错误的第一印象。

Now on to your code. First (and especially if you will be looping over elements in an array) initialize your structs to all zero when declared. That will eliminate the chance of invoking Undefined Behavior by inadvertently attempting to read from an uninitialized value. You can either use a named initializer for the first member (all others will be set zero by default), or you can use the universal initializer (e.g. {0}) to accomplish the same thing. Example:

现在开始你的代码。首先(特别是如果你将循环遍历数组中的元素)在声明时将结构初始化为全零。这将通过无意中尝试从未初始化的值读取来消除调用未定义行为的可能性。您可以为第一个成员使用命名的初始化程序(默认情况下所有其他成员都将设置为零),或者您可以使用通用初始值设定项(例如{0})来完成相同的操作。例:

int main (void){    /* initialize your struct to all zero using a named initializer     * or the universal intializer {0}     */    EXP_CONDITION stInputs = { .NumofParam = 0 };    EXP_CONDITION* pstInputs;    pstInputs = &stInputs;    ...    EXP_CONDITION ExpNum[MAXV] = {{ .NumofParam = 0 }};

Now look at the logic of your assignments. You declare 3 stucts in the array ExpNum. Each struct has within it 3 arrays of 3 values each, e.g.

现在看看你的作业的逻辑。您在数组ExpNum中声明了3个stucts。每个结构在其中具有3个数组,每个数组包含3个值,例如

    double     ExperimentResults[MAXV];    double     ValueofParamOne[MAXV];    double     ValueofParamTwo[MAXV];

When you attempt to fill each of ExpNum[0] ExpNum[1] and ExpNum[2] -- you are only filling one element, e.g.

当您尝试填充ExpNum [0] ExpNum [1]和ExpNum [2]中的每一个时 - 您只填充一个元素,例如

    /*assign values to Experiment 1*/    ExpNum[0].ValueofParamOne[0]=200;    ExpNum[0].ValueofParamTwo[0]=400;    ExpNum[0].ExperimentResults[0]=1000;    /*assign values to Experiment 2*/    ExpNum[1].ValueofParamOne[1]=210;    ExpNum[1].ValueofParamTwo[1]=440;    ExpNum[1].ExperimentResults[1]=2000;    ...

To completely fill a single struct your would need

要完全填充您需要的单个结构

    /*assign values to Experiment 1*/    ExpNum[0].ValueofParamOne[0]=200;    ExpNum[0].ValueofParamTwo[0]=400;    ExpNum[0].ExperimentResults[0]=1000;    ExpNum[0].ValueofParamOne[1]=210;    ExpNum[0].ValueofParamTwo[1]=440;    ExpNum[0].ExperimentResults[1]=2000;    ExpNum[0].ValueofParamOne[2]=220;    ExpNum[0].ValueofParamTwo[2]=480;    ExpNum[0].ExperimentResults[2]=3000;

Now let's look at myfunction which we will assume just outputs the values stored in each struct (this is just for example):

现在让我们看看myfunction,我们将假设只输出存储在每个结构中的值(这只是例如):

void myfunction (EXP_CONDITION *exp){    printf ("\nNumofParam: %u\n", exp->NumofParam);    for (int i = 0; i < MAXV; i++)        printf (" %7.1lf    %7.1lf    %7.1lf\n", exp->ExperimentResults[i],                exp->ValueofParamOne[i], exp->ValueofParamTwo[i]);    putchar ('\n');     /* tidy up with newline */}

Think about what will print if we call myfunction (pstInputs)? What will print if we call myfunction (&ExpNum[0])? Or myfunction (&ExpNum[1])

想想如果我们调用myfunction(pstInputs)会打印什么?如果我们调用myfunction(&ExpNum [0])会打印什么?或者myfunction(&ExpNum [1])

Hints:

Initial stInputs structNumofParam: 2     0.0        0.0        0.0     0.0        0.0        0.0     0.0        0.0        0.0Content of each of ExpNum structsNumofParam: 0  1000.0      200.0      400.0     0.0        0.0        0.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0  2000.0      210.0      440.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0     0.0        0.0        0.0  3000.0      220.0      480.0

I could be wrong, but logically, it seems you intended to have all experiments within your stInputs struct, and not sprinkled as one-row each in your ExpNum array. The beauty of coding and structs is that you can easily shuffle the data together to now put it in one place so you can manage the data properly. Simple loop over the ExpNum array and copy all data to the stInputs struct in its proper place, e.g.

我可能是错的,但从逻辑上讲,似乎你打算在stInputs结构中进行所有实验,而不是在ExpNum数组中分别作为一行进行分割。编码和结构的优点在于,您可以轻松地将数据混合在一起,现在将其放在一个位置,以便您可以正确地管理数据。在ExpNum数组上进行简单循环,并将所有数据复制到适当位置的stInputs结构中,例如

    /* now put all values in your stInputs struct like it appears     * you intended to do?     */    for (int i = 0; i < MAXV; i++) {        pstInputs->ExperimentResults[i] = ExpNum[i].ExperimentResults[i];        pstInputs->ValueofParamOne[i] = ExpNum[i].ValueofParamOne[i];        pstInputs->ValueofParamTwo[i] = ExpNum[i].ValueofParamTwo[i];    }

Now when you call myfunction (pstInputs) you get all of your data, e.g.

现在,当您调用myfunction(pstInputs)时,您将获得所有数据,例如

Output of the completely filled stInputs structNumofParam: 2  1000.0      200.0      400.0  2000.0      210.0      440.0  3000.0      220.0      480.0

Putting all the pieces together, your final example could look something like the following:

将所有部分放在一起,您的最终示例可能如下所示:

#include <stdio.h>#define MAXV 3  /* if you need a constant, #define one (or more) */typedef struct {    unsigned int     NumofParam;    double     ExperimentResults[MAXV];    double     ValueofParamOne[MAXV];    double     ValueofParamTwo[MAXV];} EXP_CONDITION;void myfunction (EXP_CONDITION *exp){    printf ("\nNumofParam: %u\n", exp->NumofParam);    for (int i = 0; i < MAXV; i++)        printf (" %7.1lf    %7.1lf    %7.1lf\n", exp->ExperimentResults[i],                exp->ValueofParamOne[i], exp->ValueofParamTwo[i]);    putchar ('\n');     /* tidy up with newline */}int main (void){    /* initialize your struct to all zero using a named initializer     * or the universal intializer {0}     */    EXP_CONDITION stInputs = { .NumofParam = 0 };    EXP_CONDITION* pstInputs;    pstInputs = &stInputs;    pstInputs->NumofParam = 2U;    EXP_CONDITION ExpNum[MAXV] = {{ .NumofParam = 0 }};    /*assign values to Experiment 1*/    ExpNum[0].ValueofParamOne[0]=200;    ExpNum[0].ValueofParamTwo[0]=400;    ExpNum[0].ExperimentResults[0]=1000;    /*assign values to Experiment 2*/    ExpNum[1].ValueofParamOne[1]=210;    ExpNum[1].ValueofParamTwo[1]=440;    ExpNum[1].ExperimentResults[1]=2000;    /*assign values to Experiment 3*/    ExpNum[2].ValueofParamOne[2]=220;    ExpNum[2].ValueofParamTwo[2]=480;    ExpNum[2].ExperimentResults[2]=3000;    /* output your first stInputs struct */    puts ("Initial stInputs struct");    myfunction (&stInputs);    /* output values in each of your ExpNum array of struct     * (but note, you only assign one-row in each struct)     */    puts ("Content of each of ExpNum structs");    for (int i = 0; i < MAXV; i++)        myfunction (&ExpNum[i]);    /* now put all values in your stInputs struct like it appears     * you intended to do?     */    for (int i = 0; i < MAXV; i++) {        pstInputs->ExperimentResults[i] = ExpNum[i].ExperimentResults[i];        pstInputs->ValueofParamOne[i] = ExpNum[i].ValueofParamOne[i];        pstInputs->ValueofParamTwo[i] = ExpNum[i].ValueofParamTwo[i];    }    /* output the completely filled stInputs struct */    puts ("Output of the completely filled stInputs struct");    myfunction (pstInputs);    return 0;}

Full Example Use/Output

完整示例使用/输出

$ ./bin/expstructInitial stInputs structNumofParam: 2     0.0        0.0        0.0     0.0        0.0        0.0     0.0        0.0        0.0Content of each of ExpNum structsNumofParam: 0  1000.0      200.0      400.0     0.0        0.0        0.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0  2000.0      210.0      440.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0     0.0        0.0        0.0  3000.0      220.0      480.0Output of the completely filled stInputs structNumofParam: 2  1000.0      200.0      400.0  2000.0      210.0      440.0  3000.0      220.0      480.0

Look things over and let me know if you have further questions. If I have misinterpreted your question, just drop a comment or edit your question and let me know.

仔细看看,如果您有其他问题,请告诉我。如果我误解了您的问题,只需发表评论或编辑您的问题并告诉我。

#2


0  

If you want to pass the ExpNum array, simply pass it. There really isn't much to it:

如果要传递ExpNum数组,只需传递它即可。真的没有多少:

myfunction(ExpNum);

The compiler will actually pass a pointer to the first element in the array, but that doesn't matter. You know it's 3 elements (and if not, you can simply add a 2nd parameter to the function to provide the number of elements).

编译器实际上会将指针传递给数组中的第一个元素,但这并不重要。你知道它是3个元素(如果没有,你可以简单地在函数中添加第二个参数来提供元素数量)。

Inside myfunction you can then access the elements in the array like so:

在myfunction中,您可以访问数组中的元素,如下所示:

void myfunction(EXP_CONDITION* expNum){    double foo = expNum[0].ValueofParamOne[0];    double bar = expNum[1].ValueofParamOne[0];    double baz = expNum[2].ValueofParamOne[0];    // ...}

#3


0  

In your first line:

在你的第一行:

EXP_CONDITION stInputs;

you've constructed an object of the EXP_CONDITION struct. All it's members are uninitialized (i.e. "random").You then create pointer to this struct and set the value of the NumofParam member. All other members remain uninitialized:

你已经构造了一个EXP_CONDITION结构的对象。它的所有成员都是未初始化的(即“随机”)。然后创建指向此结构的指针并设置NumofParam成员的值。所有其他成员仍未初始化:

pstInputs = &stInputs;pstInputs->NumofParam = 2U;

Now, and here's where your getting tripped up I think. You create an array of 3 more EXP_CONDITION structs. They are sitting somewhere else in memory. Modifying these structs will not modify the original stInputs object that you declare above.

现在,我认为这是你绊倒的地方。您创建一个包含3个EXP_CONDITION结构的数组。他们坐在记忆中的其他地方。修改这些结构不会修改您在上面声明的原始stInputs对象。

EXP_CONDITION ExpNum[3];

You finally pass a pointer to the original stInputs object to your function. It is still uninitialized.

最后,将指向原始stInputs对象的指针传递给函数。它还没有初始化。

Now, when you are assigning results, you have 2 levels of array, where it's not clear why. For example:

现在,当您分配结果时,您有两个级别的数组,其中不清楚原因。例如:

/*assign values to Experiment 2*/ExpNum[1].ValueofParamOne[1]=210;ExpNum[1].ValueofParamTwo[1]=440;ExpNum[1].ExperimentResults[1]=2000;

assigns the values to the second ExpNum (which seems fine), but then you assign it to the second index in the arrays inside the struct (e.g. ExpNum[1].ValueofParamTwo[0] is unintialized).

分配值给第二ExpNum(这似乎细),但随后将其分配给阵列中的结构体内部的第二索引(例如ExpNum [1] .ValueofParamTwo [0]是unintialized)。

So, my guess as to what you want: Start with a top level structure that will hold all your experiments, then create a separate structure to hold each experiment's results.

所以,我猜你想要什么:从一个*结构开始,它将包含你所有的实验,然后创建一个单独的结构来保存每个实验的结果。

typedef struct {    double     ExperimentResults;    double     ValueofParamOne;    double     ValueofParamTwo;} ExperimentData;typedef struct {    unsigned int     NumofParam;    ExperimentData     experiments[3];} EXP_CONDITION;

and you can assign the values like this:

你可以像这样分配值:

EXP_CONDITION stInputs;stInputs.NumofParam = 2U;stInputs.experiments[0].ValueofParamOne = 200;stInputs.experiments[0].ValueofParamTwo = 400;stInputs.experiments[0].ExperimentResults =  1000;stInputs.experiments[1].ValueofParamOne = 210;stInputs.experiments[1].ValueofParamTwo = 440;stInputs.experiments[1].ExperimentResults =  2000;// etc . . .

#1


1  

It is quite apparent that you are just a little bit confused on how to put the values together into a single struct to make use of all values at once.

很明显,您对如何将值组合到一个结构中以便一次性使用所有值感到困惑。

In your code. You assign a value to your stInputs.NumofParam -- but no other values are assigned.

在你的代码中。您为stInputs.NumofParam分配值 - 但不分配其他值。

You then declare an array of 3-struct in ExpNum, but then inexplicably assign only one row of values in each struct?

然后在ExpNum中声明一个3结构数组,但是然后莫名其妙地在每个结构中只分配一行值?

From the gist of your question, it looks like you are trying to fill one struct with all values so you can pass that struct to myfunction (which we will presume does something like output all values in the struct)

从你的问题的要点,看起来你正在尝试用所有值填充一个结构,所以你可以将该结构传递给myfunction(我们将假设它像输出结构中的所有值)

Before looking at fixes, let's look at some general coding issues.

在查看修复之前,让我们看看一些常见的编码问题。

First, don't use magic numbers in your code (except where absolutely required like with the scanf field-width modifier). Your 3 is a magic number. Instead, If you need a constant, #define one (or more), or use a global enum to do the same thing. That way you have one single place at the top of your code to change things if needed and you don't have to go picking through your declarations or loop limits to change things, e.g.

首先,不要在代码中使用幻数(除非像scanf field-width修饰符那样绝对需要)。你的3是一个神奇的数字。相反,如果你需要一个常量,#define一个(或更多),或使用全局枚举来做同样的事情。这样,如果需要,您可以在代码顶部有一个单独的位置来更改内容,而无需通过声明或循环限制来更改内容,例如:

#include <stdio.h>#define MAXV 3  /* if you need a constant, #define one (or more) */typedef struct {    unsigned int     NumofParam;    double     ExperimentResults[MAXV];    double     ValueofParamOne[MAXV];    double     ValueofParamTwo[MAXV];} EXP_CONDITION;

Next, C avoids the use of camelCase or MixedCase variable names in favor of all lower-case while reserving upper-case names for use with macros and constants. It is a matter of style -- so it is completely up to you, but failing to follow it can lead to the wrong first impression in some circles.

接下来,C避免使用camelCase或MixedCase变量名来支持所有小写,同时保留用于宏和常量的大写名称。这是一个风格问题 - 所以它完全取决于你,但如果不遵循它可能会在某些圈子中产生错误的第一印象。

Now on to your code. First (and especially if you will be looping over elements in an array) initialize your structs to all zero when declared. That will eliminate the chance of invoking Undefined Behavior by inadvertently attempting to read from an uninitialized value. You can either use a named initializer for the first member (all others will be set zero by default), or you can use the universal initializer (e.g. {0}) to accomplish the same thing. Example:

现在开始你的代码。首先(特别是如果你将循环遍历数组中的元素)在声明时将结构初始化为全零。这将通过无意中尝试从未初始化的值读取来消除调用未定义行为的可能性。您可以为第一个成员使用命名的初始化程序(默认情况下所有其他成员都将设置为零),或者您可以使用通用初始值设定项(例如{0})来完成相同的操作。例:

int main (void){    /* initialize your struct to all zero using a named initializer     * or the universal intializer {0}     */    EXP_CONDITION stInputs = { .NumofParam = 0 };    EXP_CONDITION* pstInputs;    pstInputs = &stInputs;    ...    EXP_CONDITION ExpNum[MAXV] = {{ .NumofParam = 0 }};

Now look at the logic of your assignments. You declare 3 stucts in the array ExpNum. Each struct has within it 3 arrays of 3 values each, e.g.

现在看看你的作业的逻辑。您在数组ExpNum中声明了3个stucts。每个结构在其中具有3个数组,每个数组包含3个值,例如

    double     ExperimentResults[MAXV];    double     ValueofParamOne[MAXV];    double     ValueofParamTwo[MAXV];

When you attempt to fill each of ExpNum[0] ExpNum[1] and ExpNum[2] -- you are only filling one element, e.g.

当您尝试填充ExpNum [0] ExpNum [1]和ExpNum [2]中的每一个时 - 您只填充一个元素,例如

    /*assign values to Experiment 1*/    ExpNum[0].ValueofParamOne[0]=200;    ExpNum[0].ValueofParamTwo[0]=400;    ExpNum[0].ExperimentResults[0]=1000;    /*assign values to Experiment 2*/    ExpNum[1].ValueofParamOne[1]=210;    ExpNum[1].ValueofParamTwo[1]=440;    ExpNum[1].ExperimentResults[1]=2000;    ...

To completely fill a single struct your would need

要完全填充您需要的单个结构

    /*assign values to Experiment 1*/    ExpNum[0].ValueofParamOne[0]=200;    ExpNum[0].ValueofParamTwo[0]=400;    ExpNum[0].ExperimentResults[0]=1000;    ExpNum[0].ValueofParamOne[1]=210;    ExpNum[0].ValueofParamTwo[1]=440;    ExpNum[0].ExperimentResults[1]=2000;    ExpNum[0].ValueofParamOne[2]=220;    ExpNum[0].ValueofParamTwo[2]=480;    ExpNum[0].ExperimentResults[2]=3000;

Now let's look at myfunction which we will assume just outputs the values stored in each struct (this is just for example):

现在让我们看看myfunction,我们将假设只输出存储在每个结构中的值(这只是例如):

void myfunction (EXP_CONDITION *exp){    printf ("\nNumofParam: %u\n", exp->NumofParam);    for (int i = 0; i < MAXV; i++)        printf (" %7.1lf    %7.1lf    %7.1lf\n", exp->ExperimentResults[i],                exp->ValueofParamOne[i], exp->ValueofParamTwo[i]);    putchar ('\n');     /* tidy up with newline */}

Think about what will print if we call myfunction (pstInputs)? What will print if we call myfunction (&ExpNum[0])? Or myfunction (&ExpNum[1])

想想如果我们调用myfunction(pstInputs)会打印什么?如果我们调用myfunction(&ExpNum [0])会打印什么?或者myfunction(&ExpNum [1])

Hints:

Initial stInputs structNumofParam: 2     0.0        0.0        0.0     0.0        0.0        0.0     0.0        0.0        0.0Content of each of ExpNum structsNumofParam: 0  1000.0      200.0      400.0     0.0        0.0        0.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0  2000.0      210.0      440.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0     0.0        0.0        0.0  3000.0      220.0      480.0

I could be wrong, but logically, it seems you intended to have all experiments within your stInputs struct, and not sprinkled as one-row each in your ExpNum array. The beauty of coding and structs is that you can easily shuffle the data together to now put it in one place so you can manage the data properly. Simple loop over the ExpNum array and copy all data to the stInputs struct in its proper place, e.g.

我可能是错的,但从逻辑上讲,似乎你打算在stInputs结构中进行所有实验,而不是在ExpNum数组中分别作为一行进行分割。编码和结构的优点在于,您可以轻松地将数据混合在一起,现在将其放在一个位置,以便您可以正确地管理数据。在ExpNum数组上进行简单循环,并将所有数据复制到适当位置的stInputs结构中,例如

    /* now put all values in your stInputs struct like it appears     * you intended to do?     */    for (int i = 0; i < MAXV; i++) {        pstInputs->ExperimentResults[i] = ExpNum[i].ExperimentResults[i];        pstInputs->ValueofParamOne[i] = ExpNum[i].ValueofParamOne[i];        pstInputs->ValueofParamTwo[i] = ExpNum[i].ValueofParamTwo[i];    }

Now when you call myfunction (pstInputs) you get all of your data, e.g.

现在,当您调用myfunction(pstInputs)时,您将获得所有数据,例如

Output of the completely filled stInputs structNumofParam: 2  1000.0      200.0      400.0  2000.0      210.0      440.0  3000.0      220.0      480.0

Putting all the pieces together, your final example could look something like the following:

将所有部分放在一起,您的最终示例可能如下所示:

#include <stdio.h>#define MAXV 3  /* if you need a constant, #define one (or more) */typedef struct {    unsigned int     NumofParam;    double     ExperimentResults[MAXV];    double     ValueofParamOne[MAXV];    double     ValueofParamTwo[MAXV];} EXP_CONDITION;void myfunction (EXP_CONDITION *exp){    printf ("\nNumofParam: %u\n", exp->NumofParam);    for (int i = 0; i < MAXV; i++)        printf (" %7.1lf    %7.1lf    %7.1lf\n", exp->ExperimentResults[i],                exp->ValueofParamOne[i], exp->ValueofParamTwo[i]);    putchar ('\n');     /* tidy up with newline */}int main (void){    /* initialize your struct to all zero using a named initializer     * or the universal intializer {0}     */    EXP_CONDITION stInputs = { .NumofParam = 0 };    EXP_CONDITION* pstInputs;    pstInputs = &stInputs;    pstInputs->NumofParam = 2U;    EXP_CONDITION ExpNum[MAXV] = {{ .NumofParam = 0 }};    /*assign values to Experiment 1*/    ExpNum[0].ValueofParamOne[0]=200;    ExpNum[0].ValueofParamTwo[0]=400;    ExpNum[0].ExperimentResults[0]=1000;    /*assign values to Experiment 2*/    ExpNum[1].ValueofParamOne[1]=210;    ExpNum[1].ValueofParamTwo[1]=440;    ExpNum[1].ExperimentResults[1]=2000;    /*assign values to Experiment 3*/    ExpNum[2].ValueofParamOne[2]=220;    ExpNum[2].ValueofParamTwo[2]=480;    ExpNum[2].ExperimentResults[2]=3000;    /* output your first stInputs struct */    puts ("Initial stInputs struct");    myfunction (&stInputs);    /* output values in each of your ExpNum array of struct     * (but note, you only assign one-row in each struct)     */    puts ("Content of each of ExpNum structs");    for (int i = 0; i < MAXV; i++)        myfunction (&ExpNum[i]);    /* now put all values in your stInputs struct like it appears     * you intended to do?     */    for (int i = 0; i < MAXV; i++) {        pstInputs->ExperimentResults[i] = ExpNum[i].ExperimentResults[i];        pstInputs->ValueofParamOne[i] = ExpNum[i].ValueofParamOne[i];        pstInputs->ValueofParamTwo[i] = ExpNum[i].ValueofParamTwo[i];    }    /* output the completely filled stInputs struct */    puts ("Output of the completely filled stInputs struct");    myfunction (pstInputs);    return 0;}

Full Example Use/Output

完整示例使用/输出

$ ./bin/expstructInitial stInputs structNumofParam: 2     0.0        0.0        0.0     0.0        0.0        0.0     0.0        0.0        0.0Content of each of ExpNum structsNumofParam: 0  1000.0      200.0      400.0     0.0        0.0        0.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0  2000.0      210.0      440.0     0.0        0.0        0.0NumofParam: 0     0.0        0.0        0.0     0.0        0.0        0.0  3000.0      220.0      480.0Output of the completely filled stInputs structNumofParam: 2  1000.0      200.0      400.0  2000.0      210.0      440.0  3000.0      220.0      480.0

Look things over and let me know if you have further questions. If I have misinterpreted your question, just drop a comment or edit your question and let me know.

仔细看看,如果您有其他问题,请告诉我。如果我误解了您的问题,只需发表评论或编辑您的问题并告诉我。

#2


0  

If you want to pass the ExpNum array, simply pass it. There really isn't much to it:

如果要传递ExpNum数组,只需传递它即可。真的没有多少:

myfunction(ExpNum);

The compiler will actually pass a pointer to the first element in the array, but that doesn't matter. You know it's 3 elements (and if not, you can simply add a 2nd parameter to the function to provide the number of elements).

编译器实际上会将指针传递给数组中的第一个元素,但这并不重要。你知道它是3个元素(如果没有,你可以简单地在函数中添加第二个参数来提供元素数量)。

Inside myfunction you can then access the elements in the array like so:

在myfunction中,您可以访问数组中的元素,如下所示:

void myfunction(EXP_CONDITION* expNum){    double foo = expNum[0].ValueofParamOne[0];    double bar = expNum[1].ValueofParamOne[0];    double baz = expNum[2].ValueofParamOne[0];    // ...}

#3


0  

In your first line:

在你的第一行:

EXP_CONDITION stInputs;

you've constructed an object of the EXP_CONDITION struct. All it's members are uninitialized (i.e. "random").You then create pointer to this struct and set the value of the NumofParam member. All other members remain uninitialized:

你已经构造了一个EXP_CONDITION结构的对象。它的所有成员都是未初始化的(即“随机”)。然后创建指向此结构的指针并设置NumofParam成员的值。所有其他成员仍未初始化:

pstInputs = &stInputs;pstInputs->NumofParam = 2U;

Now, and here's where your getting tripped up I think. You create an array of 3 more EXP_CONDITION structs. They are sitting somewhere else in memory. Modifying these structs will not modify the original stInputs object that you declare above.

现在,我认为这是你绊倒的地方。您创建一个包含3个EXP_CONDITION结构的数组。他们坐在记忆中的其他地方。修改这些结构不会修改您在上面声明的原始stInputs对象。

EXP_CONDITION ExpNum[3];

You finally pass a pointer to the original stInputs object to your function. It is still uninitialized.

最后,将指向原始stInputs对象的指针传递给函数。它还没有初始化。

Now, when you are assigning results, you have 2 levels of array, where it's not clear why. For example:

现在,当您分配结果时,您有两个级别的数组,其中不清楚原因。例如:

/*assign values to Experiment 2*/ExpNum[1].ValueofParamOne[1]=210;ExpNum[1].ValueofParamTwo[1]=440;ExpNum[1].ExperimentResults[1]=2000;

assigns the values to the second ExpNum (which seems fine), but then you assign it to the second index in the arrays inside the struct (e.g. ExpNum[1].ValueofParamTwo[0] is unintialized).

分配值给第二ExpNum(这似乎细),但随后将其分配给阵列中的结构体内部的第二索引(例如ExpNum [1] .ValueofParamTwo [0]是unintialized)。

So, my guess as to what you want: Start with a top level structure that will hold all your experiments, then create a separate structure to hold each experiment's results.

所以,我猜你想要什么:从一个*结构开始,它将包含你所有的实验,然后创建一个单独的结构来保存每个实验的结果。

typedef struct {    double     ExperimentResults;    double     ValueofParamOne;    double     ValueofParamTwo;} ExperimentData;typedef struct {    unsigned int     NumofParam;    ExperimentData     experiments[3];} EXP_CONDITION;

and you can assign the values like this:

你可以像这样分配值:

EXP_CONDITION stInputs;stInputs.NumofParam = 2U;stInputs.experiments[0].ValueofParamOne = 200;stInputs.experiments[0].ValueofParamTwo = 400;stInputs.experiments[0].ExperimentResults =  1000;stInputs.experiments[1].ValueofParamOne = 210;stInputs.experiments[1].ValueofParamTwo = 440;stInputs.experiments[1].ExperimentResults =  2000;// etc . . .