I have created an array of struct i am very frustrated beacuse I just can not figure out how to pass this array of struct into a function. here is my created struct and a general idea of what i have been trying

#include <stdio.h>

void functionOne(MyStruct)

struct MyStruct
{
    string IDname;
    int numbeOne;
    int numberTwo;
 };

int main()
{
MyStruct RecArray[100];

functionOne(RecArray);

return 0;
}

functionOne(MyStruct ArrayOfStruct[])
{
//function code
}

Dani AI

Generated

is right about order and matching declarations. One more key point: when you pass a C-style array parameter like T p[], it decays to T*. The function cannot know how many elements you sent, so either pass the size too, or use a type that carries its size.

Classic and clear: pointer + count (use const if you only read).

struct MyStruct { std::string id; int first; int second; };  // declare before use

void functionOne(MyStruct* items, std::size_t count);  // or: const MyStruct*

int main() {
    MyStruct items[100]{};
    functionOne(items, sizeof items / sizeof items[0]);  // pre-C++17
    // functionOne(items, std::size(items));             // C++17
}

void functionOne(MyStruct* items, std::size_t count) {
    // use items[0..count-1]
}

If you want the callee to know the bound without a separate size, bind to the array itself:

template<size_t N>
void functionOne(MyStruct (&items)[N]) {
    // N is the element count
}

Modern alternatives that avoid raw arrays entirely:

  • std::array<MyStruct,100>& for fixed-size.
  • std::vector<MyStruct>& for dynamic size.
  • C++20: std::span<MyStruct> to accept arrays, vectors, and subranges uniformly.

Extra tips: put the struct before any prototype that uses it (or forward-declare with struct MyStruct; if only pointers/references appear). Include <string> for std::string. Prefer <cstdio> over <stdio.h> in C++ and avoid using namespace std; at global scope.

This works:

#include <stdio.h>
#include <string>
using namespace std;



struct MyStruct
{
    string IDname;
    int numbeOne;
    int numberTwo;
 };
 
void functionOne(MyStruct ArrayOfStruct[]);

int main()
{
MyStruct RecArray[100];

functionOne(RecArray);

return 0;
}

void functionOne(MyStruct ArrayOfStruct[])
{
//function code
}

Your original function declaration did not match your actual function (needed the void in the function and the brackets in the declaration. Also needed to include string library and the namespace, as well as add a semicolon. I don't think your problem was related to the fact that the object type was a struct except that you need to have your struct declaration before anything USING that struct (i.e. the function declaration).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.