c

Which dynamic libraries on OS X can be taken for granted?

If I want to make an OS X program as self-contained as possible to ease installation, what dynamic libraries can I expect everyone or most people to have? If I know that, I won't have to compile static libraries for everything. ...

Why didn't C have a boolean data type prior to C99?

I realise you can just #define some integers, but why didn't C have a dedicated boolean data type before C99? It's such a common occurence in programming and logic, I don't understand the absense of an explicit type and notation. ...

how 'free' works when pointer is incremented

When malloc is called, the size is stored adjacent to the allocated block so that free will know how much to free etc ( http://c-faq.com/malloc/freesize.html ). My question is, Say we have dynamically allocated memory and later in the code we increment the pointer pointer++ And then later, if i call a free(pointer) what memor...

When should -m32 option of gcc be used?

I am writing a program which if I compiler on Suse 10 32 bit system without adding the -m32 option and execute it on Suse 10 64 bit works fine. In this case is it not required for me to add -m32 option? Can we execute programs built on 32 bit systems directly on their 64 bit counterparts without any side-effects? Or any updates or cha...

sequence with and without recursion

I have a sequence. a1 = 1 - cos(x); ai = a1 + (-1)^(i-1) * x^(2*i-2) / (2*i-2)! I need to write this with and without recursion. But it has a different results. Here is my code: http://codepaste.net/q213q6 ...

Alternatives to C++ Reference/Pointer Syntax

What languages other than C and C++ have explicit reference and pointer type qualifiers? People seem to be easily confused by the right-to-left reading order of types, where char*& is "a reference to a pointer to a character", or a "character-pointer reference"; do any languages with explicit references make use of a left-to-right readin...

Writing to a file in Unicode

I am having some problems writing to a file in unicode inside my c program. I am trying to write a unicode Japanese string to a file. When I go to check the file though it is empty. If I try a non-unicode string it works just fine. What am I doing wrong? setlocale(LC_CTYPE, ""); FILE* f; f = _wfopen(COMMON_FILE_PATH,L"w"); fwprintf(f,L"...

Why can't I create an array with size determined by a global variable?

Why does the array a not get initialized by global variable size? #include<stdio.h> int size=5; int main() { int a[size]={1,2,3,4,5}; printf("%d",a[0]); return 0; } The compilation error is shown as "variable-sized object may not be initialized". According to me the array should get initialized by size. and what would be...

C: Convert A ? B : C into if (A) B else C

I was looking for a tool that can convert C code expressions for the form: a = (A) ? B : C; into the 'default' syntax with if/else statements: if (A) a = B else a = C Does someone know a tool that's capable to do such a transformation? I work with GCC 4.4.2 and create a preprocessed file with -E but do not want such structures...

Casting to a struct from LPVOID - C

Hello, I am writing a simple console application which will allow me to create a number of threads from a set of parameters passed through the arguments I provide. DWORD WINAPI ThreadFunc(LPVOID threadData) { } I am packing them into a struct and passing them as a parameter into the CreateThread method and trying to unpack them by ca...

Please explain syntax rules and scope for "typedef"

What are the rules? OTOH the simple case seems to imply the new type is the last thing on a line. Like here Uchar is the new type. typedef unsigned char Uchar; But a function pointer is completely different. Here the new type is pFunc: typedef int (*pFunc) (int); I can't think of any other examples offhand but I have come across...

Calling cdecl Functions That Have Different Number of Arguments

I have functions that I wish to call based on some input. Each function has different number of arguments. In other words, if (strcmp(str, "funcA") == 0) funcA(a, b, c); else if (strcmp(str, "funcB") == 0) funcB(d); else if (strcmp(str, "funcC") == 0) funcC(f, g); This is a bit bulky and hard to maintain. Ideally, these are variadic f...

Glib segfault g_free hash table

I'm not quite sure why if I try to free the data I get segfault. Any help will be appreciate it. struct mystu { char *q; }; static GHashTable *hashtable; static void add_inv(char *q) { gpointer old_key, old_value; if(!g_hash_table_lookup_extended(hashtable, q, &old_key, &old_value)){ g_hash_table_insert(hashtable, g...

C: How to convert a string of ints into actual ints and store them in an array?

I have a string "14 22 33 48". I need to insert each of the values in the string into the respective location in the array: int matrix[5]; so that matrix[0] = 14; matrix[1] = 22; matrix[2] = 33; matrix[3] = 48; How do I do this? ...

How to launch a c program from another c program

Hi, I have a c program which I can launch at command prompt. Is it possible for me the lunch this application in my another c program? If yes, how? All the google result shows me how to do that using pthread? Will that work? I suspect that I need a new process for my c program. Thank you. ...

CIELab Colorspace conversion

Is there a canonical colorspace conversion library? I can't find any pre-existing solutions. Is CIELab conversion is too obscure? ...

How to get the incoming ssh/telnets clients local IP address from a shell script or C

I need a way to obtain a local (not WAN) address of an incoming telnet or ssh session using a shell script or C. ...

I have a following gcc compilation warning

symbol.h:179: note: expected ‘uintptr_t *’ but argument is of type ‘PRECEDENCE’ The corresponding code is : 176 void symbol_SetCount(SYMBOL, unsigned long); 177 unsigned long symbol_GetCount(SYMBOL); 178 179 size_t symbol_Ordering(uintptr_t*, SYMBOL); 180 181 void symbol_CheckIndexInRange(int); 182...

Where do I put all these function-like #defines, in C?

I'm working with an embedded system, and I'm ending up with a ton of HW-interfacing #define functions. I want to put all of these into a separate file (for OOP-ness), but I don't know the best way to #include that. Do I just put them all into a .c file, then include that? Seems silly to put these in a .h file. ...

Behavior of a pipe after a fork()

When reading about pipes in Advanced Programming in the UNIX Environment, I noticed that after a fork that the parent can close() the read end of a pipe and it doesn't close the read end for the child. When a process forks, does its file descriptors get retained? What I mean by this is that before the fork the pipe read file descriptor...