how do u put a struct from main to a function so that the function
would display it?
how do u put a struct from main to a function so that the function
would display it?
hope i helped youCode:#include <stdio.h> #include <iostream.h> #include<string.h> struct myStruct{ int m; int c; }; void myFunc(myStruct &); //prototype for function int main(int argc, char *argv[]) { myStruct s1; myFunc(s1); //pass by refrence cout<<"S1.m= "<<s1.m<<endl; cout<<"S1.c= "<<s1.c<<endl; cin>>"press enter"; return 0; } void myFunc(myStruct & refStruct){ refStruct.m=5; refStruct.c=10; return; }
Programming is a high logical enjoyable art for both programer and user !!
thanx,
what's refstruct?
but i cant use this for any structure
refStruct ----> is a reference (check up using references in any book or tutorial).........
I have to pass the structure as reference not by value from main to the fuction as passing by value will occure error coz the structure ll be out of scope (as the structure ll be local in the main)
so passing by reference (using the operator &) ll solve this problem .
sure you can use this way for any structure (give me exact problem , I ll show you how to solve it )
good luck
Programming is a high logical enjoyable art for both programer and user !!
well try this
myfunc(take any struct)
{
//do stuff
}
what u did was something like
myfunc(existing struct )
do u get my point?
execuse me.........i cant get your point , u may clear urslef more , please
Programming is a high logical enjoyable art for both programer and user !!
pode, you are misunderstanding the situation.
myFunc(myStruct &), means that 'myFunc' accepts a reference
to a structure. It can be any structure.
I think that you should read up about function parameters and
arguments.
this is a complete chapter from "teach yourself c++"
about references, all u need about this topic ll be answered in this chapter.its very clear and full of examples, read it carefully, im ready to help u if u missunderstand anything within .......
alright??? good luck
Programming is a high logical enjoyable art for both programer and user !!
Do you mean that you want to print contents of any struct? Then you need to use templates and provide a method that prints the contents in the struct so that the function can call it.
No, you can't. Not for ANY structure at least.Originally posted by pode
but i cant use this for any structure
You have to know what type of structure you are dealing with to be able to use it.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
If you had declared an array of structures you wouldn't of needed to pass it by reference, as the address is automatically passed when dealing with arrays. Is this right?
Be a leader and not a follower.