代码之家  ›  专栏  ›  技术社区  ›  vico

将结构指针数组传递给函数

c++
  •  0
  • vico  · 技术社区  · 5 年前

    试图将结构指针数组传递给函数 doIt() .看来我的方法不正确,因为我找不到正确的第二个数组元素:

    struct c {
        int a;
        char* b;
    };
    
    
    struct cc {
        int a;
        c* b;
    };
    
    char a[] = "aaa";
    char b[] = "bbb";
    char e[] = "eee";
    
    c d1 = {1,a};
    c d2 = {2,b};
    c d3 = { 12,e };
    
    cc g1 = { 123, &d1 };
    cc g2 = { 321, &d2 };
    cc g3 = { 333, &d3 };
    
    
    
    void doIt( c *  s)
    {
        cout << s->b;
        s++;
        cout << s->b;
    }
    

    传递结构指针数组的正确方法是什么?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Zakk    5 年前

    C(和C++)中的原始数组只是指针。它们指向数组的第一个元素。例如,如果您想要一个 int ,你会这样写 int* array .如果你想要一系列 struct c ,你会这样写 c* array .如果你想要一个指向 结构c ,你会这样写 c** array .

    要访问元素,请不要使用 array++ 使用 array[i] 哪里 i 是要访问的元素的索引(位置),0是第一个元素的索引,1是第二个元素的索引,以此类推。

    因此,您的代码应该如下所示:

    void doIt(c** s)
    {
        cout << s[0]->b; // s[0] is the first element
        cout << s[1]->b; // s[1] is the second
    }
    

    注意,在C++中,最好使用 std::vector 而不是原始数组。

    void doIt(std::vector<c*> s)
    {
        cout << s[0]->b;
        cout << s[1]->b;
    }
    
        2
  •  0
  •   Sergey Aleksandrovich    5 年前

    如果要将数组传递给函数,还需要传递此数组的长度:

    #include <iostream>
    #include <vector>
    
    struct c {
        int a;
        char* b = nullptr;
        size_t size = 0;
    };
    
    void doIt(c* all, size_t length);
    
    int main()
    {
        char a[] = "aaa";
        const size_t sizeOfA = sizeof(a)/sizeof(a[0]);
        char b[] = "bbb";
        const size_t sizeOfB = sizeof(b)/sizeof(b[0]);
        char e[] = "eee";
        const size_t sizeOfE = sizeof(e)/sizeof(e[0]);
    
        c d1 {1, a, sizeOfA}; 
        c d2 {2, b, sizeOfB};
        c d3 {12, e, sizeOfE};
    
        c all[] = {d1, d2, d3};
        const size_t length = sizeof(all)/sizeof(all[0]);
    
        doIt(all, length);
    
    return 0;
    }
    
    void doIt(c* all, size_t length)
    {
        if (!all)
        {
            std::cerr << "Pointer to array is null" << std::endl;
        }       
        for (size_t i = 0; i < length; ++i)
        {
            for (size_t j = 0; j < all[i].size; ++j)
            {
                std::cout << all[i].b[j];
            }
            std::cout << std::endl;
        }
    }
    

    你可以用 std::vector .所以,你不需要使用一个附加参数(向量的长度):

    #include <iostream>
    #include <vector>
    #include <string>
    
    struct c {
        int a;
        std::string b;
    };
    
    void doIt(const std::vector<c>& myVector);
    
    int main()
    {
        std::vector<c> myVector;
        myVector.emplace_back(1, "aaa");
        myVector.emplace_back(2, "bbb");
        myVector.emplace_back(12, "eee");
    
        doIt(myVector);
    
    return 0;
    }
    
    void doIt(const std::vector<c>& myVector)
    {
        for (size_t i = 0; i < myVector.size(); ++i)
        {
            std::cout << myVector[i].b << std::endl;
        }
    }