question about arrays, structures, functions

A

Art Cummings

Good morning all. I just finished an assignment using structures. In this
assignment I used an array of structures. What I would have liked was to
use an array of structures with a function. The problem I ran into, was
when the i tried to read the data in the structure I got garbage. Normally
when I use arrays they retain the information that I enter once the function
completes. I'm thinking that it has something to do with using the
structure within the function. I was able to create the function prototype,
the function call and the function header, but I wasn't able to get any data
populated in my structures. I've include the prototype, function call and
header. Any help with understanding if and how this is possible would be
appreciated. The program I created uses an array of structures but does not
call it in a fuction so I got around having to retain the data after the
function ended.


Thanks Art

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData
void getData(WeatherStats[], int);

//currentYear is type WeatherStatas
getData(currentYear, NUM_MONTHS); //function call to getData


//function header
void getData(WeatherStats currentMonth[], int theMonth)
 
A

Alf P. Steinbach

* Art Cummings:
Good morning all. I just finished an assignment using structures. In this
assignment I used an array of structures. What I would have liked was to
use an array of structures with a function. The problem I ran into, was
when the i tried to read the data in the structure I got garbage. Normally
when I use arrays they retain the information that I enter once the function
completes. I'm thinking that it has something to do with using the
structure within the function. I was able to create the function prototype,
the function call and the function header, but I wasn't able to get any data
populated in my structures. I've include the prototype, function call and
header. Any help with understanding if and how this is possible would be
appreciated. The program I created uses an array of structures but does not
call it in a fuction so I got around having to retain the data after the
function ended.


Thanks Art

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData
void getData(WeatherStats[], int);

//currentYear is type WeatherStatas
getData(currentYear, NUM_MONTHS); //function call to getData

Here you're saying the first argument is an array of stats for the
current year, and the second arg is the number of months in the year (or
elements in the array). But
//function header
void getData(WeatherStats currentMonth[], int theMonth)

here you're saying the first argument is the stats for a single month,
and the second argument is the month number.

So what is it?

Since I can't make heads or tails of your example (see comments above)
I'll just give you an example of filling an array with data, C style:

struct Datum
{
int x, y;
};

Datum datum( int x, int y ) { Datum d = {x, y}; return d; }

void getComputedData( Datum data[], int size )
{
for( int i = 0; i < size; ++i )
{
data = datum( i, -i );
}
}

int main()
{
Datum data[5];
getComputedData( data, 5 );
// Use data here.
}

Doing the same C++ style:

#include <vector>

struct Datum
{
int x, y;
Datum( int x_, y_ ): x( x_ ), y( y_ ) {}
};

typedef std::vector<Datum> Data;

Data computedData( int size )
{
Data result;
for( int i = 0; i < size; ++i )
{
result.push_back( Datum( i, -i ) );
}
return result;
}

int main()
{
Data data = computedData();
// Use data here.
}

Cheers, & hth.,

- Alf


PS: It's a good idea to not use all uppercase names except for macros.
 
E

Erik Wikström

Good morning all. I just finished an assignment using structures. In this
assignment I used an array of structures. What I would have liked was to
use an array of structures with a function. The problem I ran into, was
when the i tried to read the data in the structure I got garbage. Normally
when I use arrays they retain the information that I enter once the function
completes. I'm thinking that it has something to do with using the
structure within the function. I was able to create the function prototype,
the function call and the function header, but I wasn't able to get any data
populated in my structures. I've include the prototype, function call and
header. Any help with understanding if and how this is possible would be
appreciated. The program I created uses an array of structures but does not
call it in a fuction so I got around having to retain the data after the
function ended.


Thanks Art

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month
};
//function prototype for data getData
void getData(WeatherStats[], int);

//currentYear is type WeatherStatas
getData(currentYear, NUM_MONTHS); //function call to getData


//function header
void getData(WeatherStats currentMonth[], int theMonth)

We would have to see definition of getData() to help you. Notice though
that when passing an array to a function it will decay into a pointer,
which means that your getData() is the same as

void getData(WeatherStats*, int);

If you use new to re-create the array inside the function that would
explain why you loose data, the newly created array would be somewhere
else in memory than the original one that you passed in.
 
B

bob_blaine

Good morning all. I just finished an assignment using structures. In this
assignment I used an array of structures. What I would have liked was to
use an array of structures with a function. The problem I ran into, was
when the i tried to read the data in the structure I got garbage. Normally
when I use arrays they retain the information that I enter once the function
completes. I'm thinking that it has something to do with using the
structure within the function. I was able to create the function prototype,
the function call and the function header, but I wasn't able to get any data
populated in my structures. I've include the prototype, function call and
header. Any help with understanding if and how this is possible would be
appreciated. The program I created uses an array of structures but does not
call it in a fuction so I got around having to retain the data after the
function ended.

Thanks Art

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month};

//function prototype for data getData
void getData(WeatherStats[], int);

//currentYear is type WeatherStatas
getData(currentYear, NUM_MONTHS); //function call to getData

//function header
void getData(WeatherStats currentMonth[], int theMonth)

I agree with all of the comments in the responses, but would also like
to propose to you that you look into STL containers (like vector). The
advantage is that they handle the memory management for you in a trie-
and-true way. In your case the code would look like this:

#include <vector>
std::vector<WeatherStats> currentYear;

and the prototype is:

void getData(vector<WeatherStats> &);

A couple of things to watch out for in using STL containers -- it uses
the copy constuctor and assignment operator of the class/structure, so
if you need a deep copy, you need to supply these.

Cheers and happy coding!
- Madhacker
 
A

Art Cummings

Thanks everyone, I'll look into your suggestions.

Art
Good morning all. I just finished an assignment using structures. In
this
assignment I used an array of structures. What I would have liked was to
use an array of structures with a function. The problem I ran into, was
when the i tried to read the data in the structure I got garbage.
Normally
when I use arrays they retain the information that I enter once the
function
completes. I'm thinking that it has something to do with using the
structure within the function. I was able to create the function
prototype,
the function call and the function header, but I wasn't able to get any
data
populated in my structures. I've include the prototype, function call
and
header. Any help with understanding if and how this is possible would be
appreciated. The program I created uses an array of structures but does
not
call it in a fuction so I got around having to retain the data after the
function ended.

Thanks Art

struct WeatherStats
{
float rainFall; //total rainfall by month
float highTemp; //high temp for month
float lowTemp; //low temp for month
float avgTemp; //avg temp for month};

//function prototype for data getData
void getData(WeatherStats[], int);

//currentYear is type WeatherStatas
getData(currentYear, NUM_MONTHS); //function call to getData

//function header
void getData(WeatherStats currentMonth[], int theMonth)

I agree with all of the comments in the responses, but would also like
to propose to you that you look into STL containers (like vector). The
advantage is that they handle the memory management for you in a trie-
and-true way. In your case the code would look like this:

#include <vector>
std::vector<WeatherStats> currentYear;

and the prototype is:

void getData(vector<WeatherStats> &);

A couple of things to watch out for in using STL containers -- it uses
the copy constuctor and assignment operator of the class/structure, so
if you need a deep copy, you need to supply these.

Cheers and happy coding!
- Madhacker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top