c

Converting a char string to its underlying data type.

I have a string(char*), and i need to find its underlying datatype such as int, float, double, short, long, or just a character array containing alphabets with or with out digits(like varchar in SQL). For ex: char* str1 = "12312" char* str2 = "231.342" char* str3 = "234234243234" char* str4 = "4323434.2432342" char*...

How to initialise a rather complex char array in C?

Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that looks like this: { { "(SAME", "AS", "U+4E18)", "HILLOCK", "OR", "MOUND"}, { "TO", "LICK;", {1, 1, 0}, "TASTE,", "A", "MAT,", "BAMBOO", "BARK"}, { "(J)", "NON-STANDARD", "FORM", "OF", "U+559C", ",", {1, 1, 0}, "LIKE,", "LOVE,", "ENJOY;", {1, 1, 4}, "JOYFUL", ...

switch case vs if else

I was wondering if there was any difference in the way the following code was compiled into assembly. I've heard that switch-case is more efficient than if else, but in this example I am not quite sure if that would be the case. if(x==1){ ... }else if(x==2){ ... }else{ ... } and switch(x){ case 1: ... break; case 2: ... break;...

Spinlock Versus Semaphore!

What is the basic differences between Semaphores & Spinlock? & In what best situations or conditions, we can use these. ...

How to make a char string from a C macro's value?

For example, how to avoid writing the 'func_name' twice? #ifndef TEST_FUN # define TEST_FUN func_name # define TEST_FUN_NAME "func_name" #endif I'd like to follow the Single Point of Truth rule. Version of C preprocessor: $ cpp --version cpp (GCC) 4.1.2 20070626 (Red Hat 4.1.2-14) ...

OSX lacks memalign

I'm working on a project in C and it requires memalign(). Really, posix_memalign() would do as well, but darwin/OSX lacks both of them. What is a good solution to shoehorn-in memalign? I don't understand the licensing for posix-C code if I were to rip off memalign.c and put it in my project- I don't want any viral-type licensing LGPL-i...

Cross-platform editor control

I need a cross-platform editor control to use as GUI-part in an in-house tool. The control may be commercial, but with reasonable price. Required features: Platforms: Win32, OS X, Linux UTF-8 support Fine-grained run-time control to the text style (or at least color) Nice low-level plain C API without usual horrible bloat Should not p...

How do I create a symlink in Windows Vista?

I am looking to create symlinks (soft links) from Java on a Windows Vista/ 2008 machine. I'm happy with the idea that I need to call out to the JNI to do this. I am after help on the actual C code though. What is the appropriate system call to create the link? Pointers to some good documentation on this subject would be very much appreci...

K & R Question: Need help understanding "getbits()" method in Chapter 2

As I've mentioned before, I'm going through K&R, and overall am doing all right with it. However, in chapter 2, the section on bitwise operators (section 2.9), I'm having trouble understanding how one of the sample methods works -- and as a result, I'm having trouble with the associated exercises. (This isn't a dupe of my prior questio...

Embedded C: what does var = 0xFF; do?

I'm working with embedded C for the first time. Although my C is rusty, I can read the code but I don't really have a grasp on why certain lines are the way the are. For example, I want to know if a variable is true or false and send it back to another application. Rather than setting the variable to 1 or 0, the original implementor chos...

Printing pointers in C

I was trying to understand something with pointers, so I wrote this code: #include <stdio.h> int main(void) { char s[] = "asd"; char **p = &s; printf("The value of s is: %p\n", s); printf("The direction of s is: %p\n", &s); printf("The value of p is: %p\n", p); printf("The direction of p is: %p\n", &p); p...

How do you reverse a string in place in C or C++?

How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string? ...

Is it possible to initialize a const struct without using a function?

I have a fairly simple const struct in some C code that simply holds a few pointers and would like to initialize it statically if possible. Can I and, if so, how? ...

Recommendations for a small c-based vector and matrix library.

I'm in need of a lightweight library for 2d & 3d vectors and 3x3 & 4x4 matrices. In basic C. Just so I don't reinvent the wheel suboptimally. Any suggestions? ...

Best way to detect integer overflow in C/C++

I was writing a program in C++ to find all solutions of a^b = c (a to the power of b), where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and ran a digit-counting routine each time on a, b and a^b to check if the digits condition was satisfied. However, spurious solutions can be gen...

Print spinning cursor in a terminal running application using C

How would I print a spinning curser in a utility that runs in a terminal using standard C? I'm looking for something that prints: \ | / - over and over in the same position on the screen? Thanks ...

Using C++ library in C code

I have a C++ library that provides various classes for managing data. I have the source code for the library. I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time. I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an is...

How should C bitflag enumerations be translated into C++?

C++ is mostly a superset of C, but not always. In particular, while enumeration values in both C and C++ implicitly convert into int, the reverse isn't true: only in C do ints convert back into enumeration values. Thus, bitflags defined via enumeration declarations don't work correctly. Hence, this is OK in C, but not in C++: typedef en...

Converting C source to C++

How would you go about converting a reasonably large (>300K), fairly mature C codebase to C++? The kind of C I have in mind is split into files roughly corresponding to modules (i.e. less granular than a typical OO class-based decomposition), using internal linkage in lieu private functions and data, and external linkage for public func...

How to detect possible / potential stack overflow problems in a c / c++ program?

Is there a standard way to see how much stack space your app has and what the highest watermark for stack usage is during a run? Also in the dreaded case of actual overflow what happens? Does it crash, trigger an exception or signal? Is there a standard or is it different on all systems and compilers? I'm looking specifically for Win...