< 1 2 3 4 5 6 7 > >>   Sort: Rank

So can I query the malloc package to find out how big an allocated block is?
So can I query the malloc package to find out how big an allocated block is? Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself.
2016-03-24, 1237👍, 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, 1112👍, 0💬

Having dynamically allocated an array , can I change its size?
Having dynamically allocated an array , can I change its size? Yes. This is exactly what realloc is for. Given a region of malloced memory its size can be changed using code like: dynarray = realloc(dynarray, 20 * sizeof(int)); Note that realloc may not always be able to enlarge memory regions in-pl...
2016-03-21, 1177👍, 0💬

Is it legal to pass a null pointer as the first argument to realloc? Why would you want to?
Is it legal to pass a null pointer as the first argument to realloc? Why would you want to? ANSI C sanctions this usage (and the related realloc(..., 0), which frees), although several earlier implementations do not support it, so it may not be fully portable. Passing an initially-null pointer to re...
2016-03-16, 1191👍, 0💬

What is the difference between calloc and malloc? ....
What's the difference between calloc and malloc? Which should I use? Is it safe to take advantage of calloc's zero-filling? Does free work on memory allocated with calloc, or do you need a cfree? calloc(m, n) is essentially equivalent to p = malloc(m * n); memset(p, 0, m * n); There is no important ...
2016-03-16, 1219👍, 0💬

What is alloca and why is its use discouraged?
What is alloca and why is its use discouraged? alloca allocates memory which is automatically freed when the function which called alloca returns. That is, memory allocated with alloca is local to a particular function's ``stack frame'' or context. alloca cannot be written portably, and is difficult...
2016-03-14, 1064👍, 0💬

Why doesnt strcat work? ....
Why doesn't strcat(string, '!'); work? There is a very real difference between characters and strings, and strcat concatenates strings. A character constant like '!' represents a single character. A string literal between double quotes usually represents multiple characters. A string literal like "!...
2016-03-14, 1178👍, 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, 1100👍, 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, 1086👍, 0💬

I cant get strcat to work. I tried ...
I can't get strcat to work. I tried char *s1 = "Hello, ";br> char *s2 = "world!";br> char *s3 = strcat(s1, s2);br> but I got strange results. the main problem here is that space for the concatenated result is not properly allocated. C does not provide an automatically-managed string type. C compiler...
2016-03-09, 1072👍, 0💬

What is the difference between these initializations? ....
What is the difference between these initializations? char a[] = "string literal"; char *p = "string literal"; My program crashes if I try to assign a new value to p[i]. A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways: 1. As the in...
2016-03-09, 1226👍, 0💬

How can I get the numeric value corresponding to a character? ....
How can I get the numeric value (i.e. ASCII or other character set code) corresponding to a character, or vice versa? In C, characters are represented by small integers corresponding to their values in the machine's character set. Therefore, you don't need a conversion function: if you have the char...
2016-03-07, 1237👍, 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, 1109👍, 0💬

I am reading strings typed by the user into an array ...
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...
2016-03-04, 1166👍, 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, 1103👍, 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, 1081👍, 0💬

What is the right type to use for Boolean values in C? ....
What is the right type to use for Boolean values in C? Is there a standard type? Should I use #defines or enums for the true and false values? Traditionally, C did not provide a standard Boolean type, partly and partly to allow the programmer to make the appropriate space/time tradeoff. (Using an in...
2016-03-02, 1269👍, 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, 1138👍, 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, 1095👍, 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...
2016-02-29, 1168👍, 0💬

A third-party header file I just started using is defining its own TRUE and FALSE values incompatibly ...
A third-party header file I just started using is defining its own TRUE and FALSE values incompatibly with the code I've already developed. What can I do? This is indeed an annoying situation. It's a classic namespace problem;Ideally, third-party vendors would be conscientious when defining symbols ...
2016-02-26, 1058👍, 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, 1091👍, 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, 1123👍, 0💬

How can I write a generic macro to swap two values?
How can I write a generic macro to swap two values? There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable. If the macro ...
2016-02-24, 1203👍, 0💬

< 1 2 3 4 5 6 7 > >>   Sort: Rank