1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help to send structure from a funtion.

Discussion in 'C++' started by bashamsc, Jan 18, 2008.

  1. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    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 a moderator: Jan 18, 2008
  2. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    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 ...
     
  3. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    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
     
  4. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    s->=2.1;
    s->="basha"
    how come this is compilable friend :) :D
     
  5. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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.
     
  6. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    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 a moderator: Jan 21, 2008
  7. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
  8. SudeshPower

    SudeshPower New Member

    Joined:
    May 29, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jan 21, 2008
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Learn to use code block when you have code snippets in posts.
     
  10. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    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 a moderator: Jan 23, 2008
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    bashamsc, please use code blocks to use in posts for code in the posts.
     
  12. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    @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..
     

Share This Page