global scope problem

Discussion in 'C' started by k0der, Apr 16, 2009.

  1. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    i have a structure
    struct node
    {
    int element;
    struct node* next;
    };

    in one file "tree_stack.c"
    i have other file check_stack.c in which stack is implemented.This file contains the structure
    struct st
    {
    struct node* nd;
    struct st* nxt;
    }

    i have to use struct node in this check_stack.c file.So i have included this file in "tree_stack.c".
    i have used it as extern struct node *nd.
    but when i try to access a data
    like
    curr->nd->element // produces error
    where curr is
    struct st* curr;

    i am getting error of smth like "accessing incomplete data ".
    anyone got clue??
    guide me where i am wrong and how to correct it.
    thanks
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    i did't get your problem plaese clearify more ...
    whatever i got see this code

    In File "tree_stack.c"
    Code:
    #include<stdio.h>
    
    struct node
    {
     int element;
     struct node* next;
    };
    in File "tree_stack.c"

    Code:
    #include <stdio.h>
    #include "test.c"
    
    extern struct node* nd;
    struct st
    {
     struct node* nd;
     struct st* nxt;
    };
    
    int main()
    {
      struct st* curr;
      int data=curr->nd->element=10;
      printf("%d",data);
      
      return 0;
    }
    But this is not giving error ...
    Try it or any thing else reply ...
    Note: i have not allocated on heap, this is just for your answer...
     
  3. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thanks again for replying..
    actually my struct isa someth like..

    in file stack_check.c
    #include<stdio.h>

    struct node
    {
    int element;
    struct node* next;
    };
    ....
    ....
    ....


    and in other file where i am actually implementing stack for tree operations like storing the nodes of the tree onto the stack in file name tree_stack.c .
    the rudimentary file is smth like...(just modifying in your code)

    extern struct node* nd;
    struct st
    {
    struct node* nd;
    struct st* nxt;
    };

    void display_stack()
    {
    struct st* curr;
    while(....)
    printf("%d",curr->nd->element); // here its showing error

    return 0;
    }
    thanks again.. :)
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    So, you've removed half the code (probably including the code that has the actual problem) and the error message, and you want us to solve the problem?

    As has been pointed out, there is nothing intrinsically wrong with curr->nd->element, so something else must be wrong.

    What error is it showing? Are you getting a compile-time error or a runtime error?

    It's not a good idea to #include C files in C files. Rename stack_check.c to stack_check.h and make sure it only has header stuff in it. Better still, merge everything into one C file, solve the problems, then you can split it back out again as necessary.
     
  5. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thank again for help :)
    i have actually given the entire susceptible code which might cause problem.and believe me it that simple as i have wrote. its compile time error.
    anyways i am really sorry that i dont have code now ..i will surely post it next time when i come to browse internet.any i really apologize for that.
    i just included the file as .c .i know that .h is advisable.But may i know any good reason for that will create problem later on?
    thanks again.. ;)
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's just a matter of using the correct labels. h files can be included anywhere; you don't need to worry about their contents and the assumption made by just about everyone will be that a .h file doesn't contain any code. #including a file that contains code will work once, but after that you'll get compiler errors (likely duplicate symbols), so it's best to stick to what works. Compile C files and #include H files.

    It's like putting acid in a bottle labelled Water. Fine as long as you know that, but the moment you forget, or the moment someone else comes along looking for a drink, then you have a problem.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Because the stuff you've posted isn't complete it's impossible to work out what's really going wrong. What you need to do therefore is to strip this down to a MINIMAL but COMPLETE set of files then post the filename and COMPLETE file contents, then we stand a chance of working out what is up.

    So for example I've got the following code:
    thread46040.cpp:
    Code:
    // thread46040.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "tree_stack.c"
    #include "check_stack.c"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	display_stack();
    	return 0;
    }
    
    tree_stack.c:
    Code:
    extern struct node* nd;
    struct node
    {
    int element;
    struct node* next;
    };
    
    void display_stack()
    {
    struct st* curr;
    // while(....)
    printf("%d",curr->nd->element); // here its showing error
    
    return;
    }
    
    and check_stack.c:
    Code:
    struct st
    {
    struct node* nd;
    struct st* nxt;
    }
    
    and I'm getting various errors:
    Code:
    1>------ Build started: Project: thread46040, Configuration: Debug Win32 ------
    1>Compiling...
    1>thread46040.cpp
    1>c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\tree_stack.c(12) : error C2027: use of undefined type 'display_stack::st'
    1>        c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\tree_stack.c(10) : see declaration of 'display_stack::st'
    1>c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\tree_stack.c(12) : error C2227: left of '->nd' must point to class/struct/union/generic type
    1>c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\tree_stack.c(12) : error C2227: left of '->element' must point to class/struct/union/generic type
    1>c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\thread46040.cpp(8) : error C2628: 'st' followed by 'int' is illegal (did you forget a ';'?)
    1>c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\thread46040.cpp(9) : error C3874: return type of 'wmain' should be 'int' instead of 'st'
    1>c:\documents and settings\dave\my documents\visual studio 2008\projects\go4e\thread46040\thread46040.cpp(11) : error C2440: 'return' : cannot convert from 'int' to 'st'
    1>        No constructor could take the source type, or constructor overload resolution was ambiguous
    
    but I have no way of knowing if any of these correspond to any of the errors you're getting.

    Both me and asadullah have now tried - and failed - to construct your code from the sparse descriptions you have given, so I think it's only fair now that you realise you must strip (an experimental copy of, if necessary) the program down to the bare minimum and show us exactly what is going wrong.

    Also you need to post the EXACT errors, like I have done above. It's no use saying vague stuff like "error of smth like "accessing incomplete data "" and "here its showing error" - these could both mean absolutely anything and give no clue at all as to what is wrong.
     
  8. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    there are two files:

    Code:
    /************ stack.check.c   */
    #include<stdio.h>
    #include"tree_stack.c"
    
    struct node
    {
    	int element;
    	struct node* next;
    };
    main()
    {
    	int ch;
    	ch='y';
    	int opt;
    	
    	struct node* tmp;
    
    	do {
    	       printf("\n************** MENU **********\n");
            	printf("\n\t1.add node\n\t2.Delete node \n\t3.display Stack\n");
    		scanf("%d",opt);
    		switch(opt)
    		{
    			case 1: 
    				if( (tmp=(struct node*)malloc(sizeof(struct node*) )) == NULL)
    				{	printf("memory error!!");   break;}
    				printf("\nEnter the node element:\t");
    				scanf("%d",tmp->element);
    				tmp->next=NULL;
    			        tree_push(tmp);        	
    				break;
    			case 2:
    				tree_pop();
    				break;
    			case 3:
    				tree_display();
    				break;
    		}
    	printf("\ndo you wish to continue? [y/n]\t");
    	scanf("%c",ch);
            }while(ch=='y');
    }
    
    
    and then 
    
    **************tree_stack.c **********8888
    
    #include<stdlib.h>
    
    extern struct node nd;
    struct st
    {
    	struct node* nd;
    	struct st* next; 
    };
    struct st* top=NULL;
    //struct st* tmp;
    
    
    void tree_push(struct node* tnode)
    {
     	struct st* tmp; 
      	 if( (tmp=(struct st*)malloc(sizeof(struct node*)))== NULL )
    		printf("stack overflow!!\n");
             else
         	{
    
         		tmp->nd=tnode;
    		tmp->next=top;
    		top=tmp;
    		printf("\n node successfully pushed!!\n");      	
             }
    }
    
    
     struct node* tree_pop()
    {	
    	struct st* curr;
    	extern struct node* n; 
    	if(top==NULL)		
    	{	
    		printf("\n stack UnderFlow!!\n");
            	return NULL;
    	}
    	curr=top;
    	n=curr->nd;
    	top=top->next;
    	free(curr);
    	printf("\n stack popped!!\n ");
            return n;
    }
    
    void tree_display()
    {
    	struct st* curr;
    	if(top==NULL)
    		printf("\nstack empty!!\n");
    	else
    	{
    		curr=top;
    		while(curr != NULL)
    		{
    			printf("\t%d\t",curr->nd->element);
    		}
    	}
    }
    
    and error i am getting is :
    %gcc -g -o stack_check stack_check.c
    In file included from stack_check.c:2:
    tree_stack.c: In function `tree_display':
    tree_stack.c:56: error: dereferencing pointer to incomplete type
    %
    now i hope i have given the complete prob of mine ..thanks for help
     
    Last edited by a moderator: May 5, 2009
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Try moving #include"tree_stack.c" after the struct node definition in stack_check.c.
    The error occurs because the compiler doesn't know what struct node is, and that's because it hasn't been defined yet.
     
  10. k0der

    k0der New Member

    Joined:
    Apr 14, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thanks buddy.. that worked!!! :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice