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; }
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 ...
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
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.
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.
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; }
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.
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
@basha.. HEHE..u are right..i tried the code on Windows using Visual Studio 2003 and Studio 2005.. there we need to include the header file <string> ... or the above program results in compilation error..!! i then tried it on Linux and the code is working fine there.. and if header file is not a problem..then ur code shud have worked with just the change Salem has told in his earlier post..