C采用指针结构数组

C采用指针结构数组

本文介绍了如何通过在C / C采用指针结构数组++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C code我坚持到结构数组传递给一个函数,这里的code类似于我的问题:

in C code I'm stuck to pass an array of struct to a function, here's the code that resembles my problem:


typedef struct
{
   int x;
   int y;
   char *str1;
   char *str2;
}Struct1;

void processFromStruct1(Struct1 *content[]);
int main()
{
    Struct1 mydata[]=
    { {1,1,"black","cat"},
      {4,5,"red","bird"},
      {6,7,"brown","fox"},
    };

    processFromStruct1(mydata);//how?!?? can't find correct syntax

    return 0;
}

void processFromStruct1(Struct1 *content[])
{
    printf("%s", content[1]->str1);// if I want to print 'red', is this right?
        ...
}

在MSVC编译错误是这样的:

Compile error in msvc is something like this:


error C2664: 'processFromStruct1' : cannot convert parameter 1 from 'Struct1 [3]' to 'Struct1 *[]'
1>       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

如何解决此问题? TNX。

How to solve this? tnx.

推荐答案

您几乎拥有了,可以在本

You almost had it, either this

void processFromStruct1(Struct1 *content);

或本

void processFromStruct1(Struct1 content[]);

和,为阿洛克的评论中指出,改变这一

and, as Alok points out in comments, change this

content[1]->str1

这个

content[1].str1

您阵列结构的数组,而不是一个指针数组,所以一旦你选择具有特定结构[1]有没有必要进一步取消对它的引用。

Your array is an array of structures, not an array of pointers, so once you select a particular structure with [1] there is no need to further dereference it.

这篇关于如何通过在C / C采用指针结构数组++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:00