structures with functions

my knowledge till now tells me to pass structures to functions one must declare structure as global??? clarify please
//Main.cpp

//Structure is global
struct Data {
int x, y;
};

//Function is also global
void Foo(Data d) {
//Do stuff
}

Is that what you mean?
closed account (jvqpDjzh)
my knowledge till now tells me to pass structures to functions one must declare structure as global???
If you want to use a struct type, defined in 1 function, inside another function, how could you do it? See example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void foo()
{
    struct person//local struct
    {
        std::string name = "name";
        int age = 99;
    };

    person person1;

    std::cout << "name = "<<person1.name<<'\n';
    std::cout << "age = "<<person1.age<<'\n';
}

struct client//global struct
{
    std::string name= "client_name";
    std::string age = "client_age";
};

void fun(client client1)
{
    std::cout << "name = "<<client1.name<<'\n';
    std::cout << "age = "<<client1.age<<'\n';
}

void bar(person person1)//person was not declared.
{
    std::cout << "name = "<<person1.name<<'\n';
    std::cout << "age = "<<person1.age<<'\n';
}

int main()
{
    client client1;
    client1.name = "Dude";
    client1.age = "300";

    fun(client1);

    std::cin.get();
    return 0;
}
Last edited on
void bar(person person1)//person was not declared.


is this really possible, I think bar() would not recognize "person" and if let's say it does recognize then concept of global is nothing all things inside functions are all globals
is this really possible

No. The declaration of person has local scope, so bar() has no visibility to the declaration.
so if bar() does not see person then how can we declare person type variable inside bar()???
If you are going to use a struct/class as a function parameter or return type you have to declare as global either outside main or in a header you include in your main file. Having a local struct/class inside a function would be pointless as it would go out of scope as soon as the function ends. You could do something like this, but I think it is rather silly to do so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

void foo(string &name, int &age)
{
	struct Person{
		string name = "Danicpp";
		int age = 27;  // sorry just a random number
	};
	
	Person person1;
	
	name = person1.name;
	age = person1.age;
}

int main()
{
	int number;
	string strTest;
	
	foo(strTest, number);
	
	cout << strTest << " .::. " << number << endl;
	
	return 0;
}
Danicpp .::. 27
lolx BHX Specter well you got close actually it's 27-3=24 lolx

well actually I asked this question because (jvqpDjzh) did declare a struct inside foo and then used it as a parameter in another function bar

declared inside foo

void foo()
{
struct person//local struct
{
std::string name = "name";
int age = 99;
};

person person1;

std::cout << "name = "<<person1.name<<'\n';
std::cout << "age = "<<person1.age<<'\n';
}


used as parameter of bar

void bar(person person1)//person was not declared.
{
std::cout << "name = "<<person1.name<<'\n';
std::cout << "age = "<<person1.age<<'\n';
}


I think bar() shouldn't recognize the type person right
Yeah, that isn't possible and the compiler shouldn't compile it at all. He deleted his account so I have no clue why he put that other than maybe to demonstrate that it isn't possible to do it.
Last edited on
Topic archived. No new replies allowed.