传递一个数组元素(struct)来运行

时间:2021-03-19 21:42:37

I have a struct as below

我有一个结构如下

typedef struct _someStruct
{
int V1;
char    V2[10];
} SomeStruct;

SomeStruct *struct;
int elemNo = FillStruct(&struct);
for (i=0; i<elemNo; i++)
    PrintElem(&struct[i]);

Function PrintElem is as below:

功能PrintElem如下:

void PrintElem(SomeStruct *s)
{
    printf("\n\tV1   : %d\n\tV2   : %s\n", s->V1, s->V2);
}

Code fails in the loop from 2'n or 3'rd element.

代码在2'n或3'rd元素的循环中失败。

Any suggestion on what's wrong?

什么是错的任何建议?

EDIT: Actually the PrintElem was (the reasoin I made it this way is the fact that the real struct had about 30 fields with long names, so I made it to make the code readable) :

编辑:实际上PrintElem是(我用这种方式做的事实是真正的结构有大约30个具有长名称的字段,所以我使它使代码可读):

void PrintElem(SomeStruct *s)
{
    printf(
        "\n\tV1   : %d"
        "\n\tV2   : %s\n",
        s->V1, s->V2);
}

I didn't thought this can make a difference, but it did (actually WAS the key of the problem - see my own answer below) I just post it hoping it will help someone later.

我没想到这会有所作为,但确实如此(实际上是问题的关键 - 请参阅下面我自己的答案)我只是发布它希望以后会帮助别人。

Thanks all for all suggestions and for your time

感谢大家的所有建议和时间

4 个解决方案

#1


2  

This one got a few issues, but let's try to sort them out

这个有一些问题,但让我们尝试解决它们

    typedef struct _someStruct
    {
    int V1;
    char    V2[10];
    } SomeStruct;

    /* assuming you create an array of pointers to structures allocated from heap
       and then return the data to the struct1 double pointer */
    SomeStruct ** struct1;
    int elemNo = FillStruct(struct1);
    for (i=0; i<elemNo; i++)
        PrintElem(struct[i]);

    void PrintElem(SomeStruct *s)
    {
        printf("\n\tV1   : %d\n\tV2   : %s\n", s->V1, s->v2);
    }

Hope this helps, if you could clarify FillStruct, that would help.

希望这有助于,如果你能澄清FillStruct,那将有所帮助。

#2


1  

In fact SomeStruct is the name of the variable not the type. I would suggest you split the typedef and variable definition on two lines:

事实上,SomeStruct是变量的名称而不是类型。我建议你在两行中拆分typedef和variable定义:

struct _someStruct
{
  int V1;
  char    V2[10];
};

_someStruct *struct;
int elemNo = FillStruct(&struct);
for (i=0; i<elemNo; i++)
  PrintElem(&struct[i]);


void PrintElem(_someStruct *s)
{
   printf("\n\tV1   : %d\n\tV2   : %s\n", s->V1, s->v2);
}

Also make sure that in FillStruct you allocate an array of type _someStruct, not just one element. Otherwise this will not be strange at all.

还要确保在FillStruct中分配一个类型为_someStruct的数组,而不仅仅是一个元素。否则这根本就不奇怪。

#3


1  

From these lines:

从这些线:

SomeStruct *struct;
int elemNo = FillStruct(&struct);

I assume you want FillStruct function also to allocate memory for these structs. Your problem is most likely in the body of this function (you are allocating too small memory block or you return incorrect value).

我假设您还希望FillStruct函数为这些结构分配内存。您的问题很可能出现在此函数的主体中(您分配的内存块太小或返回的值不正确)。

FillStruct function could look like this:

FillStruct函数可能如下所示:

int FillStruct(SomeStruct **s)
{
    *s = malloc( sizeof(SomeStruct) * 10 );
    // fill members of these structs
    return 10;
}

Also note that struct is a keyword, you can't use it as a name of variable.

另请注意,struct是一个关键字,您不能将其用作变量的名称。

#4


0  

The correct form of PrintElem should be

PrintElem的正确形式应该是

void PrintElem(SomeStruct *s)
{
printf(
    "\n\tV1   : %d"\
    "\n\tV2   : %s\n",
    s->V1, s->V2);
}

Thank you all for help

谢谢大家的帮助

#1


2  

This one got a few issues, but let's try to sort them out

这个有一些问题,但让我们尝试解决它们

    typedef struct _someStruct
    {
    int V1;
    char    V2[10];
    } SomeStruct;

    /* assuming you create an array of pointers to structures allocated from heap
       and then return the data to the struct1 double pointer */
    SomeStruct ** struct1;
    int elemNo = FillStruct(struct1);
    for (i=0; i<elemNo; i++)
        PrintElem(struct[i]);

    void PrintElem(SomeStruct *s)
    {
        printf("\n\tV1   : %d\n\tV2   : %s\n", s->V1, s->v2);
    }

Hope this helps, if you could clarify FillStruct, that would help.

希望这有助于,如果你能澄清FillStruct,那将有所帮助。

#2


1  

In fact SomeStruct is the name of the variable not the type. I would suggest you split the typedef and variable definition on two lines:

事实上,SomeStruct是变量的名称而不是类型。我建议你在两行中拆分typedef和variable定义:

struct _someStruct
{
  int V1;
  char    V2[10];
};

_someStruct *struct;
int elemNo = FillStruct(&struct);
for (i=0; i<elemNo; i++)
  PrintElem(&struct[i]);


void PrintElem(_someStruct *s)
{
   printf("\n\tV1   : %d\n\tV2   : %s\n", s->V1, s->v2);
}

Also make sure that in FillStruct you allocate an array of type _someStruct, not just one element. Otherwise this will not be strange at all.

还要确保在FillStruct中分配一个类型为_someStruct的数组,而不仅仅是一个元素。否则这根本就不奇怪。

#3


1  

From these lines:

从这些线:

SomeStruct *struct;
int elemNo = FillStruct(&struct);

I assume you want FillStruct function also to allocate memory for these structs. Your problem is most likely in the body of this function (you are allocating too small memory block or you return incorrect value).

我假设您还希望FillStruct函数为这些结构分配内存。您的问题很可能出现在此函数的主体中(您分配的内存块太小或返回的值不正确)。

FillStruct function could look like this:

FillStruct函数可能如下所示:

int FillStruct(SomeStruct **s)
{
    *s = malloc( sizeof(SomeStruct) * 10 );
    // fill members of these structs
    return 10;
}

Also note that struct is a keyword, you can't use it as a name of variable.

另请注意,struct是一个关键字,您不能将其用作变量的名称。

#4


0  

The correct form of PrintElem should be

PrintElem的正确形式应该是

void PrintElem(SomeStruct *s)
{
printf(
    "\n\tV1   : %d"\
    "\n\tV2   : %s\n",
    s->V1, s->V2);
}

Thank you all for help

谢谢大家的帮助