![]() |
global scope problem
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 |
Re: global scope problem
i did't get your problem plaese clearify more ...
whatever i got see this code In File "tree_stack.c" Code:
#include<stdio.h>Code:
#include <stdio.h>Try it or any thing else reply ... Note: i have not allocated on heap, this is just for your answer... |
Re: global scope problem
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.. :) |
Re: global scope problem
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. |
Re: global scope problem
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.. ;) |
Re: global scope problem
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. |
Re: global scope problem
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.Code:
extern struct node* nd;Code:
struct stCode:
1>------ Build started: Project: thread46040, Configuration: Debug Win32 ------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. |
Re: global scope problem
there are two files:
Code:
/************ stack.check.c */%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 |
Re: global scope problem
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. |
Re: global scope problem
thanks buddy.. that worked!!! :)
|
| All times are GMT +5.5. The time now is 17:04. |