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

Why do all the lines end up containing copies of the last line?
I'm using fgets to read lines from a file into an array of pointers. Why do all the lines end up containing copies of the last line? You have only allocated memory for one line, linebuf. Each time you call fgets, the previous line is overwritten. fgets doesn't do any memory allocation: unless it rea...
2015-11-13, 1115👍, 0💬

Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?
Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer? sizeof tells you the size of the pointer. There is no portable way to find out the size of a malloc'ed block. (Remember, too, that sizeof operates at compile time
2016-03-21, 1114👍, 0💬

Did C have any Year 2000 problems?
Did C have any Year 2000 problems? No, although poorly-written C programs might have. The tm_year field of struct tm holds the value of the year minus 1900; this field therefore contains the value 100 for the year 2000. Code that uses tm_year correctly (by adding or subtracting 1900 when converting ...
2015-08-03, 1114👍, 0💬

Does C have anything like the `substr extract substrin routine present in other languages?
Does C have anything like the `substr extract substrin routine present in other languages? Not as such. To extract a substring of length LEN starting at index POS in a source string, use something like char dest[LEN+1]; strncpy(dest, &source[POS], LEN); dest[LEN] = '\0'; /* ensure \0 termination...
2016-03-07, 1112👍, 0💬

What are the complete rules for header file searching?
What are the complete rules for header file searching? The exact behavior is implementation-defined (which means that it is supposed to be documented; Typically, headers named with &lt;> syntax are searched for in one or more standard places. Header files named with "" syntax are first searched ...
2016-02-12, 1112👍, 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, 1112👍, 0💬

Why doesnt this code work?
Why doesn't this code: double d; scanf("%f", &amp;d); work? Unlike printf, scanf uses %lf for values of type double, and %f for float.%f tells scanf to expect a pointer-to-float, not the pointer-to-double you gave it. Either use %lf, or declare the receiving variable as a float.
2015-10-30, 1111👍, 0💬

How can I find out the size of a file, prior to reading it in?
How can I find out the size of a file, prior to reading it in? If the ``size of a file'' is the number of characters you'll be able to read from it in C (or which were written to it by a previous program), it can be difficult or impossible to determine this number exactly (other than by reading the ...
2015-04-15, 1111👍, 0💬

I am getting baffling syntax errors which make no sense at all
I'm getting baffling syntax errors which make no sense at all, and it seems like large chunks of my program aren't being compiled. Check for unclosed comments, mismatched #if/#ifdef/#ifndef/#else/#endi fdirectives, and perhaps unclosed quotes; remember to check header files, too.
2015-06-01, 1109👍, 0💬

Since array references decay into pointers ...
Since array references decay into pointers, if arr is an array, what's the difference between arr and &arr? The type. In Standard C, &arr yields a pointer, of type pointer-to-array-of-T, to the entire array. (In pre-ANSI C, the & in &arr generally elicited a warning, and was generall...
2015-12-09, 1108👍, 0💬

Dont ANSI function prototypes render lint obsolete?
Dont ANSI function prototypes render lint obsolete? Not really. First of all, prototypes work only if they are present and correct; an inadvertently incorrect prototype is worse than useless. Secondly, lint checks consistency across multiple source files, and checks data declarations as well as func...
2015-05-01, 1108👍, 0💬

People always say that good style is important
People always say that good style is important, but when they go out of their way to use clear techniques and make their programs readable, they seem to end up with less efficient programs. Since efficiency is so important, isn't it necessary to sacrifice some style and readability? It's true that g...
2015-05-06, 1106👍, 0💬

I think somethings wrong with my compiler ...
I think something's wrong with my compiler: I just noticed that sizeof('a') is 2, not 1 (i.e. not sizeof(char)). Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though this is another area where C++ differs).
2016-03-04, 1105👍, 0💬

Can you mix old-style and new-style function syntax?
Can you mix old-style and new-style function syntax? Doing so is legal (and can be useful for backwards compatibility), but requires a certain amount of care . Modern practice, however, is to use the prototyped form in both declarations and definitions. (The old-style syntax is marked as obsolescent...
2016-01-13, 1105👍, 0💬

I dont understand why I cant use const values in initializers ....
I don't understand why I can't use const values in initializers and array dimensions, as in const int n = 5; int a[n]; The const qualifier really means ``read-only''; an object so qualified is a run-time object which cannot (normally) be assigned to. The value of a const-qualified object is therefor...
2016-01-08, 1104👍, 0💬

I am checking a string to see if it matches a particular value...
I'm checking a string to see if it matches a particular value. Why isn't this code working? char *string; ... if(string == "value") { /* string matches "value" */ ... } Strings in C are represented as arrays of characters, and C never manipulates (assigns, compares, etc.) arrays as a whole. The == o...
2016-03-11, 1102👍, 0💬

Why cant I perform arithmetic on a void pointer?
Why cant I perform arithmetic on a void pointer? The compiler doesn't know the size of the pointed-to objects. (Remember that pointer arithmetic is always in terms of the pointed-to size; Therefore, arithmetic on void *'s is disallowed (though some compilers allow it as an extension). Before perform...
2015-12-07, 1102👍, 0💬

How can I shut off the warning ...
How can I shut off the ``warning: possible pointer alignment problem'' message which lint gives me for each call to malloc? A modern lint shouldn't be complaining about this. Once upon a time, lint did not and could not know that malloc ``returns a pointer to space suitably aligned for storage of an...
2015-05-05, 1102👍, 0💬

Why isnt it being handled properly?
I'm reading strings typed by the user into an array, and then printing them out later. When the user types a sequence like \n, why isn't it being handled properly? Character sequences like \n are interpreted at compile time. When a backslash and an adjacent n appear in a character constant or string...
2015-08-24, 1101👍, 0💬

Is if(p), where p is a pointer, a valid and portable test?
Is if(p), where p is a pointer, a valid and portable test? It is always valid. When C requires the Boolean value of an expression, a false value is inferred when the expression compares equal to zero, and a true value otherwise. That is, whenever one writes if(expr) where ``expr'' is any expression ...
2016-02-29, 1098👍, 0💬

Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0?
Should I use symbolic names like TRUE and FALSE for Boolean constants, or plain 1 and 0? It's your choice. Preprocessor macros like TRUE and FALSE (and, of course, NULL) are used for code readability, not because the underlying values might ever change. It's a matter of style, not correctness, wheth...
2015-05-11, 1098👍, 0💬

Why does the call char scanf work?
Why does the call char s[30]; scanf("%s", s); work? I thought you always needed an & on each variable passed to scanf. You always need a pointer; you don't necessarily need an explicit &amp;. When you pass an array to scanf, you do not need the &, because arrays are always passed to func...
2015-10-30, 1097👍, 0💬

How can I sort a linked list?
How can I sort a linked list? Sometimes it's easier to keep the list in order as you build it (or perhaps to use a tree instead). Algorithms like insertion sort and merge sort lend themselves ideally to use with linked lists. If you want to use a standard library function, you can allocate a tempora...
2015-08-10, 1097👍, 0💬

I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it?
I am porting this program, and it calls a routine drand48, which my library doesnt have. What is it? drand48 is a Unix System V routine which returns floating point random numbers (presumably with 48 bits of precision) in the half-open interval [0, 1). (Its companion seed routine is srand48; neither...
2015-07-20, 1094👍, 0💬

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