Tag Info

New answers tagged

0

There is probably a valid use case for the thing that *****p denotes, but almost certainly it is a better idea to name one of the intermediate levels so that you end up with **symbolTable instead. Scan the open-source code repositories; I'm almost certain you'll find instances of that many stars. You will probably be able to improve the code by introducing ...


0

The first form for ( x = 0; !done; x++ ) is a little weird and non-intuitive IMO. There's no obvious correlation between x and done in this context (you have to review the body of the loop to see that done is set when x is no longer less than max) In that case, I would much rather see something like for ( x = 0; x < max; x++ ) However, you're ...


1

Consider the following: int done = 0; for (i = 0; !done && i < max; ++i) { // ... something involving 'i', that might end early ... } My personal preference here is to keep the "break condition" in one point. If you add a second if clause that breaks, you risk to skip over that logic when you or another programmer reads over it later. As ...


4

Different blocks have different meanings: by using the ones which are commonly used for a specific purpose, you make your code easier to read and maintain. By using the ones which are not commonly used for the purpose, you force readers of your code to stop and think why you've used this construct instead of the commonly used one. While semantically ...


0

Here's some code that clearly shows memory layout for arrays. Using a union, for which memory is overlaid for each union member, we can see that values are stored in row-major order. #include <stdio.h> #define N 4*4*4 #define N_2D 8 #define N_3D 4 union multi_d_arrays { int one_d[N]; int two_d[N_2D][N_2D]; int ...


0

In addition to Jules' answer, I would like to add a bit of historical perspective. The counter-intuitiveness of this in C is due to the simple fact: In general, int a[] (length unspecified) is some sort of syntactic sugar in place of int* a Likewise, int a[][] (again, lengths unspecified) is some sort of syntactic sugar in place of int** a. Because the ...


4

You're nearly right, yes. In fact, int a[5][10]; declares 5 arrays each of which contain 10 integers, rater than the other way round. Other than that you're completely right. See http://en.m.wikipedia.org/wiki/Row-major_order for a more detailed description and an explanation of why the order is important.


6

Several options: the naive method would be an if else cascade (slow) the compiler can sort the cases behind the scene and then do a binary search (good for disjoint cases) a jump table; only good for sequential cases but very fast. For string-based switches there is the option of the prefix Trie or again the sorted table that can be binary searched.


5

Generally, switch statements are implemented as Jump Tables. There is no searching involved.


3

A field you add to a structure for the purpose of error checking is often called a dog tag (I think it's so called after Code Complete - Steve McConnel). A dog tag is used to check for corrupted memory: when you allocate a variable put a value that should remain unchanged into its tag file; when you use the structure check the tag's field value (if the ...


3

atoi() does not modify its parameter, and you ignore its return value. There's a small chance of it crashing in a situation like this (you've passed it uninitialized data, so it may carry on reading junk until it reaches unallocated memory and crashes with a segmentation violation, but more likely there will be a non-digit character that stops it fairly ...


0

C doesn't have type inference (thats what it is called when a compiler guesses the type of a variable for you) because it is old. It was developed in the early 1970's Many newer languages have systems that allow you to use variables without specifying their type (ruby, javascript, python, etc.)


29

You are comparing variable declarations to #defines, which is incorrect. With a #define, you create a mapping between an identifier and a snippet of source code. The C preprocessor will then literally substitute any occurrences of that identifier with the provided snippet. Writing #define FOO 40 + 2 int foos = FOO + FOO * FOO; ends up being the same thing ...


3

X in the second example is never a float. It is called a macro, it replaces the defined macro value 'X' in the source with the value. A readable article on #define is here. In the case of the supplied code, before compilation the preprocessor changes the code Z=Y+X; to Z=Y+5.2; and that is what gets compiled. That means you can also replace those ...


5

There are two issues at play here: Issue #1: C is a statically typed language; all type information is determined at compile time. No type information is stored with any object in memory such that its type and size can be determined at run time1. If you examine the memory at any particular address while the program is running, all you'll see is a sludge ...


4

printf() is what's known as a variadic function, which is one that accepts a variable number of arguments. Variadic functions in C use a special prototype to tell the compiler that the list of arguments is of unknown length: int printf(const char *format, ...); Standard C provides a set of functions in stdarg.h that can be used to retrieve the arguments ...


1

Think about it like you are passing variables to another function that you have defined. You normally tell the other function which type of data it should expect/receive. The same way with printf(). It is already defined in the stdio.h library and requires you to tell it which data it is receiving so that it can output it in the right format (as in your case ...


7

Because at the moment when printf is called and does its job, the compiler is no longer there to tell it what to do. The function doesn't get any information except what's in its parameters, and the vararg parameters don't have any type, so printf would have no clue how to print any if them if it didn't get explicit instructions via the format string. The ...


0

A void pointer says nothing about what it is pointing to - it's a "pointer to anything". So info -> simTime is not meaningful to the compiler. You need to cast the pointer back to the correct type before dereferencing it. If you know that it will always be a shiftInfo, then try something like ((shiftInfo *) info) -> simTime. Your second warning is ...


1

struct shiftInfo *Q = malloc(sizeof(struct shiftInfo) + (maxCust*sizeof(int))); This allocates a struct shiftInfo object with space reserved for maxCust integers. Looks ok ... struct shiftInfo info; info.simTime = atoi(argv[1]); info.arrivalRate = atof(argv[2]); info.tellers = atoi(argv[3]); info.serviceTime = atoi(argv[4]); info.sampleInterval = ...


1

The answer has been given to you on a plate, unfortunately its not going to help you learn and you are not a better programmer unless you think about how we got the answer so easily. (Or do you just want to pass an exam?) "So I'm stuck on finding some way to store all the variables and this has stumped me for hours." Did you think to ask "Is there ...


2

An important way of learning programming (and many other things) is by reading the work of others, especially masters. Equally important to programming is to do what you have been asked to do and nothing else. So, if the questions asks prompt the user to enter integers until the user enters 0. then that's all you should do. The first cut of the logic ...


0

You don't need to store all the variables, only their running total and how many of them there are to calculate the mean. FTR, to store all the variables, you would need a dynamically allocated data structure, which requires malloc/free and nobody should have that shit inflicted on them.


4

This is possible but has limitations. An implementation would utilize setjmp() / longjmp(). You can see an example implementation here: http://aceunit.sourceforge.net/doxygen/ExceptionHandling_8h.html Beware: return within try-blocks would be a bug!


6

"C program source text is free-format, using the semicolon as a statement terminator and curly braces for grouping blocks of statements." (Wikipedia) Blocks don't look like control flow, but they are; without the curly braces only the next line would be controlled by a loop keyword, but with the braces the entire block is looped over.



Top 50 recent answers are included