[Last modified March 1, 1994 by scs.]
This article is a table of contents for the comp.lang.c
frequently-asked questions (FAQ) list listing the questions
which the abridged and full versions of the FAQ list answer.
(Both lists answer all questions; the wordings of the questions
in this article are taken from the abridged list.)
If you have only just come across this article, you will
naturally be wondering where the lists which it indexes can be
found. The unabridged version is normally posted on the first of
each month, and the unabridged version twice per month, both with
Expires: lines which should keep them around all month. They can
also be found in the newsgroups comp.answers and news.answers .
Several sites archive news.answers postings and other FAQ lists,
including comp.lang.c's: two sites are rtfm.mit.edu (olectories
pub/usenet/news.answers/C-faq/ and pub/usenet/comp.lang.c/ ) and
ftp.uu.net (olectory usenet/news.answers/C-faq/ ). The archie
server should help you find others; query it for "prog C-faq".
See the meta-FAQ list in news.answers for more information.
Frequently Asked Questions in comp.lang.c
Table Of Contents
- What is this infamous null pointer, anyway?
- How do I "get" a null pointer in my programs?
- What is NULL and how is it #defined?
- How should NULL be #defined on a machine which uses a nonzero bit pattern
as the internal representation of a null pointer?
- If NULL were defined as "(char *)0," wouldn't that make function calls which pass an uncast NULL work?
- I use the preprocessor macro "#define Nullptr(type) (type *)0" to help me build null pointers of the correct type.
- Is the abbreviated pointer comparison "if(p)" to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?
- If "NULL" and "0" are equivalent, which should I use?
- But wouldn't it be better to use NULL (rather than 0) in case the value of NULL changes, perhaps on a machine with nonzero null pointers?
- I'm confused. NULL is guaranteed to be 0, but the null pointer is not?
- Why is there so much confusion
surrounding null pointers? Why do these questions come up so often?
- I'm still confused. I just can't understand
all this null pointer stuff.
- Given all the confusion surrounding null pointers, wouldn't it be easier simply to require them to be represented internally by zeroes?
- Seriously, have any actual machines really used nonzero null pointers?
- What does a run-time "null pointer assignment" error mean?
- I had the definition char a[6]
in one source file, and in another I declared extern char *a. Why didn't it work?
- But I heard that char a[] was identical to
char *a.
- So what is meant by the "equivalence of pointers and arrays" in C?
- Why are array and pointer declarations interchangeable as function formal parameters?
- How can an array be an lvalue, if you can't assign to it?
- Why doesn't sizeof properly report the size of an array
which is a parameter to a function?
- Someone explained to me that arrays were really just constant pointers.
- What is the real difference between arrays and pointers?
- I came across some "joke" code containing the "expression" 5["abcdef"].
How can this be legal C?
- My compiler complained when I passed a two-dimensional array
to a routine expecting a pointer to a pointer.
- How do I write functions which accept 2-dimensional arrays when the "width" is not known at compile time?
- How do I declare a pointer to an array?
- What's the difference between array and &array?
- How can I dynamically allocate
a multidimensional array?
- How can I use statically- and dynamically-allocated multidimensional arrays interchangeably
when passing them to functions?
- Can I simulate a non-0-based array
with a pointer?
- I passed a pointer to a function which initialized it, but the pointer in the caller was unchanged.
- I have a char * pointer that happens to point to some ints, and I want to step it over them. Why doesn't "((int *)p)++;" work?
- Can I use a void ** pointer to pass a generic pointer
to a function by reference?
- Why doesn't the code "char *answer; gets(answer);" work?
- I can't get strcat to work. I tried "char *s1 = "Hello, ", *s2 = "world!", *s3 = strcat(s1, s2);" but I got strange results.
- But the man page for strcat says that it takes two char *'s as arguments.
How am I supposed to know to allocate things?
- I have a function that is supposed to return a string, but when it returns to its caller, the returned string is garbage.
- Why does some code carefully cast the values returned by malloc
to the pointer type being allocated?
- You can't use dynamically-allocated memory after you free it, can you?
- How does free() know how many bytes
to free?
- So can I query the malloc package to find out
how big an allocated block is?
- When I free a dynamically-allocated structure containing pointers, do I have to free each subsidiary pointer first?
- Why doesn't my program's memory usage
go down when I free memory?
- Must I free allocated memory before the program exits?
- Is it legal to pass a null pointer as the first argument to realloc()?
- Is it safe to use calloc's zero-fill guarantee for pointer and floating-point values?
- What is alloca
and why is its use discouraged?
- Why doesn't the code "a[i] = i++;" work?
- Under my compiler, the code "int i = 7; printf("%d\n", i++ * i++);" prints 49. Regardless of the order of evaluation, shouldn't it print 56?
- How could the code "int i = 2; i = i++;" ever give 4?
- I just tried some allegedly-undefined
code on an ANSI-conforming compiler, and got the results I expected.
- Don't precedence and parentheses dictate order of evaluation?
- But what about the &&, ||, and comma
operators?
- If I'm not using the value of the expression, should I use i++ or ++i
to increment a variable?
- Why doesn't the code "int a = 1000, b = 1000; long int c = a * b;" work?
- What is
the "ANSI C Standard?"
- How can I get a copy
of the Standard?
- Does anyone have a tool for converting old-style C programs to ANSI C, or for automatically generating prototypes?
- How do I keep the ANSI "stringizing" preprocessing operator from stringizing the macro's name rather than its value?
- Why can't I use const values in initializers
and array dimensions?
- What's the difference between "char const *p" and "char * const p"?
- Why can't I pass a char ** to a function which expects a const char **?
- My ANSI compiler complains about a mismatch
when it sees
extern int func(float);
int func(x)
float x;
{...
- Can you mix old-style and new-style
function syntax?
- Why does the declaration "extern f(struct x {int s;} *p);" give me a warning message?
- I'm getting strange syntax errors inside code which I've #ifdeffed out.
- Can I declare main as void, to shut off these annoying "main returns no value" messages?
- Is exit(status)
truly equivalent to returning status from main?
- Why does the ANSI Standard not guarantee more than six monocase characters of external identifier
significance?
- What is the difference between memcpy and memmove?
- My compiler is rejecting the simplest possible test programs, with all kinds
of syntax errors.
- Why are some ANSI/ISO Standard library routines
showing up as undefined, even though I've got an ANSI compiler?
- Why won't frobozz-cc, which claims to be ANSI compliant, accept this code?
- Why can't I perform arithmetic on a void *
pointer?
- Is char a[3] = "abc";
legal?
- What are #pragmas
and what are they good for?
- What does #pragma once mean?
- What's the difference between implementation-defined, unspecified, and undefined behavior?
- How can I write a generic macro to swap two values?
- I have some old code that tries to construct identifiers with a macro like "#define Paste(a, b) a/**/b ", but it doesn't work any more.
- What's the best way to write a multi-statement cpp macro?
- Is it acceptable for one header file to #include another?
- Does the sizeof operator work in preprocessor #if olectives?
- How can I use a preprocessor #if expression to detect endianness?
- I've got this tricky processing I want to do at compile time and I can't figure out a way to get cpp to do it.
- How can I preprocess some code to remove selected conditional compilations, without preprocessing everything?
- How can I list all of the pre#defined identifiers?
- How can I write a cpp macro which takes a variable number of arguments?
- How can I write a function that takes a variable number of arguments?
- How can I write a function that takes a format string and a variable number of arguments, like printf, and passes them to printf to do most of the work?
- How can I discover how many arguments a function was actually called with?
- I can't get the va_arg macro to pull in an argument of type pointer-to-function.
- How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?
- How can I call a function with an argument list built up at run time?
- What is the right type to use for boolean values in C? Why isn't it a standard type? Should #defines or enums be used for the true and false values?
- What if a built-in boolean or relational operator "returns" something other than 1?
- What is the difference between an enum and a series of preprocessor #defines?
- I heard that structures could be assigned to variables and passed to and from functions, but K&R I says not.
- How does struct passing and returning work?
- I have a program which works correctly, but dumps core after it finishes. Why?
- Why can't you compare structs?
- How can I read/write structs from/to data files?
- I came across some code that declared a structure
with the last member an array of one element, and then did some tricky
allocation to make the array act like it had several elements.
Is this legal and/or portable?
- How can I determine the byte offset of a field within a structure?
- How can I access structure fields by name at run time?
- Why does sizeof report a larger size than I expect for a structure type, as if there was padding at the end?
- How can I turn off structure padding?
- Can I initialize unions?
- Can I pass constant values to routines which accept struct arguments?
- How do you decide which integer type to use?
- What should the 64-bit type on new, 64-bit machines be?
- I can't seem to define a linked list node which contains a pointer to itself.
- How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
- How can I declare a function that returns a pointer to a function of its own type?
- My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once.
- What's the best way to declare and define global variables?
- What does extern mean in a function declaration?
- How do I initialize a pointer to a function?
- I've seen different methods used for calling through pointers to functions.
- What's the auto keyword good for?
- Why doesn't the code "char c; while((c = getchar()) != EOF)..." work?
- Why doesn't the code scanf("%d", i); work?
- Why doesn't the code double d; scanf("%f", &d); work?
- Why won't the code "while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); }" work?
- Why does everyone say not to use gets()?
- Why does errno contain ENOTTY after a call to printf?
- My program's prompts and intermediate output don't always show up on the screen, especially when I pipe the output through another program.
- When I read from the keyboard with scanf, it seems to hang until I type one extra line of input.
- I'm trying to update a file in place, by using fopen mode "r+", but it's not working.
- How can I read one character at a time, without waiting for the RETURN key?
- Will fflush(stdin) flush unread characters from the standard input stream?
- How can I redirect stdin or stdout from within a program?
- Once I've used freopen, how can I get the original stdout back?
- How can I recover the file name given an open file descriptor?
- Why does strncpy not always write a '\0'?
- I'm trying to sort an array of strings with qsort, using strcmp as the comparison function, but it's not working.
- Now I'm trying to sort an array of structures with qsort. My comparison routine takes pointers to structures, but the compiler complains that the function is of the wrong type for qsort. How can I cast the function pointer to shut off the warning?
- How can I convert numbers to strings?
- How can I get the time of day in a C program?
- How can I convert a struct tm or a string into a time_t?
- How can I perform calendar manipulations?
- I need a random number generator.
- How can I get random integers in a certain range?
- Each time I run my program, I get the same sequence of numbers back from rand().
- I need a random true/false value, so I'm taking rand() % 2, but it's just alternating 0, 1, 0, 1, 0...
- I'm trying to port this old program. Why do I get "undefined external" errors for some library routines?
- I get errors due to library routines being undefined even though I #include the right header files.
- I'm still getting errors due to library routines being undefined, even though I'm requesting the right libraries.
- I need some code to do regular expression matching.
- How can I split up a string into whitespace-separated arguments?
- I just typed in this program, and it's acting strangely. Can you see anything wrong with it?
- How can I shut off the "warning: possible pointer alignment problem" message lint gives me for each call to malloc?
- Where can I get an ANSI-compatible lint?
- Is the code "if(!strcmp(s1, s2))" good style?
- What's the best style for code layout in C?
- Where can I get the "Indian Hill Style Guide" and other coding standards?
- My floating-point calculations are acting strangely and giving me different answers on different machines.
- I keep getting "undefined: _sin" compilation errors.
- Where is C's exponentiation operator?
- How do I round numbers?
- How do I test for IEEE NaN and other special values?
- I'm having trouble with a Turbo C program which crashes and says something like "floating point formats not linked."
- How can I read a single character from the keyboard without waiting for a newline?
- How can I find out if there are characters available for reading (and if so, how many)? Alternatively, how can I do a read that will not block if there are no characters available?
- How can I clear the screen?
- How do I read the mouse?
- How can my program discover the complete pathname to the executable file from which it was invoked?
- How can a process change an environment variable in its caller?
- How can I check whether a file exists?
- How can I find out the size of a file, prior to reading it in?
- How can a file be shortened in-place without completely clearing or rewriting it?
- How can I implement a delay, or time a user's response, with sub-second resolution?
- How can I read in an object file and jump to routines in it?
- How can I invoke an operating system command from within a program?
- How can I invoke an operating system command and trap its output?
- How can I read a directory in a C program?
- How can I do serial ("comm") port I/O?
- What can I safely assume about the initial values of variables which are not explicitly initialized?
- What's wrong with
f() { char a[] = "Hello, world!"; }
?
- How can I write data files which can be read on other machines with different data formats?
- How can I delete a line from the middle of a file?
- How can I return several values from a function?
- How can I call a function, given its name as a string?
- I seem to be missing the system header file <sgtty.h>. Can someone send me a copy?
- How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP) functions from C?
- Does anyone know of a program for converting Pascal or FORTRAN to C?
- Can I use a C++ compiler to compile C code?
- I'm looking for C development tools (cross-reference generators, code beautifiers, etc.).
- Where can I get copies of all these public-domain programs?
- When will the next Obfuscated C Code Contest be held? How can I get a copy of the previous winning entries?
- Why don't C comments nest? Are they legal inside quoted strings?
- How can I get the ASCII value corresponding to a character?
- How can I implement sets and/or arrays of bits?
- What is the most efficient way to count the number of bits which are set in a value?
- How can I make this code more efficient?
- Are pointers really faster than arrays? How much do function calls slow things down?
- Why does the code "char *p = "Hello, world!"; p[0] = tolower(p[0]);" crash?
- This program crashes before it even runs!
- What does "Segmentation violation" mean?
- My program is crashing, apparently somewhere down inside malloc.
- Does anyone have a C compiler test suite I can use?
- Where can I get a YACC grammar for C?
- I need code to parse and evaluate expressions.
- I need to compare two strings for close, but not necessarily exact, equality.
- How can I find the day of the week given the date?
- Will 2000 be a leap year?
- How do you pronounce "char"?
- What's a good book for learning C?
- Are there any C tutorials on the net?
- Where can I get extra copies of this list?
Steve Summit
scs@eskimo.com
This article is Copyright 1994 by Steve Summit.
It may be freely redistributed so long as the author's name, and this
notice, are retained.