<< < 164 165 166 167 168 169 170 171 172 173 174 > >>   Sort: Date

This program crashes before it even runs!
This program crashes before it even runs! (When single-stepping with a debugger, it dies before the first statement in main.) You probably have one or more very large (kilobyte or more) local arrays. Many systems have fixed-size stacks, and even those which perform dynamic stack allocation automatic...
2015-05-29, 1180👍, 0💬

I have got some code that is trying to unpack external structures
I've got some code that's trying to unpack external structures, but it's crashing with a message about an ``unaligned access.'' What does this mean? The code looks like this: struct mystruct { char c; long int i32; int i16; } s; char buf[7], *p; fread(buf, 7, 1, fp); p = buf; s.c = *p++; s.i32 = *(l...
2015-05-20, 1180👍, 0💬

How can I trap or ignore keyboard interrupts like control-C?
How can I trap or ignore keyboard interrupts like control-C? The basic step is to call signal, either as #include &lt;signal.h> signal(SIGINT, SIG_IGN); to ignore the interrupt signal, or as extern void func(int); signal(SIGINT, func); to cause control to transfer to function func on receipt of ...
2015-03-06, 1180👍, 0💬

How can I return multiple values from a function?
How can I return multiple values from a function? There are several ways of doing this. (These examples show hypothetical polar-to-rectangular coordinate conversion functions, which must return both an x and a y coordinate.) 1. Pass pointers to several locations which the function can fill in: #incl...
2015-02-25, 1180👍, 0💬

How can I swap two values without using a temporary?
How can I swap two values without using a temporary? The standard hoary old assembly language programmer's trick is: a ^= b; b ^= a; a ^= b; But this sort of code has little place in modern, HLL programming. Temporary variables are essentially free, and the idiomatic code using three assignments, na...
2015-02-06, 1179👍, 0💬

People keep saying that the behavior of ...
People keep saying that the behavior of i = i++ is undefined, but I just tried it on an ANSI-conforming compiler, and got the results I expected. A compiler may do anything it likes when faced with undefined behavior (and, within limits, with implementation-defined and unspecified behavior), includi...
2015-11-18, 1177👍, 0💬

How can my program discover the complete pathname to the executable from which it was invoked?
How can my program discover the complete pathname to the executable from which it was invoked? argv[0] may contain all or part of the pathname, or it may contain nothing. You may be able to duplicate the command language interpreter's search path logic to locate the executable if the name in argv[0]...
2015-03-13, 1176👍, 0💬

Is it acceptable for one header file to #include another?
Is it acceptable for one header file to #include another? It's a question of style, and thus receives considerable debate. Many people believe that ``nested #include files'' are to be avoided: the prestigious Indian Hill Style Guide disparages them; they can make it harder to find relevant definitio...
2016-02-16, 1175👍, 0💬

I inherited some code which contains far too many ...
I inherited some code which contains far too many #ifdef's for my taste. How can I preprocess the code to leave only one conditional compilation set, without running it through the preprocessor and expanding all of the #include's and #define's as well? There are programs floating around called unifd...
2016-01-29, 1175👍, 0💬

I had a frustrating problem which turned out to be caused by the line ....
I had a frustrating problem which turned out to be caused by the line printf("%d", n); where n was actually a long int. I thought that ANSI function prototypes were supposed to guard against argument type mismatches like this. A:When a function accepts a variable number of arguments, its prototype d...
2016-01-11, 1175👍, 0💬

I have a pre-ANSI compiler, without stdarg.h What can I do?
I have a pre-ANSI compiler, without stdarg.h What can I do? There's an older header, &lt;varargs.h>, which offers about the same functionality. Here is the vstrcat function, rewritten to use &lt;varargs.h>: #include &lt;stdio.h> #include &lt;varargs.h> #include &lt;string.h> exte...
2015-06-10, 1174👍, 0💬

Why is this loop always executing once?
Why is this loop always executing once? for(i = start; i &lt; end; i++); { printf("%d\n", i); } A: The accidental extra semicolon hiding at the end of the line containing the for constitutes a null statement which is, as far as the compiler is concerned, the loop body. The following brace-enclos...
2015-06-03, 1174👍, 0💬

I came across some code that puts a (void) cast before each call to printf. Why?
I came across some code that puts a (void) cast before each call to printf. Why? printf does return a value (the number of characters printed, or an error code), though few programs bother to check the return values from each call. Since some compilers (and lint) will warn about discarded return val...
2015-05-13, 1173👍, 0💬

What if a built-in logical or relational operator `returns something other than 1? ....
Isn't #defining TRUE to be 1 dangerous, since any nonzero value is considered ``true'' in C? What if a built-in logical or relational operator ``returns'' something other than 1? It is true (sic) that any nonzero value is considered true in C, but this applies only ``on input'', i.e. where a Boolean...
2016-02-29, 1172👍, 0💬

I have some old code that tries to construct identifiers with a macro like ...
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. It was an undocumented feature of some early preprocessor implementations (notably Reiser's) that comments disappeared entirely and could therefore be used for token pa...
2016-01-27, 1172👍, 0💬

What's the difference between using a typedef or a define for a user-defined type?
What's the difference between using a typedef or a define for a user-defined type? In general, typedefs are preferred, in part because they can correctly encode pointer types. For example, consider these declarations: typedef char *String_t; #define String_d char * String_t s1, s2; String_d s3, s4; ...
2016-02-22, 1171👍, 0💬

My ANSI compiler complains about a mismatch when it see
My ANSI compiler complains about a mismatch when it sees extern int func(float); int func(x) float x; { ... You have mixed the new-style prototype declaration ``extern int func(float);'' with the old-style definition ``int func(x) float x;''. It is usually possible to mix the two styles but not in t...
2016-01-13, 1171👍, 0💬

I am trying to take some square roots
I'm trying to take some square roots, and I've simplified the code down to main() { printf("%f\n", sqrt(144.)); } but I'm still getting crazy numbers. Make sure that you have #included &lt;math.h>, and correctly declared other functions returning double. (Another library function to be careful w...
2015-07-01, 1171👍, 0💬

I had a frustrating problem which turned out to be caused by the line
I had a frustrating problem which turned out to be caused by the line printf("%d", n); where n was actually a long int. I thought that ANSI function prototypes were supposed to guard against argument type mismatches like this. When a function accepts a variable number of arguments, its prototype doe...
2015-06-15, 1171👍, 0💬

I can write C code that looks more like Pascal? ....
Here are some cute preprocessor macros: #define begin { #define end } With these, I can write C code that looks more like Pascal. What do y'all think? Use of macros like these, though perhaps superficially attractive, is generally discouraged; in severe cases the practice is called ``preprocessor ab...
2016-02-24, 1170👍, 0💬

Why cant I open a file by its explicit path?
Why can't I open a file by its explicit path? The call fopen("c:\newdir\file.dat", "r") is failing. The file you actually requested--with the characters \n and \f in its name--probably doesn't exist, and isn't what you thought you were trying to open. In character constants and string literals, the ...
2015-04-08, 1170👍, 0💬

What are near and far pointers?
What are near and far pointers? These days, they're pretty much obsolete; they're definitely system-specific. They had to do with 16-bit programming under MS-DOS and perhaps some early versions of Windows. If you really need to know, see a DOS- or Windows-specific programming reference. If you're us...
2015-03-02, 1170👍, 0💬

How can I do serial comm port I O?
How can I do serial (``comm'') port I/O? It's system-dependent. Under Unix, you typically open, read, and write a device file in /dev, and use the facilities of the terminal driver to adjust its characteristics.Under MS-DOS, you can use the predefined stream stdaux, or a special file like COM1, or s...
2015-04-22, 1168👍, 0💬

How can I access an I O board directly?
How can I access an I O board directly? At one level, at least, it's quite simple: you have a device register which is actually wired up so that the bits written to it get coverted to actual voltage levels in the real world that you can do interesting things with. In general, there are two ways to g...
2015-04-20, 1167👍, 0💬

<< < 164 165 166 167 168 169 170 171 172 173 174 > >>   Sort: Date