(C++) How do I access elements of a structure from a function where the element isn't declared?

BigToque

Lifer
Oct 10, 1999
11,700
0
76
In one of my functions, I've declared a structure.

I want to be able to modify the contents of that structure (specifically an array I've defined) from another function, but I keep getting an error saying my structure is undeclared.

How do I go about doing this?
 

BigToque

Lifer
Oct 10, 1999
11,700
0
76
Originally posted by: BingBongWongFooey
Declare the struct outside of the function, where both can see it.

Wouldn't that make it global?

I thought declaring global variables was considered "bad pratice". Is that false?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
it's not clear to me what you're doing. if you have a struct declared outside of your function and you then declare a variable whose type is that struct, then you should be able to pass the variable to another function and have it manipulate the public members of that variable (assuming that the 2nd function knows what the struct looks like).
 

BigToque

Lifer
Oct 10, 1999
11,700
0
76
Here is my thread on cprogramming.com. I find the
Code:
 tag to be better then the one here and is easier to read. It should give you a better idea of what I'm trying to do.

[url="http://cboard.cprogramming.com/showthread.php?t=63323"]Post[/url]
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
A struct definition is not a variable, so no, it's not a global variable. And you can also declare it at the namespace or class scope if it matters to you.
 

TheLonelyPhoenix

Diamond Member
Feb 15, 2004
5,594
1
0
Pass a pointer to the struct to the function you want to modify it.

You could also declare the struct as a global variable, but you're right, that is horrible programming practice.