Skip to main content
GameDev.net gamedev.net
🔒 Locked

returning values from a c function

Started by juanpaco Nov 21, 2009 at 6:43 AM 2 replies 900+ views
Original Post
juanpaco
juanpaco
Let me preface by staying that I swear I'm not a noob to programming in general-- just apparently to C. I've done lots of C++. I'm hitting a sort of quirk, and I'm not sure why. Assume that createStack is defined after main and has prototype: int createStack(Element **stack); I have the following code:

int main (int argc, char * const argv[]) {
  Element *stack;
  createStack(&stack);
	
  int push1 = 3;

  return 0;
}
That does not compile (Visual Studio 2005, project set to strict C compiling). However, if I change that code to:

int main (int argc, char * const argv[]) {
  Element *stack;
  int res = createStack(&stack);
	
  int push1 = 3;

  return 0;
}
changing only a variable declaration that stores the result of the function call, it compiles just fine. I thought that one (for better or for worse) need not use the return value of a function, even if said function provides one. Is that a C++ "extension," or is there some other foul essence at play here? Thanks in advance for any replies. Google searching turns up a lot about how to declare functions with return types, but not whether the caller need acknowledge that there is a return value.
juanpaco
juanpaco
DAH! Nevermind!

I just remembered-- strict C, all variable declarations have to happen at the top. The difference between the 2 versions is that int res = blah counts as another variable declaration, and thus the int push1 = blah is still also at the top.

My bad. It really has been a long time since doing C. Hopefully this at least provided some amusement.

[Edited by - juanpaco on November 21, 2009 7:47:51 AM]
Cromulent
Cromulent
Quote:
Original post by juanpaco
DAH! Nevermind!

I just remembered-- strict C, all variable declarations have to happen at the top. The difference between the 2 versions is that int res = blah counts as another variable declaration, and thus the int push1 = blah is still also at the top.

My bad. It really has been a long time since doing C. Hopefully this at least provided some amusement.


Not in C99.

C99 allows you to declare variables where you like. I'd suggest setting your compiler to strict C99 mode (or if using the Microsoft C++ compiler, use a different compiler all together - the Intel compiler has good C99 support, as does GCC).
juanpaco
juanpaco
Oh cool. Thanks for the heads up. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.