problem during creation of object file

priyaranjan

Right off the assembly line
hi,
I have a problem in understanding the creation of object file just look at the code below. in the code below i have defined struct node in another C file. I have no any declaration of struct node in this code . But when i am compiling this code to generate the object code it is compiling.

gcc -w -c treestack.c

but when i m using struct node data in place of struct node *data it is not compiling .

can you explain why???

dont go to my code deeply just see the lines i have commented.

Code:
/* treestack.c */
 
#include<stdlib.h>
   struct treestack
   {
     struct node  * data;    // when changes this line to struct node data      
     struct treestack *link;// it doesnt compile
   };
  
  struct treestack *temp;
  struct  treestack *top=NULL;
 
  void push()
  {
   temp= (struct treestack *)malloc(sizeof(struct treestack));
   printf("enter the data you wnt to");
   scanf("%d",&temp->data);
   temp->link=top;
   top=temp;
  }
 
 
  void pop()
  {
    if (top==NULL)
   {
    printf("stack is empty");
    return;
   }
    top=top->link;
  }
 
  void travel()
  {
   struct treestack *var;
   var = top;
   while(var != NULL)
   {
    printf("values in stack are %d",var ->data);
    var =var->link;
   }

}
 

Liverpool_fan

Sami Hyypiä, LFC legend
This line is a problem.
Code:
[B]   scanf("%d",&temp->data);[/B]

I am not sure what are trying to achieve with trying to scanf a struct/pointer to struct. Obviously when you are trying to scanf a struct pointer (when you get error), it will not work because it is not supposed to work. When you pass the pointer to scanf, you are effectively passing address of the pointer, and that is the pointer to struct itself and the compiler is only letting you away with a warning (or error) depending on the implementation of the compiler. It will though crash your program eventually.
 

umeshtangnu

NULL_PTR
ya it will not compile as the compiler cannot find the "node" structure so it does not know how much memory to allocate but on the other hand when you use a pointer to the node the compiler allocates 4 byte of memory for it :)
 
Last edited:

Neuron

Electronic.
I've never used gcc.But it should give you the line number or position where the error was encountered.This will help you locate the error causing statement.Like Liverpool_fan pointed out you need to change the scanf structure when changing the datatype of the variable data.
 

umeshtangnu

NULL_PTR
I've never used gcc.But it should give you the line number or position where the error was encountered.This will help you locate the error causing statement.Like Liverpool_fan pointed out you need to change the scanf structure when changing the datatype of the variable data.
he has already given the location of the compilation error
 
Top Bottom