Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   help to send structure from a funtion. (http://www.go4expert.com/forums/help-send-structure-funtion-t8307/)

bashamsc 18Jan2008 07:46

help to send structure from a funtion.
 
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;
}


technosavvy 18Jan2008 14:04

Re: help to send structure from a funtion.
 
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 ...

bashamsc 18Jan2008 14:35

Re: help to send structure from a funtion.
 
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

technosavvy 18Jan2008 14:40

Re: help to send structure from a funtion.
 
Code:

s->=2.1;
s->="basha"

how come this is compilable friend :) :D

Salem 18Jan2008 17:21

Re: help to send structure from a funtion.
 
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.

bashamsc 21Jan2008 09:49

Re: help to send structure from a funtion.
 
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.

technosavvy 21Jan2008 10:33

Re: help to send structure from a funtion.
 
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;
}


SudeshPower 21Jan2008 16:00

Re: help to send structure from a funtion.
 
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.

shabbir 21Jan2008 17:27

Re: help to send structure from a funtion.
 
Learn to use code block when you have code snippets in posts.

bashamsc 23Jan2008 10:38

Re: help to send structure from a funtion.
 
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


All times are GMT +5.5. The time now is 01:08.