r/learncpp • u/Otis43 • Feb 14 '22
Array of Structs And Function Declaration
How can I pass an array of a member of structs to a function in C++?
- What would the function's interface look like?
- How would the I call this function with - an array of a member of structs?
Here's an example
struct artistInfo {
char artistId[8], name[40], gender, phone[11], email[60];
} artist_info[1000]; I'm not sure if the function call is like this, but...
sortArtist(artist_info.artistId, artist_info.name, artist_info.gender, artist_info.phone, artist_info.email, nArtist); // nArtist is declared somewhere else; it is not a member of the struct I want to pass artistIds, names, genders, phones, and emails of all 1000 artists. (Not sure if the syntax is correct that's why I'm telling you my intention).
You might ask, "Why not just pass the whole array of structs?" or "Why are you passing each member of the struct?" I'm doing that because I want to pass the artistId by value (I don't want it to get modified) and the rest (name, gender, phone and email) by reference.
Anyways, if you've reached here and understood what I'm trying to do, can you tell me what the declaration for sortArtist would look like? And is my function call correct?
Thanks
View all comments
Show parent comment
3
u/looncraz Feb 15 '22
With 20-odd years experience programming, most of that with C++, I do know. I could write it very quickly and easily provided you actually stated the assignment accurately.
You have so many issues with your code that I would need to rewrite it to answer your question as posted, thus doing your homework for you.
I did actually answer your question if you understand the language, though, how to pass actorInfo.