Question:
C++ passing dynamically declared structure to a function?
Siskin
2011-03-28 04:57:05 UTC
Source file 1:
_________________________________
struct element{
char letter[15];
int nr_letters;
int freq;
};

struct bag{
int nr_elem;
element elem[50];
};

bag *a[20];


int main()
{
Read_bags(); //calls function Create for each new bag in the array.
bag *result = Union(a, n);
getch();
return 0;
}
_________________________________



Source file 2:
__________________________________

bag* Create() // "a" in here is tot the array "a", it could have been named "temp"... "a" is just a personal preference
{
bag *a;
a = new bag;
a->nr_elem = 0;
return a;
}


bag* Union(bag** a, int n)
{
//code... the problem is that in here the array "a" no longer contains the information it had in function main. (it is empty)
}
_________________________________


So the question is, what em i doing wrong ?
Three answers:
?
2011-03-28 08:40:08 UTC
A proper C++ program would be using vectors of bags rather than passing around pointers to pointers to bags.



Meaning, you would have



bag Create()

{

     return bag(); // parentheses guarantee zero initialization

}



and



std::vector a(20); // why is it global, by the way?



and



bag Union(const std::vector& a, int n)



and so on



But you're mentioning "requirements" concerning functions, globals, and files, which means it must be a class assignment.. and class assignments are notorious for requiring C in C++ classes.



Try to reduce your code to the minimal *compilable* example that still demonstrates the error, and maybe a C programmer will walk through it with you.



My guess is that there is an error dealing with global objects in this program rather than anything having to do with parameter passing. For example, the following example compiles and runs and, to the best of what I could gather from your comments, satisfies the requirements:



source file 1:

------------------------------------

extern int n; // the global n is defined elsewhere

struct bag;

bag* a[20]; // array defined HERE

void Read_bags();

bag* Union(bag**, int);

int main()

{

     Read_bags();

     bag* result = Union(a, n);

}



source file 2:

------------------------------------------------------

#include

int n=0; // the global n defined HERE

struct bag { int nr_elem; };

extern bag* a[20]; // the array a is defined elsewhere

void Read_bags()

{

     a[0] = new bag(); a[0]->nr_elem = 1;

     a[1] = new bag(); a[1]->nr_elem = 2;

     a[2] = new bag(); a[2]->nr_elem = 3;

}

bag* Union(bag** a, int n)

{

     std::cout << a[0]->nr_elem << '\n';

     std::cout << a[1]->nr_elem << '\n';

     std::cout << a[2]->nr_elem << '\n';

     return a[0];

}
2016-11-15 08:14:17 UTC
If Avg is defined in the function, then as quickly by using fact the function ends the workstation will overlook approximately what an Avg is and you will discover it provides an blunders message in case you attempt to get right of entry to it. If Avg is defined in international area alongside with on the right before purposes are declared, then you definately ought to have not got any difficulty returning a pointer to Avg from a function. once you're warned approximately malloc/unfastened and new/delete it extremely is for stable reason. in case you create a pointer in a function, and allocate memory for that pointer manually, the pointer ceases to exist while the function ends, however the memory continues to be allotted. for this reason lots of people who write hyperlink record courses will do a *Node Addnode(string records) function which allocates the memory and returns it. you will would desire to delete the node later in this methodology -- so the final analysis is human beings already do it in records shape courses and you ought to have the skill to discover examples of it online.
?
2011-03-28 05:15:21 UTC
You did not write Read_bags() function at all?. How to answer.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...