c

runtime determine type for C

If the type of a variable must be determined as runtime in C but the variable name is fixed and given, is there any way to reuse the code that involves the variable? Actually I am asking about the C counterpart situation of runtime determine type for C++. If possible please give some examples. ...

How to calculate the length of a string in C efficiently?

How to calculate the length of a string in C efficiently (in time)? Right now I'm doing: int calculate_length(char *string) { int length = 0; while (string[length] != '\0') { length++; } return length; } But it's very slow compared to strlen() for example, is there any other way to do it? Thanks. EDIT: I'm w...

Interview question: C program to sort a binary array in O(n)

I've comeup with the following program to do it, but it does not seem to work and goes into infinite loop. Its working is similar to quicksort. int main() { int arr[] = {1,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1}; int N = 18; int *front, *last; front = arr; last = arr + N; while(front <= last) { while( (front < last) && (*front == 0...

Custom X86_64 calling convention to C function call

Hi stackoverflow I've to do an interface (say, a wrapper) that allow a call from X86_64 assembly code using his calling convention to C function, with other calling convention. The best thing would be to be pretty "compiler independant" (juste modifying the wrapper), so i'm looking for something that puts registers/stack stuff on compi...

main function does not return anything. Why?

Hi, With respect to C/C++ main() must always return an integer(zero to indicate success and non-zero to indicate failure). I can understand this as the program is run it becomes a process and every process should have an exit status, which we obtain by doing echo $? from shell after the process gets over. Now I don't understand why is...

C Binary/Source Code in an iPhone XCode Project

What's the best practice to include existing C Libraries into an iPhone Project? I searched the Internet and actually found some pieces but nothing that explains the exact way to do it. I'm trying to include the following Citrix XenServer SDK Library. Any help is appreciated. Thanks! ...

Which is better c89 or c99

Hello, gcc 4.4.1 I am just wondering which standard is better and more portable? I was doing some research on this. And found that c89 is still widely used more than c99. Not many C developers are switching to the newer c99 standard. And also, is c89 more portable than c99? As c99 has been around for me than 10 years. I would have ...

C - what is the return value of a semicolon?

im just curious about the following example #include<stdio.h> int test(); int test(){ // int a = 5; // int b = a+1; return ; } int main(){ printf("%u\n",test()); return 0; } i compiled it with 'gcc -Wall -o semicolon semicolon.c' to create an executable and 'gcc -Wall -S semicolon.c' to get the assembler...

PHP extension: convert an object to string with __toString()

Writing a PHP extension in C, I want to convert a userland object (IS_OBJECT) to a string through __toString() if it has one, and fail otherwise. What should I use? I don't need another zval on output, just a char *. zval *zo; switch (Z_TYPE_P(zo)) { case IS_STRING: ... Z_STRVAL_P(zo) ... break; case IS_OBJECT: ... ???(zo)...

C - How to create a pattern in code segment to recognize it in memory dump?

I dump my RAM (a piece of it - code segment only) in order to find where is which C function being placed. I have no map file and I don't know what boot/init routines exactly do. I load my program into RAM, then if I dump the RAM, it is very hard to find exactly where is what function. I'd like to use different patterns build in the C ...

Questions about C bitfields

Is bitfield a C concept or C++? Can it be used only within a structure? What are the other places we can use them? AFAIK, bitfields are special structure variables that occupy the memory only for specified no. of bits. It is useful in saving memory and nothing else. Am I correct? I coded a small program to understand the usage of bit...

Relink a shared library to a different version of libc

I have a linux shared library (.so) compiled with a specific version of libc (GLIBC2.4) and I need to use it on a system with different version of libc. I do not have sources for the library in question so I cannot recompile for the new system. Is it somehow possible to change the dependencies in that library to a different libc? ...

How to single-quote an argument in a macro?

I would like to create a C pre-processor macro that will single-quote the argument. Just like the common used #X. I want Q(A) to be expanded to 'A'. I am using gcc on Linux. Does any one have an idea? I know # double-quotes. I am looking for a similar mechanism fir single-quoting. ...

Logging function calls to a file and using this log file as program code

I have an application which is written in C and logs my function calls by writing them to a file. Now I need to re-use these logged function calls in another C-application (which is also the reason for logging). This application should do - next to other things - exactly the things which were logged. example: MYLOGFILE which was writt...

What does this C warning mean? "int format, pointer arg"

#include <stdio.h> int main() { // Declarations int iCount1, iCount2; int iXXTest[4][3] = {{2, 3, 5}, {9, 8, 6}, {1, 8, 4}, {5, 9, 7}}; // Walk through 1st dimension for (iCount1 = 0; iCount1 < 4; iCount1++) { // Walk through 2nd dimension for (iCount2 = 0; iCount2 < 3; iCount2++) { print...

Using popen() to invoke a shell command?

When running the following code through xcode I get inconsistent behavior. Sometimes it prints the git version correctly, other times it doesn't print anything. The return code from the shell command is always 0 though. Any ideas on why this might be? What am I doing wrong? #define BUFFER_SIZE 256 int main (int argc, const char * arg...

Javascript-friendly preprocessor dilemma.

I've been working on a (nearly finished) Javascript project for a little over 14 months now. The project started out as a hack I expected to finish overnight, but over time the Javascript part has grown up to be 68 separate files and 10,314 non-empty lines, sadly currently dependent on the C preprocessor for building. It's hard to expla...

what does compiler do with a[i] which a is array? And what if a is a pointer?

I was told by c-faq that compiler do different things to deal with a[i] while a is an array or a pointer. Here's an example from c-faq: char a[] = "hello"; char *p = "world"; Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the ch...

C: putting arrays in arrays properly

myRainfallDB is a three-dimensional array containing information about the rainfall in random places on earth. I want to set up the following array structure: myRainfallDB[] contains a list of place records. These coordinates must be stored in doubles. myRainfallDB[][] contains At index 0: a double containing the X coordinate of the ...

Why does OpenProcessToken fail with ERROR_ACCESS_DENIED

I'm running a process as a user in the Administrators group, trying to get a process token for another process. The other process is run by a user not in the Administrators group. Here's the gist of the code I'm using. pid in this code represents the process id of the non-admin process. All of this is on Windows XP SP 2 and all on th...