简体   繁体   中英

C++ : Array of struct as function parameter

I have a struct:

typedef struct
{  
   int nNum; 
   string str;   
}KeyPair;

Let's say I initialize my struct:

KeyPair keys[] = 
{    {0, "tester"},  
     {2, "yadah"},  
     {0, "tester"} 
};  

I want to use the initialized values in a function. How do I pass this array struct as a function parameter?

I have:

FetchKeys( KeyPair *pKeys)
{
     //get the contents of keys[] here...   
}

How about?

template<int N> void FetchKeys(KeyPair const (&r)[N]){}

EDIT 2:

Or even

template<int N> void FetchKeys(KeyPair const (*p)[N])

with the call as

FetchKeys(&keys);

You can do it as @MSalters mentioned, or you can create a std::vector<KeyPair> and pass it to the function. Here is a sample code:

using namespace std;

struct KeyPair 
{ 
   int nNum;
   string str;  
};

void fetchKeys(const vector<KeyPair>& keys)
{
    //Go through elements of the vector
    vector<KeyPair>::const_iterator iter = keys.begin();
    for(; iter != keys.end(); ++iter)
    {
        const KeyPair& pair = *iter;

    }
}



int main()
{
    KeyPair keys[] = {{0, "tester"}, 
                   {2, "yadah"}, 
                   {0, "tester"}
                  }; 

    //Create a vector out of the array you are having
    vector<KeyPair> v(keys, keys + sizeof(keys)/sizeof(keys[0]));

    //Pass this vector to the function. This is safe as vector knows
    //how many element it contains
    fetchKeys(v);
    return 0;

}

Should be

// Definition
void FetchKeys( KeyPair *pKeys, int nKeys)
{
     //get the contents of keys[] here...   
}
// Call
FetchKeys(keys, sizeof(keys)/sizeof(keys[0]));

You just call FetchKeys(keys);

EDIT

Pay attention to declare FetchKeys ' return type.

EDIT 2

If you also need the number of items, you add size as FetchKeys input parameters:

void FetchKeys(KeyPair*, size_t size);

and call FetchKeys(keys, sizeof(keys)/sizeof(*keys));

BTW, state all your question by editing your first post if you can.

In c/c++ the name of the array (of any type) represents the address of the first element of the array, so keys and &keys [0] are same. You can pass any one of them for KeyPair*.

Depending on what you want to do you can even use boost range and pass it to function as a pair of iterators:

void FetchKeys(KeyPair *begin, KeyPair *end)
FetchKeys(boost::begin(keys), boost::end(keys));

See this answer: How can I pass an array by reference to a function in C++?

Wrap it in a structure, nice and easy..

#include <iostream>

struct foo
{
  int a;
  int b;
};

template <typename _T, size_t _size>
struct array_of
{
  static size_t size() { return _size; }
  _T data[_size];
};

template <typename _at>
void test(_at & array)
{
  cout << "size: " << _at::size() << std::endl;
}

int main(void)
{
  array_of<foo, 3> a = {{ {1,2}, {2,2}, {3,2} }};

  test(a);

}

EDIT: URGH, I can't see the toolbar to format the code correctly, hopefully the tags works..

i use VS 2008, and this works fine for me.

#include "stdafx.h"

typedef struct
{  
   int nNum; 
   CString str;   
}KeyPair;

void FetchKeys( KeyPair *pKeys);
int _tmain(int argc, _TCHAR* argv[])
{

    KeyPair keys[] = 
{    {0, _T("tester")},  
     {2, _T("yadah")},  
     {0, _T("tester")} 
};

    FetchKeys(keys); //--> just pass the initialized variable.
    return 0;
}

void FetchKeys(KeyPair *pKeys)
{
    printf("%d, %s\n",pKeys[0].nNum, pKeys[0].str);

}

I don't understand the difficulty. correct me if i'm wrong. To keep it simple, i avoided using vectors, templates and etc. edit: to know size of struct, you can pass one more arg.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM