DIGIT-ARC read-only archive · snapshot: July 23, 2026

Problem with Pointer in Structure

6 posts · originally from https://geek.digit.in/community/threads/problem-with-pointer-in-structure.98692/

veddotcom
veddotcom Original post #1
Sep 29, 2008 at 8:15 PM
#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
Sep 29, 2008 at 8:46 PM
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}
QwertyManiac
Sep 29, 2008 at 9:03 PM
Yes, and the question is, why are you changing it that way?
veddotcom
Oct 1, 2008 at 12:10 PM
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...
red_devil
Oct 1, 2008 at 8:41 PM
man i'm not able to understand your question..can you please be a bit more specific ?
dheeraj_kumar
Oct 1, 2008 at 9:36 PM
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);