Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C#
struct node
{
    string bousha;
    node *next;
};

void main()
{
    string name;
    node *head=NULL;
    cout<<"Enter the name :"<<endl;
    cin>>name;
    node *temp;
    temp = (node*)malloc(sizeof(string));
    temp->bousha=name;
    temp->next=head;
    head=temp;

    cout<<"name is :"<<temp->bousha<<endl;
    getch();
}


why the list can not storage string ?????
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jul-12 18:37pm    
Where is your definition of string. Is it std::string or not?
--SA
Anderso0on 9-Jul-12 18:40pm    
yes i did
Sergey Alexandrovich Kryukov 11-Jul-12 15:11pm    
You should allocate both string and node with new, the string can also be initialized with assignment.
--SA
Philippe Mori 9-Jul-12 19:15pm    
As mentionned by SA, you pass the wrong struct to sizeof operator. Thus you don't allocate enough memory and this is why your program does not works as expected.

To start with, (node*)malloc(sizeof(string)); is just gibberish. Not only you use wrong way of allocation (you would need new), not only you do not preserve memory for node (node does not consist of the string along), not only you do not define what string (the code won't compile as is), you don't understand that the operator sizeof cannot take into account the variable-size data like string, just because the different instances of the same type take different amount of memory, depending on the string content.

I don't know how to help here except writing some working code for you, but this is not what can help you. I took a look at your past questions — it looks like you are not learning, just doing something which cannot help you to learn. You could spend about four months since your first question in a more productive way; it would be quite enough to get some very basic skills. Instead, you keep asking "what is the mean this code?". Can you finally understand that this is not productive at all? You should write all the code by yourself, and only the lines you fully understand. At first, it can be very simple, but later on you can extend your repertoire. This is impossible to do using your guesswork, so you need to read some textbook on the language and basics of programming, the simplest you can find. If you start doing it without rushing yourself, you well see considerable progress really soon.

—SA
 
Share this answer
 
v3
Comments
[no name] 9-Jul-12 19:42pm    
Yes! Especially paragraph 2.
Sergey Alexandrovich Kryukov 9-Jul-12 20:14pm    
Thank you, Wes.
--SA
Stefan_Lang 11-Jul-12 8:29am    
My 5. I agree - this is a case of basic lack of understanding, not a specific problem. A good tutorial will teach this guy more than any amount of questions of this type. And in a lot less time that it even takes to write those questions, too!
Sergey Alexandrovich Kryukov 11-Jul-12 10:31am    
Right.
Thank you, Stefan.
--SA
You should write your whole program in C++ and not part in C and other in C++.

That is, you should use new instead of malloc or even better uses STL list (std::list) instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900