c

Is if(TRUE) a good idea in C?

In the C programming language, it is my understanding that variables can only be defined at the beginning of a code block, and the variable will have the scope of the block it was declared in. With that in mind, I was wondering whether it is considered bad practice to artificially create a new scope as in this example: void foo() { ...

wait statement in C-component?

Is there a wait statement in c-component? for example, wait for 0.5 second before continuing the process? thanks! ...

C: What is the best way to modify an int pointer passed via a typedef?

I have a typedef int my_type and i have a function which looks like void my_func (my_type* x); How should I use this func to modify x using the best practice? ...

What does select(2) do if you close(2) a file descriptor in a separate thread?

What is the behavior of the select(2) function when a file descriptor it is watching for reading is closed by another thread? From some cursory testing, it does return right away. I suspect the outcome is either that (a) it still continues to wait for data, but if you actually tried to read from it you'd get EBADF (possibly -- there's ...

Calculate SLOC GCC C/C++ Linux

Hi, We have a quite large (280 binaries) software project under Linux and currently it has a very dispersed code structure - that means one can't [work out] what code from the source tree is valid (builds to deployable binaries) and what is deprecated. But the Makefiles are good. We need to calculate C/C++ SLOC for entire project. Here...

Is there any ordinary reason to use open() instead of fopen()?

I'm doing a small project in C after quite a long time away from it. These happen to include some file handling. I noticed in various documentation that there are functions which return FILE * handles and others which return (small integer) descriptors. Both sets of functions offer the same basic services I need so it really does not mat...

Reading "integer" size bytes from a char* array.

I want to read sizeof(int) bytes from a char* array. a)In what scenario's we need to worry if endian needs to be checked b)how would you read the first 4 bytes considering taking endian consideration or no consideration. EDIT: The sizeof(int) bytes that I have read needs to be compared with the an integer value. What is the best app...

How to pass variable length width specifier in sscanf?

sscanf(input_str, "%5s", buf); //reads at max 5 characters from input_str to buf But I need to use something like %MACRO_SIZEs instead of %5s A trivial solution is to create a format string for the same char fmt_str[100] = ""; snprintf(fmt_str, 100, "%%%ds", MACRO_SIZE); sscanf(input_str, fmt_str, buf); Is there a better way to...

Regular expressions with matching brackets

I'm trying to extract specific hard coded variables from C source code. My remaining problem is that I'd like to parse array initialisation, for example: #define SOMEVAR { {T_X, {1, 2}}, {T_Y, {3, 4}} } It's enough to parse this example into "{T_X, {1, 2}}" and "{T_Y, {3, 4}}", since it's then possible to recurse to get the full struc...

Reference code containing every single possible construct in C

Hi all, Chapter 6 Language of the C Standard defines all the different concepts, conversions, lexical elements, expressions, declarations, statements, blocks, external definitions and so on which are defined in the C standard. I was wondering if there is a reference body of code anywhere which contains all these elements of the C langu...

What is the best way to test for file existence on a network share on Windows Vista?

My question is a slight variation on the question What is the best way to test whether a file exists on Windows?, with some specific caveats. Specifically, the data is located on a mapped drive, and the SMB 2.0 protocol is used. (By definition, this requires that the drive be mapped from a Vista machine to either a Vista or Server 2008...

Segfault when using printf

I am debugging some Linux C code in a signal handler for floating point exceptions. The goal is to check the floating point registers, print some information, and then abort. I get a segmentation fault when attempting to printf the result of (char)('0' + phyreg). struct ucontext * uc = (struct ucontext *) data; fpregset_t fp = uc ...

Passing arrays and matrices to functions as pointers and pointers to pointers in C

Given the following code: void foo( int* array ) { // ... } void bar( int** matrix ) { // ... } int main( void ) { int array[ 10 ]; int matrix[ 10 ][ 10 ]; foo( array ); bar( matrix ); return 0; } I don't understand why I get this warning: warning: passing argument 1 of ‘bar’ from incompatible poi...

Use #ifdefs and #define to optionally turn a function call into a comment

Is it possible to do something like this #ifdef SOMETHING #define foo // #else #define foo MyFunction #endif The idea is that if SOMETHING is defined, then calls to foo(...) become comments (or something that doesn't get evaluated or compiled), otherwise it becomes a call to MyFunction. I've seen __noop used, but I don't believe I ca...

Why does gcc have "â" in all its error messages?

For some reason, my installation of gcc seems to be printing an "a with a carat" character in place of all %s's in its error messages, e.g., test.c:4: error: expected â, â, â, â or â before â token Has anyone else seen this before? (Needless to say, it's difficult to Google for.) (This is on Ubuntu 8.10) Edit: The guy at http://ubun...

Alternate method for typecasting in C?

I came across this line in some code and can't find the syntax defined anywhere: *(float *)csCoord.nX = lImportHeight* .04f; /* magic number to scale font size */ If I remove the f from .04f then the compiler gives a warning about possible data loss due to a conversion from 'double' to 'float'. I assume the f is doing some sort...

kill a process started with popen

After opening a pipe to a process with popen, is there a way to kill the thread that is started? Using pclose is not what I want because that will wait for the thread to finish, but I need to kill it. ...

Mis-aligned pointers on x86

Can someone provide an example were casting a pointer from one type to another fails due to mis-alignment? In the comments to this answer, bothie states that doing something like char * foo = ...; int bar = *(int *)foo; might lead to errors even on x86 if alignment-checking is enabled. I tried to produce an error condition after set...

Ignoring line breaks in C

I'm trying to write some code that reads a file and ignores the line breaks (\n), so far I have: c = fgetc(fp); for(int loop = 0; c != EOF; loop++) { if((c != '\n') && (c != '\\')) { buffer[loop] = c; } c = fgetc(fp); } but its just not seeming to ignore the '\n' bits (not sure about the '\') And sorry for the la...

What is the type of an enum whose values appear to be strings?

I am working with Apple's ScriptingBridge framework, and have generated a header file for iTunes that contains several enums like this: typedef enum { iTunesESrcLibrary = 'kLib', iTunesESrcIPod = 'kPod', iTunesESrcAudioCD = 'kACD', iTunesESrcMP3CD = 'kMCD', iTunesESrcDevice = 'kDev', iTunesESrcRadioTuner = 'kTun'...