help to send structure from a funtion.

bashamsc's Avatar, Join Date: May 2007
Go4Expert Member
I am trying the following program to send a structure but i am not able to get the result what i want . Can any one tell me what changes i have to do to the following program.

Code:
#include<iostream>
using namespace std;

struct data
{
int a;
float b;
string name;
}

data *read();

main()
{
data *p;

p=read();

cout<<data->a<<data->b<<data->name<<endl;

}

data *read()
{
data *s;
data x;
s=&x;
s->a=1;
s->=2.1;
s->="basha";
return s;
}

Last edited by shabbir; 18Jan2008 at 09:16.. Reason: Code block
0
technosavvy's Avatar
Contributor
leave about whether the code is giving right results or not..

at least post something which can be compiled...
please post a code which is compilable and then ask your question ...
0
bashamsc's Avatar, Join Date: May 2007
Go4Expert Member
It can be compilable but not geting the result what i am looking for.

If i print the output in the function i can get but not in main.

I am asking what i have to change to get the correct output
0
technosavvy's Avatar
Contributor
Code:
s->=2.1;
s->="basha"
how come this is compilable friend
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
Syntax errors aside, the main problem with your read() function is that you're returning a pointer to a local variable.
When the function returns, the variable goes out of scope, and your pointer is now pointing at junk.

data *s = new data;
would allocate you some space which would survive the function return, but you then need to delete the memory after you've finished with it.

static data x;
would also fix your code, but brings a whole set of other problems along with it.
0
bashamsc's Avatar, Join Date: May 2007
Go4Expert Member
sorry to all i am posting corrected program.
Code:
#include<iostream>
using namespace std;
                                                                                
struct data
{
int a;
float b;
string name;
};
                                                                                
data *read();
                                                                                
main()
{
data *p;
                                                                                
p=read();
                                                                                
cout<<p->a<<endl;
                                                                                
cout<<p->b<<endl;
                                                                                
cout<<p->name<<endl;
}
data *read()
{
data *s;
data x;
s=&x;
s->a=1;
s->b=2.110;
s->name="basha";
cout<<s->a<<endl;
cout<<s->b<<endl;
cout<<s->name<<endl;
return s;
}
This can be compilable. I can result in the function.

I tried data *s = new data;

But i didn't get the reqired result.

Last edited by shabbir; 21Jan2008 at 17:25.. Reason: Code block
0
technosavvy's Avatar
Contributor
hi bashamsc.. even this code is not compiling on my machine... i think u have just missed ..
Code:
#include <string>
in your code

try this..it should work the way you want..
Code:
#include<iostream>
#include <string>
using namespace std;

struct data
{
int a;
float b;
string name;
};

data *read();

int main() {
data *p;
p=read();
cout<<p->a<<endl;
cout<<p->b<<endl;
cout<<p->name<<endl;
}


data *read() {
data *s = new data;
s->a=1;
s->b=2.110;
s->name = "basha";
cout<<s->a<<endl;
cout<<s->b<<endl;
cout<<s->name<<endl;
return s;
}
0
SudeshPower's Avatar, Join Date: May 2007
Newbie Member
Try to concern on your function
Code:
data *read()
{
  data *s;
  data x;
  s=&x;
  s->a=1;
  s->b=2.110;
  s->name="basha";
  cout<<s->a<<endl;
  cout<<s->b<<endl;
  cout<<s->name<<endl;
  return s;
}
Here you are creating a local variable name
data x;
All the memory allocated to this function will be deleted after the return of the function. You are keeping its address in another pointer 'x'. Although address is returned but it still prints some garbage values..


Try following script for allocation of data
x = new data;
This pointer will hold its memory even after returning from the function.

Last edited by shabbir; 21Jan2008 at 17:26.. Reason: Code block
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Learn to use code block when you have code snippets in posts.
0
bashamsc's Avatar, Join Date: May 2007
Go4Expert Member
Thanks for all.

Now i am writing the working code

Code:
#include<iostream>
using namespace std;

struct data
{
int a;
float b;
string name;
};

data *read();

main()
{
data *p;

p = read();

cout<<p->a<<endl;

cout<<p->b<<endl;

cout<<p->name<<endl;

}
data *read()
{
data *s ;
s = new data;
s->a=1;
s->b=2.110;
s->name="basha";
cout<<s->a<<endl;
cout<<s->b<<endl;
cout<<s->name<<endl;
return s;
}
Hello tecchnosavvy i have compiled this code in Linux platform. If u r trying in another platform make some changes

Last edited by shabbir; 23Jan2008 at 15:36.. Reason: Code block