-1

So i have a simple C file with the intention of making a pile but i keep having trouble with a function that checks if the pile is empty:

int checkEmpty(pile a)
{  
    return a->top==NULL;
} 

say that i declared a function that constructs me an empty pile with a->top=NULL
edit: giving more info i defined pile by:

typedef struct{
    info* top;
}pile_t;
typedef pile_t* pile;

info is another structure, my program compiles but it stop working when the it uses the function
edit: my bad, this is the code i am using:

typedef struct elem_t{
    int num;
    struct elem_t* next;
}elem_s;
typedef elem_s* elem;
typedef struct{
    elem top;
}pile_s;
typedef pile_s* pile;
elem consElem(int i){
    elem p=(elem)malloc(sizeof(elem_s));
    p->num=i;
    p->next=NULL;
    return p;
}
pile consPile(){
    pile a=(pile)malloc(sizeof(pile_s));
    a->top=NULL;
    return a;
}
bool checkEmpty(pile A){
    return A->top==NULL;
}
void main(){
pile A=consPile();
printf("%d",checkEmpty(A)==TRUE);
}
Oscar
  • 11
  • 3
  • What is the trouble you are having? – Fermat's Little Student Feb 08 '15 at 03:41
  • Can you post the exact error? – Kacy Feb 08 '15 at 03:44
  • 3
    If you think it gives *you* trouble, imagine it from our end, where we don't have a clue what `pile` is, or `top` either. What does "having trouble" actually *mean* ?? – WhozCraig Feb 08 '15 at 03:44
  • I think we can infer that "pile" is like a pile of things, or in other words, a stack. He's checking if the stack is empty by checking if the top element is NULL. – Kacy Feb 08 '15 at 03:51
  • 1
    Do you need `a.top==NULL`? – Spikatrix Feb 08 '15 at 03:51
  • "my program compiles" - no it doesn't; not if what you posted here is how `pile` is formally declared. The proper syntax would be as @CoolGuy demonstrated; not what you have. – WhozCraig Feb 08 '15 at 03:54
  • why? if im using pointers all the time, im using dynamic memory – Oscar Feb 08 '15 at 03:54
  • Does that look like a pointer ==> `pile a` ? – WhozCraig Feb 08 '15 at 03:55
  • true, had to edit that – Oscar Feb 08 '15 at 03:57
  • 2
    Seriously, this isn't the mighty-morphing power-code hour. Please take your time and post *real* code that *exhibits* the **real** problem. We now have two different scopes of answers, *neither* of which could be considered absolutely conclusive based on the edit history of the question. I suspect Mighty Kacy is about to hit one out of the park, but without real code we can't say. – WhozCraig Feb 08 '15 at 04:00
  • @KacyRaye it shouldn't be. Your conclusion is inescapable. A one line function that bombs on inception given only a pointer to dereference would be a pretty compelling argument that maybe the caller passed bogus data (and don't know if you got the double-ent. for the baseball analogy. WhenI saw you name [I just couldn't pass it up](http://en.wikipedia.org/wiki/Casey_at_the_Bat). – WhozCraig Feb 08 '15 at 04:07
  • @Oscar Can you post the code that actually shows where the function gets called? It'd be easier (and by easier I mean actually possible) to spot the source of your segfault. – Kacy Feb 08 '15 at 04:08
  • alright, theres most of the code, only missing the main that makes an empty file and verifys if its empty – Oscar Feb 08 '15 at 04:11
  • @Oscar "the main" is what we actually need to see. Can you add it ? – Kacy Feb 08 '15 at 04:13
  • Great, but those are all functions *that no one is calling*. Show us the *real* code that (1) calls `consPile()`, (2) called `checkEmpty()`, and do *both* of those *after* you remove the casts from your `mallocs` and ensure `#include ` is included in your source file(s). All part of posting a [**MCVE**](http://stackoverflow.com/help/mcve), stress the **C**. – WhozCraig Feb 08 '15 at 04:13
  • @Oscar I think you meant to make `elem top;` a pointer in your pile_s struct. – Kacy Feb 08 '15 at 04:16
  • @KacyRaye well in the example i did but in my code i defined it before as ´typedef elem_t* elem´ – Oscar Feb 08 '15 at 04:20
  • @KacyRaye it is (see the `elem_s` above it). One of the many *many* reasons I so-strongly discourage people from hiding pointers in typedefs ([and it has been discussed thoroughly on this very site](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers)). there are few places where it is appropriate, and I see none of them here. – WhozCraig Feb 08 '15 at 04:20
  • @WhozCraig i know :/, but as a C starter it makes the code easy for me to write and understand – Oscar Feb 08 '15 at 04:25
  • 1
    @Oscar Please, *please* don't take this the wrong way, as I've been at this with a high degree of certainly longer than you've been alive. Take that for what its worth. C programs *want to see asterisks*. They're like giant flags in the code that scream *look here! a pointer!*. There are in-practice just two places where they are appropriate: callback function form definitions, and handle-based library API's. I *promise* you your grasp of pointers and your fluency in their use will grow with considerably *less* pain. ok. enuf of that. Will you verify for me that `stdlib.h` is included?? – WhozCraig Feb 08 '15 at 04:30
  • 1
    I mean at the top of your source file(s), and *please*, *please* remove the casts in front of your `malloc` calls. [Read this for reasons why](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). I suspect (a) `stdlib.h` was *not* include in the code calling `malloc`, (b) the blind casts hid the fact that the compiler is auto generating a function decl of malloc that returns `int`, (c) you're on a x64 platform with 64-bit pointers, and finally (d) that also has 32-bit `int`s. Do you see where this trainwreck is going? – WhozCraig Feb 08 '15 at 04:31
  • @WhozCraig, 1st of all thanks a lot for your help and KacyRaye help. Yes i have included stdlib.h and stdio.h. Yes i have x64 plataform and i dont know what you mean by the d (well sizeof(int) gives me 4 bytes) and the trainwreck :L. And i will take your advice on the pointers – Oscar Feb 08 '15 at 04:40
  • @Oscar I'm going to assume you fixed your bug. Good luck with life, Oscar. And next time, post allllll the information you think we need upfront to solve your problem :) – Kacy Feb 08 '15 at 04:43
  • @KacyRaye, well its not fixed yet :P, im fixing the extra typedefs. Its so weird because i made another code for a queue and checkEmpty works – Oscar Feb 08 '15 at 04:53
  • 1
    Well i guess i found the error, it was an added library that our teacher demands to add. Again all my thanks to both of you for the time and the advices about malloc, pointers and forums. – Oscar Feb 08 '15 at 05:04
  • @Oscar sure thing, [a slightly sanitized version](http://ideone.com/i11JDG) of your code. Anyway, slow and steady. It will get there. Best of luck. – WhozCraig Feb 08 '15 at 05:08

3 Answers3

1

You used the type instead the variable name; that is, you want return a->top==NULL.

I find it hard to believe that this compiled.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • im sorry i had to write that code here because my own is in spanish, wouldnt have compiled otherwise i agree. its a->top==NULL – Oscar Feb 08 '15 at 03:43
1

I just compiled and ran your code. It works...

The output is: 1

The only thing I did was define TRUE and bool myself. And include stdlib.h and stdio.h

Kacy
  • 3,330
  • 4
  • 29
  • 57
0

looks like your program wants to return a bool instead of an int try

bool checkEmpty(pile a){  
return pile->top==NULL;}

or creating a bool variable and if statement that is true if the statement is true and vice versa

  • A bool can be promoted to an int. – Kacy Feb 08 '15 at 03:45
  • And a *type* cannot be *dereferenced*. – WhozCraig Feb 08 '15 at 03:45
  • @Oscar Changing the return type to bool or int won't change the behavior for this case since you're always returning 0 or 1. If you were returning other numbers then having a bool return type would be an issue. – Kacy Feb 08 '15 at 03:48