Tag Info

Hot answers tagged

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 ...


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 ...


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.


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

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!


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 ...


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 ...


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.


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 ...


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 ...


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 ...


1

matrix is a pointer to a 5-element array of double; this means that the type of the expression *matrix is "5-element array of double"; sizeof *matrix will return the number of bytes required by such an object. So we're telling malloc to set aside enough memory for 5 5-element arrays of double, and assign the resulting pointer to matrix. Because of how ...


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 ...


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 ...


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 ...



Only top voted, non community-wiki answers of a minimum length are eligible