c

C : Making sure the element you return from an array is correct.

Say we have this piece of C code: int x[] = {1, 2, 3, 4, 5}; printf("%d", *(x + 1)); //prints 2 printf("%d", *(x + 500)); //prints 7209065 (...?) As you can see from the second call, it still returns something...but it's garbage. So I ask, how do you handle such a case in C? ie, how do you know if the returned element is really an ...

are there any tutorials to help a proficient c++ programmer learn c?

I became a professional programmer in the era of object oriented code, and have years of experience programming in C++. I often work on large projects that have years of legacy code in a mix of c++ and c. I feel less comfortable working on pure c parts of systems. From programming in C++ I understand all the c syntax, but there's a hole...

Where can I get information about the C/C++ linker in Visual Studio?

I'd like to learn more about C/C++ linker issues and troubleshooting in Visual Studio. I've had linker problems crop up from time to time and they are really annoying since you get such limited information from the linker error messages. I've seen a few not-so-detailed MSDN articles but nothing in depth. Where can I find a good source...

How do I get an address in C?

There is probably a really simple answer to this but I just can't see it. #include <stdio.h> int main(void) { int x = 0; printf("\n%d\n%d\n",x,&x); } Okay, so the printf gives 0 (x) and 2293752 (&x). I am trying to figure out a way to look at any address (&) and see what is already stored there. This is just for experimentation. Any ...

C String literals not in machine code?

I need to slightly change a string in an exe which I dont have source code for anymore. It was writtin in C. I notice that C string literals do not seem to appear in the machine code listing at all - not in raw ASCII anyway, not in utf8/16/32 or anything like that. They seem to be encoded, I am guessing as part of 32bit op-codes. For ex...

Latitude/Longitude storage and compression in C

Does anyone know the most efficient representation for lat/long coordinates? Accuracy level should be enough for consumer GPS devices. Most implementations seem to use double for each unit, but I'm suspicious that a float or fixed point format should be sufficient. I'd be curious to hear from anyone who has tried to compress and or stor...

winapi c - read/write mbr of system drive

Does anyone have any example code that shows how to identify the system boot harddisk, and read/write the mbr from the harddisk in windows xp and vista? I know createfile can be used. But I do not know how to properly identify which device is the boot device. Like how do you find out which ones you are meant to use? \.\Volume{GUID}\...

C: Bus Error using different compilation arguments

So it's the third week of my life in C and I've been given the task of writing a program which takes in upto 100words which are upto 80characters long, calculates the average word length of the inputs, prints words larger than the average, and finally prints the average word size. EDIT: We also have to use an emalloc, a recursive output ...

When exactly is the postfix increment operator evaluated in a complex expression?

Say I have an expression like this short v = ( ( p[ i++ ] & 0xFF ) << 4 | ( p[ i ] & 0xF0000000 ) >> 28; with p being a pointer to a dynamically allocated array of 32 bit integers. When exactly will i be incremented? I noticed that the above code delivers a different value for v than the following code: short v = ( p[ i++ ] & 0xFF) ...

How does FastCGI work on a web server (eg. Apache 2.2+)?

Hi, I had a look at the sources of FastCGI (fcgi-2.4.0) and actually there's no sign of fork. If I'm correct the web server spwans a process for FastCGI module (compiled in it or loaded as a SO/DLL) and handles control of the main socket (the port TCP:80 usually) over to it. On *nix the FastCGI module "locks" that socket using a file w...

C Main Loop without 100% cpu

#include <stdio.h> int main() { while(!DONE) { /* check for stuff */ } return 0; } The above code sample uses 100% cpu until DONE is true. How can I implement a program that loops and only terminates when DONE, but which doesn't use 100% cpu? Modern languages use something like App.ProcessMessages or something like that to g...

How to resize a pixmap with XLib?

I'm using a pixmap as a window's backup in order to restore it under expose events. When the window is resized, must I resize the backup pixmap? If so, what is the best way? Create a new pixmap with the new size? ...

Guaranteed yielding with pthread_cond_wait and pthread_cond_signal

Assuming I have a C program with 3 POSIX threads, sharing a global variable, mutex, and condition variable, two of which are executing the following psuedocode: ...process data... pthread_mutex_lock( &mutex ); variable = data_ptr; pthread_cond_signal( &cond ); pthread_mutex_unlock( &mutex ); And the third running: while(1) { whil...

read from argv[0]

Hi i have a simple C question .. : int main(int argc,char *argv[]) { char *text; int textLen,repNum; text = stream2string(stdin,&textLen); //....text = argv[0] doesnt work :( so how can i read from argv[0]? I use netbeans .. and everytime i have to type in stdin.. when i use argv then the programm will execute without my inp...

Mpi function define

I wrote a program in c using MPI (Message Passing Inteface) that compute recursively the inverse of a lower triangular matrix. Every cpu sends 2 submatrices to other two cpus, they compute them and they give them back to the cpu caller. When the cpu caller has its submatrices it has to perform a matrix multiplication. In the recurrence ...

Compound boolean expressions in C/PHP and variable assignment

Forgive me if the title doesn't exactly match what I'm asking, I'm having a tough time describing the question exactly. Consider the following PHP code: #!/usr/bin/php <?php class foo{ function bar() { return true; } } if (true && $a = new foo() && $a->bar()) { echo "true"; } else { echo "false"; } It give...

Alternatives to popen/pclose?

I'm writing a program that has to execute other external processes; right now the program launches the processes' commandlines via popen, grabs any output, and then grabs the exit status via pclose. What is happening, however, is that for fast-running processes (e.g. the launched process errors out quickly) the pclose call cannot get th...

We have to use C "for performance reasons"

Hello StackOverflowers of the World, In this age of many languages there seems to be a great language for just about every task and I find myself professionally struggling against a mantra of 'nothing but C is fast' where fast is really intended to mean 'fast enough'. I work with very rational open minded people who like comparing numb...

C - Change all values of an array of structures in one line

I can declare a structure: typedef struct { int var1; int var2; int var3; } test_t; Then create an array of those structs structure with default values: test_t theTest[2] = { {1,2,3}, {4,5,6} }; But after I've created the array, is there any way to change the values in the same way I did above, using only one line, spec...

Programatically press a button on another application (C, Windows)

I'm trying to use the following code to press a button on my other application: HWND ButtonHandle; if( (wnd = FindWindow(0, "Do you want to save?")) ) { ButtonHandle = FindWindowEx(wnd, 0, "SaveButton", "&Save"); SendMessage(wnd, WM_COMMAND, MAKEWORD(GetDlgCtrlID(ButtonHandle), BN_CLICKED ), (LPARAM)ButtonHandle); } It doesn...