-1

I am learning link list through book and in one book head pointer is initialised globally and in another its initialised within main and then passed to create. Is there any advantage of declaring it in main.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void create(void);
void traverse(void);
void create()
{

char ch;
struct node *ptr,*cpt;
ptr=(struct node*) malloc(sizeof(struct node));
if(ptr==NULL)
{
    printf("Memory cant be allocated");

}
printf("Enter information you want to store in node.\n");
scanf("%d",&ptr->info);
first=ptr;
do
   {
    cpt=(struct node*) malloc(sizeof(struct node));
    if (cpt==NULL)
    printf("Can't allocate memory");
    printf("Enter next node information.");
    scanf("%d", &cpt->info);
    ptr->link=cpt;
    ptr=cpt;
    printf("Do you want to enter another node?(y/n)");
    ch=getch();
}while(ch=='y');
ptr->link = NULL;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Sulabh Kumar
  • 101
  • 7

1 Answers1

0

Memory model in c is as below

Global variables go into `BSS (Block start with Symbols) region`

Global initialized variables go into `Initialized data region`

Scope limited variables go into `Stack`

The variable declared inside main are by default have storage classifier automatic. Hence They all go into stack. But if you have static variables inside main (or anywhere), they'll go into 'BSS'.

So this makes the difference. Huge amount of data in main requires large stack

nmxprime
  • 1,506
  • 3
  • 25
  • 52
  • THanks but found what i was looking for on another website:http://www.geeksforgeeks.org/how-to-write-functions-that-modify-the-head-pointer-of-a-linked-list/ – Sulabh Kumar Feb 02 '14 at 11:39
  • So if you post what you've found, that'd be helpful for someone in search IMHO – nmxprime Feb 03 '14 at 04:01