simplest struct code

#include<iostream.h>
#include<conio.h>
struct node{ char info;
node * next;
} ;
node *abc=new node;
abc->info='k';
void main()
{ clrscr();
cout<<abc->info<<" bbb "<<abc->next;
getch();
}

Whats the problem in this code?
<iostream.h> does not exist (was deprecated and removed decades ago) - use <iostream>

<conio.h> does not exist (non-standard)

main must return int

You never assign to abc->next so it just has a random garbage value.
Last edited on
Moving my response here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Don't be afraid of whitespace.  I added new lines to make the code look less cramped.
//  It makes it easier to read and follow

#include <iostream> // <- the header is <iostream>, not <iostream.h>
#include <conio.h> // <- nonstandard header, but that's OK as long as your environment has it.
    // Just know that not everyone's environment will have this header available.

using namespace std; // <- if you want to use 'cout' without the 'std::' prefix, you need
    // to dump it into the global namespace

struct node{ char  info;
	     node * next;
	     } ;

node *abc=new node;
//abc->info='k'; // <- you can't put executable code globally.  It must go in a function.
int main() // <- main must return an int.  Not a void.
{
    abc->info = 'k'; // <- you can do this inside main.
    clrscr();
    cout<<abc->info<<" "<<abc->next; // <- abc->next will be garbage because you never
        // initialized it.
    getch();

    delete abc;  // <- anything you 'new' you must 'delete' or you get a memory leak.
}
It seems that you are trying to make a linked list datastructure in C. Here is little tutorial for that http://www.cprogramming.com/tutorial/c/lesson15.html

It would be nice, if you told us what you expect that piece of code to do.
Looks safe to create a destructor that deallocates any memory pointed to by node::next.

Aceix.
In my TurboC4 compiler, header files don't get included without ".h".
There is compatibility issue between my computer and compiler as after sometime mouse pointer freezes and then i have to run all commands from keyboard only.
Please give me suitable link to download most suitable c compiler. my system is "Windows8 64 bit". I have tried almost all "Google search result".
Thank u!!
The compiler you are using is severely outdated and hasn't been updated in about a decade. Please, please, please switch to a modern compiler. Anything from the last few years will do.
please reply with "compiler download link" suitable for my computer !!
Topic archived. No new replies allowed.