c

How do I do this clever casting in C?

You can see what i'm trying to do below: typedef struct image_bounds { int xmin, ymin, xmax, ymax; } image_bounds; #define IMAGE_BOUNDS(X) ((image_bounds *)(X)); typedef struct { image_bounds bounds; float dummy; } demo; int main(void) { demo my_image; /* this works fine */ ((image_bounds *)(&my_image))->xmin...

Copying C-Style String to Free Store Using Only Dereference

As said in the title, the goal is to copy a c-style string into memory without using any standard library functions or subscripting. Here is what I have so far [SOLVED] #include "std_lib_facilities.h" char* strdup(const char* p) { int count = 0; while(p[count]) ++count; char* q = new char[count+1]; for(int i = 0; i < c...

Good way to organize C source files?

The way I've always organized my C source was to put struct, macro and function prototypes in header files and function implementations in .c files. However, I have recently been reading alot of other peoples code for large projects and I'm starting to see that people often define things like structs and macros in the C source itself, im...

C Golf: # spaces in string

I'm trying to come up with with shortest C function (including spaces, any macros that are substituted into it) possible that is passed a NULL-terminated string and returns the number of spaces in that string. This is what I've got so far: int n(char*l){return*l?!(*l-32)+n(++l):0;} Which is 42 characters long. Any improvements? ...

C, C++ preprocessor macro

Can anyone please explain how this works #define maxMacro(a,b) ( (a) > (b) ) ? (a) : (b) inline int maxInline(int a, int b) { return a > b ? a : b; } int main() { int i = 1; j = 2, k = 0; k = maxMacro(i,j++); // now i = 1, j = 4 and k = 3, Why ?? Where is it incremented ? //reset values i = 1; j = 2, k = 0; k = maxInlin...

program simple tone generator that plays two tones one in each of the two stereo

i wish to generate two opposing tone one in the right stereo channel and one in the left stereo channel both at different frequencies i wish to accomplish this in c or objective c ...

How to get the inside dimensions of a decorated window in XWindows?

If I create a full screen window where m_winw and m_winh is the full screen size, it seems to create a window for me where the outside dimension is the full screen and the inside is smaller based on the "decoration" (window border) size. Is there a way to query the window to get it's inside width and height? m_win=XCreateWindow(m_displa...

How to copy string to clipboard in C?

The SetClipboardData function requires a HANDLE reference; I'm having trouble converting my string for use in the function. Here is my code: char* output = "Test"; HLOCAL hMem = LocalAlloc( LHND,1024); char* cptr = (char*) LocalLock(hMem); memcpy( cptr, output, 500 ); SetClipboardData(CF_TEXT, hMem); LocalUnlock( hMem ); LocalFree( hM...

Ping servers and check the result from C program?

Hi This is My last question. Now my new requirement is to ping some set of servers and check if they are replying or not. I am trying my way of system("ping xxx.xx.xx.xx >out.txt"); And then parsing the out.txt for a string "Request timed out.". This is yielding me good results. But is there any better way to do from c program. Non p...

string concatenate char* with LPCTSTR

LPCTSTR Machine=L"Network\\Value"; char s[100]="Computer\\"; strcat(s,(const char*)Machine); printf("%s",s); Here i received output Computer\N only i expect output like Computer\Network\Value . Give Solution for that.. ...

Decode WMA with FFMpeg to PCM

Hello, I want to decode a WMA stream to 16 Bit PCM. Now i have a Question concerning FFMpeg- what is the output format of .. len = avcodec_decode_audio2(c, (int16_t *)outbuf, &outbuf_used, inbuf_ptr, size); is this the right function for this task? Thank you ...

Internal implementation of atomic_cas_64() on Solaris on Sparc?

On 64-bit Solaris on Sparc, is the atomic_cas_64() function call implemented using load-link/condition-store? If not, what if any API does Solaris offer for user-mode C code to utilize ll/sc? ...

Link-scope IPv6 Multicast packets suddenly not routable on a MacBook Pro?

Hi all, This is a slightly obscure question, but I'm stumped and I thought maybe somebody out there might have more of a clue on the issue. My co-worker has been successfully running an in-house application that uses IPv6 multicasting on his MacBook Pro for several months, but today the Mac decided to stop routing the multicast packets...

Structure Problem in C

Hi My structure looks as follows: typedef struct { unsigned long attr; char fileName[128]; } entity; Then I try to assign some values but get an error message... int attribute = 100; char* fileNameDir = "blabla....etc"; entity* aEntity; aEntity->attr = attributes; aEntity->fileName = fileNameDir; Compiler tells me: ...

Sparse Multi-Dimensional Data Representation

I'm working on a cardiac simulation tool that uses 4-dimensional data, i.e. several (3-30) variables at locations in 3D space. I'm now adding some tissue geometry which will leave over 2/3 of the points in the containing 3D box outside of the tissue to be simulated, so I need a way to efficiently store the active points and not the othe...

Library for password protecting PDF from copying, editing and printing (not viewing)

I'm looking for a good and lightweight library (preferably C or C++) to password protect PDF files from copying, editing and printing. Preferably free or cheap commercial variant, since i want library only to use this functionality. Thanks in advance. ...

Memory snapshot of a huge C/C++ project (Windows/Unix)

I'm trying to take a snapshot of the memory used by a large application running on both Unix / Windows. My ultimate aim would be to have a kind of chart breaking down the memory used by which area of code. The program is split into about 30 different projects most of which are either static libraries or dynamic dlls. Some of these are w...

What is activation record in the context of C and C++?

What does it mean and how important to know about it for a C/C++ programmers? Is it the same across the platforms, at least conceptually? I understand it as a block of allocated memory used to store local variable by a function... I want to know more ...

How do I filter FindFirstUrlCacheEntry()?

I have studied the documentation on MSDN, but it doesn't work for me, I am wondering what I'm doing wrong? My code is as follows, I am trying to filter my IE cache, for example, to get only the cookies: #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <wininet.h> int main(void) { DWORD dwEntrySize; DWORD dw...

Memory leak question in C after moving pointer (What exactly is deallocated?)

Hi! I realize the code sample below is something you should never do. My question is just one of interest. If you allocate a block of memory, and then move the pointer (a no-no), when you deallocate the memory, what is the size of the block that is deallocated, and where is it in memory? Here's the contrived code snippet: #include <std...