struct and routines

Hi guys, I'm making a game in C++, is it possible to pass a struct variable to a routine by reference?

thanks!
You mean an object of type myStruct? Try it, and see. I'm away from my desktop right now, so I don't have access to a compiler.

But maybe you can pass
1
2
myStruct myObject;
int returnVal = myFunc(&myObject)

I'm not sure.
i mean a regular struct, like

1
2
3
4
5
struct Document
{
   char Name[20];
   char Lastname[20];
}


i need routines to modify several values of the struct, so i'd rather have one routine that modifies all
Last edited on
In C++, iirc, structs can contain functions.

Or you can declare them as private, and have an object of type myStruct. Then use the object to modify values.

I'm still not sure what you're trying to do.
don't worry, i've found out how:

1
2
3
4
5
struct Document
{
   char Name[20];
   char Lastname[20];
}


supposing i need a routine to change the last name, i call it in main like this:
Changelname(&Mydoc)

and in the routine...
1
2
3
4
Document Changelname(Document* Mydoc)
{
   strcpy((*Mydoc).Lastname,"Something");
}


it was unguessable, lucky 4 me a friend of mine knew this, now i hope it helps for anyone with a similar problem

Thanks!!!
Last edited on
Your original premise was also correct. In C++, you can pass by reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Document
{
    char FirstName[20];
    char LastName[20];
};

Document& ChangeLastName(Document& doc,const char* name)
{
    strcpy(doc.LastName,name);
    return doc;
}

int main()
{
    Document someone;
    ChangeLastName(someone,"Doe");
    ...
}

Hope this helps.
Topic archived. No new replies allowed.