Frequently Asked Questions about General Programming


Q.
I have a program that works fine if I enter commands manually. However, if I use input redirection, it messes up. My program flushes the input buffer to get rid of newlines in the input.

A.
fflush() is for output files only. Its behavior is undefined for input files. Since the newline (\n) is a character like any other character, add a command to read this extra character (you don't have to do anything with it, just ignore it).


Q.
How can we declare a variable as a pointer to a pointer?

A.
Well, for example...

int *ip;

says that "ip" is a pointer (the star) to an int. I.e., the star says that it is a pointer to the type to its left (int). So,

int **ipp;

would be a pointer (right-most star) to a pointer (left star) to an int.


Q.
How come after I free() some memory I can still access it?

A.
When you free() memory, you are essentially just promising not to use that memory anymore (i.e., it allows a call to malloc() to give that memory away again)... If you try to access some memory that has been freed, you often can (although it may depend on exactly how dynamic allocation is implemented on a system), since the memory doesn't go anywhere, it's just that you're not technically allowed to use it anymore.

So, you should not try to access memory you've freed.


Q.
How does one read something like:
typedef A *B;

A.
That typedef defines "B" to be a new type, which is the type of a pointer to A. I.e., the following 2 would now be equivalent:

A *ap;
B ap;


Q.
Is it ok to call a function that returns a value and ignore the return value? For example,
int f1(void);
.
.
.
void foo(void)
{
  f1();
}

A.
That works just fine. We do it all the time...For example, printf() returns an int, but that return value is rarely used.


Q.
Is
return (a == b);

equivalent to:

if (a == b)
  return 1;
else
  return 0;

? I.e., 1 is returned if the == statement is true, 0 otherwise?

A.
Yes.


Q.
Does the "default" case in a switch always get executed, or just when none of the cases apply?

A.
It is executed when none of the cases preceding it apply. Or when the case right before it lacks a "break".


Q.
The compiler always complains "function whatever should return a value" or "control reaches end of non-void function" for functions like:
int whatever(...)
{
  if (...) {
    ...
    return x;
  }

  if (...) {
    ...
    return y;
  }

  /* No return here. */
}

A.
The problem is that there is some point in your function that could be gotten to that does not return a value. Since the function says it returns a value (i.e, the function's return type is not void), it must always (i.e., under every circumstance) do so.


Q.
A prototype for a function we are suppose to write is:
void StackPush(stackT *, stackElementT);
Can we give parameters names (such as stackT *stackP)? If not, how are we supposed to refer to it in code?

A.
Prototypes don't need parameter names, though you should put them if it adds to readability (not necessary here since the types stackT and stackElementT are quite explicit). The function definition, however, will need parameter names, and you should choose something reasonable for those.


Q. [New]
I can't get code where I call a boolean function (i.e., returns a true/false value) to work. I use something like:
if (isdigit(c) == 1)
  ...

A.
You've assumed that the boolean function returns 1 for a true value. Remember that many testing functions just return some true value on truth, not necessarily 1.

You should use idioms like:

if (isdigit(c))
  ...

if (!isdigit(c))
  ...
rather than comparing to 1 or 0.


BU CAS CS 113 - FAQ - General Programming