![]() |
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> |
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 ... |
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 |
Re: help to send structure from a funtion.
Code:
s->=2.1; |
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. |
Re: help to send structure from a funtion.
sorry to all i am posting corrected program.
Code:
#include<iostream>I tried data *s = new data; But i didn't get the reqired result. |
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>try this..it should work the way you want.. Code:
#include<iostream> |
Re: help to send structure from a funtion.
Try to concern on your function
Code:
data *read()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. |
Re: help to send structure from a funtion.
Learn to use code block when you have code snippets in posts.
|
Re: help to send structure from a funtion.
Thanks for all.
Now i am writing the working code Code:
#include<iostream> |
| All times are GMT +5.5. The time now is 14:11. |