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

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, 1174👍, 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, 1173👍, 0💬

Once I have used freopen, how can I get the original stdout (or stdin) back?
Once I have used freopen, how can I get the original stdout (or stdin) back? There isn't a good way. If you need to switch back, the best solution is not to have used freopen in the first place. Try using your own explicit output (or input) stream variable, which you can reassign at will, while leav...
2015-10-01, 1173👍, 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, 1173👍, 0💬

When I read numbers from the keyboard with scanf ...
When I read numbers from the keyboard with scanf and a "%d\n" format, like this: int n; scanf("%d\n", &amp;n); printf("you typed %d\n", n); it seems to hang until I type one extra line of input. Perhaps surprisingly, \n in a scanf format string does not mean to expect a newline, but rather to re...
2015-10-23, 1172👍, 0💬

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, 1171👍, 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, 1168👍, 0💬

I am starting to think about multinational character sets ....
I'm starting to think about multinational character sets, and I'm worried about the implications of making sizeof(char) be 2 so that 16-bit character sets can be represented. If type char were made 16 bits, sizeof(char) would still be 1, and CHAR_BIT in &lt;limits.h> would be 16, and it would si...
2016-03-02, 1167👍, 0💬

If I can say... why can't I say...
If I can say char a[] = "Hello, world!"; why can't I say char a[14]; Strings are arrays, and you can't assign arrays directly. Use strcpy instead: strcpy(a, "Hello, world!");
2016-03-11, 1165👍, 0💬

Some people say that gotos are evil and that I should never use them. Isnt that a bit extreme
Some people say that gotos are evil and that I should never use them. Isnt that a bit extreme rogramming style, like writing style, is somewhat of an art and cannot be codified by inflexible rules, although discussions about style often seem to center exclusively around such rules. In the case of th...
2015-05-08, 1165👍, 0💬

I am trying to define a few simple little function-like macros ...
I'm trying to define a few simple little function-like macros such as #define square(x) x * x but they're not always working. There are three important rules to remember when defining function-like macros: 1. The macro expansion must always be parenthesized to protect any lower-precedence operators ...
2016-02-26, 1164👍, 0💬

How can I insert or delete a line (or record) in the middle of a file?
How can I insert or delete a line (or record) in the middle of a file? In general, there is no way to do this. The usual solution is simply to rewrite the file. When you find yourself needing to insert data into an existing file, here are a few alternatives you can try: * Rearrange the data file so ...
2015-10-05, 1160👍, 0💬

I just typed in this program, and it is acting strangely. Can you see anything wrong with it?
I just typed in this program, and it is acting strangely. Can you see anything wrong with it? See if you can run lint first (perhaps with the -a, -c, -h, -p or other options ). Many C compilers are really only half-compilers, taking the attitude that it's not their problem if you didn't say what you...
2015-05-06, 1155👍, 0💬

Why does everyone say not to use gets?
Why does everyone say not to use gets? Unlike fgets(), gets() cannot be told the size of the buffer it's to read into, so it cannot be prevented from overflowing that buffer if an input line is longer than expected--and Murphy's Law says that, sooner or later, a larger-than-expected input line will ...
2015-10-16, 1153👍, 0💬

How can I find out if there are characters available for reading?
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? These, too, are entirely operating-system-specific. Some versions of curses have a nodelay function. Depending on your ...
2015-04-29, 1153👍, 0💬

My compiler isnt letting me declare a function
My compiler isnt letting me declare a function A; My compiler isn't letting me declare a function int f(...) { } i.e. accepting a variable number of arguments, but with no fixed arguments at all. A: Standard C requires at least one fixed argument, in part so that you can hand it to va_start. (In any...
2015-06-08, 1150👍, 0💬

How can I insert or delete a line (or record) in the middle of a file?
How can I insert or delete a line (or record) in the middle of a file? In general, there is no way to do this. The usual solution is simply to rewrite the file. When you find yourself needing to insert data into an existing file, here are a few alternatives you can try: * Rearrange the data file so ...
2015-04-13, 1145👍, 0💬

What's the difference between...?
What's the difference between #include &lt;> and #include "" ? The syntax is typically used with Standard or system-supplied headers, while "" is typically used for a program's own header files.
2016-02-16, 1144👍, 0💬

I am using scanf c to read a Y/N response
I'm using scanf %c to read a Y/N response, but later input gets skipped. You wanted scanf %c to read a single character, and it tried to, but when you tried to type that single character at it, before the rest of the input system would accept it, you had to hit the RETURN key, too. scanf read only t...
2015-10-21, 1143👍, 0💬

How can I write a function that takes a variable number of arguments?
How can I write a function that takes a variable number of arguments? Use the facilities of the &lt;stdarg.h> header. Here is a function which concatenates an arbitrary number of strings into malloc'ed memory: #include &lt;stdlib.h> /* for malloc, NULL, size_t */ #include &lt;stdarg.h> /...
2015-06-15, 1143👍, 0💬

My floating-point calculations are acting strangely and giving me different answers on different machines.
My floating-point calculations are acting strangely and giving me different answers on different machines. If the problem isn't that simple, recall that digital computers usually use floating-point formats which provide a close but by no means exact simulation of real number arithmetic. Among other ...
2015-06-29, 1141👍, 0💬

I am reading a number with scanf and d, and then a string with gets
I'm reading a number with scanf and %d, and then a string with gets(): int n; char str[80]; printf("enter a number: "); scanf("%d", &amp;n); printf("enter a string: "); gets(str); printf("you typed %d and "%s"\n", n, str); but the compiler seems to be skipping the call to gets()! If, in response...
2015-10-23, 1140👍, 0💬

How can I open files with names like ...
How can I open files with names like ``file1'', ``file2'', ``file3'', etc., where the numeric part is controlled by a variable? Basically I want ``file%d'', like printf.. You want printf's close cousin sprintf, which ``prints'' to a string: char filename[FILENAME_MAX]; sprintf(filename, "file%d", i)...
2015-10-07, 1140👍, 0💬

How can I find out how much free space is available on disk?
How can I find out how much free space is available on disk? There is no portable way. Under some versions of Unix you can call statfs. Under MS-DOS, use interrupt 0x21 subfunction 0x36, or perhaps a routine such as diskfree. Another possibility is to use popen to invoke and read the output of a ``d...
2015-04-03, 1140👍, 0💬

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