7 Replies - 30821 Views - Last Post: 09 June 2009 - 10:54 AM Rate Topic: -----

#1 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

to pass structures as arguments in functions

Post icon  Posted 05 December 2007 - 05:16 AM

Hello,
I want to pass structures as argument parameters of a function. For eg suppose i have 2 structures:


struct set
{
 int ms;
double code;
} settings={10,5.5};

struct trac
{
double freq[100];
double ip[100];
}trackres[5];

main()
{

}




And this runs through the main() code and gets its values. Now if i want to write a function which takes these values from the structures as inputs and gives me some calculated values as output. Is this syntax correct?

func_out= func_name(struct trackres[5], struct settings);


and also if i want to access the structures values within the function, can i use it as usual i.e
new_var=trackres[2].ip[10];


Thanks,
prads

This post has been edited by prads: 05 December 2007 - 05:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: to pass structures as arguments in functions

#2 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: to pass structures as arguments in functions

Posted 05 December 2007 - 08:12 AM

i think that u have to make a global structure so u can get it throw any other function by using it inside
Was This Post Helpful? 0
  • +
  • -

#3 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: to pass structures as arguments in functions

Posted 05 December 2007 - 01:02 PM

View Postcuriose, on 5 Dec, 2007 - 08:12 AM, said:

i think that u have to make a global structure so u can get it throw any other function by using it inside


my structures are global and defined before main()...........
Was This Post Helpful? 0
  • +
  • -

#4 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: to pass structures as arguments in functions

Posted 07 December 2007 - 03:57 AM

hello,
its still unclear about passing the structures.....can someone pls help me.
thanks,
prads
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: to pass structures as arguments in functions

Posted 07 December 2007 - 04:17 AM

Global variables are 'evil' - so don't use them unless you really have to.

the struct keyword is merely a mechanism to make a composite type, the rules for passing a variable or array are the same as for variables of any other type. The difference is that a struct type in 'C' is preceded by the struct keyword (Unless you use a typedef).
void foo( struct trac my_trac ) 

This post has been edited by Bench: 07 December 2007 - 04:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: to pass structures as arguments in functions

Posted 07 December 2007 - 02:33 PM

View PostBench, on 7 Dec, 2007 - 04:17 AM, said:

Global variables are 'evil' - so don't use them unless you really have to.

the struct keyword is merely a mechanism to make a composite type, the rules for passing a variable or array are the same as for variables of any other type. The difference is that a struct type in 'C' is preceded by the struct keyword (Unless you use a typedef).
void foo( struct trac my_trac ) 



so if i hav an array of structures then should i declare it as......
void foo( struct trac my_trac[5] ) 

Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: to pass structures as arguments in functions

Posted 07 December 2007 - 03:16 PM

View Postprads, on 7 Dec, 2007 - 09:33 PM, said:

so if i hav an array of structures then should i declare it as......
void foo( struct trac my_trac[5] ) 

Yep, you've got the idea.
Was This Post Helpful? 0
  • +
  • -

#8 Orisiti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-June 09

Re: to pass structures as arguments in functions

Posted 09 June 2009 - 10:54 AM

I have very little experience with structs, or C++ objects (i learned procedural c++ then switched to java for oop for a few years) and i am also having troubles passing structures as parameters.
I am using the allegro library that stores bitmaps is structures that are typedef'ed (or i believe they are, as i never have had to use the struct keyword with them) i am trying to create a sprite class that takes a BITMAP struct as parameter in the constructor.
here is the prototype and constructor:
Sprite(BITMAP *, int,int,int);
...
Sprite::Sprite(BITMAP *img, int frameCount, int directions, int frameDelay){
	image=img;
	frames=frameCount;
	rotations=directions;
	delay=frameDelay;
	fWidth=((image->w)/frames);
	fHeight=((image->h)/rotations);
	curFrame = 0;
	curDelay=0;
	frameSpeed=1;
}


i am getting the following errors when trying to create a sprite:
error C2533: 'Sprite::{ctor}' : constructors not allowed a return type
error C2264: 'Sprite::Sprite' : error in function definition or declaration; function not called
error C2512: 'Sprite' : no appropriate default constructor available


i apologize if this is not a problem with how i am passing the struct and therefor in the wrong topic, but i am assuming it is, as the rest of the syntax appears sound.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1