Problem with Pointer in Structure

Status
Not open for further replies.

veddotcom

Journeyman
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
int data ;
struct node * link ;
} ;

/* This is The Part of Programm*/
The Problem is when i m running This Program (of Linked List) Runs Properly But When i m Changing Code to As Below It Gives Error..It Says LINK is not Defined.



#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
int data ;

} ;
struct node * link ;

why i m getting problem when i defined the line (struct node * link ;) Outside the Structure....
Thank You
 

red_devil

Back!
AFAIK, the second code portion implies a pointer to the structure while the first one is part of the structure...

u r getting the error cos you are trying to use a pointer { link } which does not exist.

{ define a link inside the structure itself as you have done in the first part of the code to overcome the error}
 
OP
veddotcom

veddotcom

Journeyman
I m Agree with u That First One Declaration is Part of Structure and Second one is Pointer to Structure... NOw the Question is.. IF i have to Create a Variable Which COntains Address of Link List Then why We Can't Decalre the structure as follow :

structure node
{
int data;
int *link; /*instead of struct node *link */
}

IN this Declaration The link will also contain the address of integer type.. Plz Explain...
 

dheeraj_kumar

Legen-wait for it-dary!
You can do that, but you cant use the structure as a structure, but just as a block of memory. If you declare the link as a integer pointer, when you want to access the structure, you need to cast this integer pointer as a structure pointer which will point to your next node.

So simple, use the right datatype.

Its like this: 'A' is a character, you can store it as a integer(65 i think) but if you need to use it as a character, you need to cast it as a character.

int a = 65;
printf("%c", a);
 
Status
Not open for further replies.
Top Bottom