c

How to set pointer reference through a function.

In C, I am trying to set a pointer's value by sending it to a function, but the value wont change outside of the function. Here is my code: #include <stdio.h> void foo(char* str) { char* new_str = malloc(100); memset(new_str, 0, 100); strcpy(new_str, (char*)"new test"); str = new_str; } int main (int argc, char *argv...

How do I receive raw, layer 2 packets in C/C++?

How do I receive layer 2 packets in POSIXy C++? The packets only have src and dst MAC address, type/length, and custom formatted data. They're not TCP or UDP or IP or IGMP or ARP or whatever - they're a home-brewed format given unto me by the Hardware guys. My socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW) never returns from its recvfrom(). ...

Are there advantages to a crashing on a secondary thread versus the main one?

I came across this code in a large codebase DWORD WINAPI ThreadFunc (LPVOID lpParam) { int *x = 0; *x = 1234; // Access violation return 0; } void Manager::Crash () { Log("Received a remote command to crash Server."); DWORD dwThreadId, dwThrdParam = 1; HANDLE hThread = ::CreateThread(NULL, 0, ThreadFunc, &d...

I'm having trouble finding example code for libftdi's mpsse (SPI) mode.

This is not a homework problem, though it is a work problem. Where months ago, I would have just written up a specification and the boss would have contracted it out, money's tight. So I'm trying to do this myself. I'm a weak C coder, and I'm lucky if gcc spits out something that will run without segfaulting, or sometimes anything at al...

FFmpeg: Jpeg file to AVFrame

Hi! I need to join several jpg files into video using FFmpeg library. But I have a problem with reading this files. Here is a function which reads image file and makes AVFrame: AVFrame* OpenImage(const char* imageFileName) { AVFormatContext *pFormatCtx; if(av_open_input_file(&pFormatCtx, imageFileName, NULL, 0, NULL)!=0) {...

write a c program/ factorial of N using do while

it factors a # 25=5 ...

dereferencing an array

Using gcc 4.4.3 c89: I am just working on the following code below: I just have some questions about dereferencing the src pointer. Which is declared in the calling function as: char src[] = "device_id"; My confusion rests with dereferencing the src: for(i = 0; src[i] != '\0'; i++) { printf("src %d: [ %s ]\n", i, &src[i]); } ...

Serial port library/header file in C for WinCE

Hi, I am a newbie in WinCE application programming. But the starting is not as smooth as expected. I could not find code examples, tutorials to learn from. I could not find a book on WinCE programming. Are all the libraries and header files for winCE same as for windows (WIN32)? I am programming in C. I want to use serial port communi...

Implicit declaration in C

Does the following program invoke Undefined Behaviour in C? int main() { printf("Printf asking: Where is my declaration ?"); } In the above program there is an implicit declaration of printf(), so is the above code fully standard compliant or it just has some implementation specific behaviour? ...

Is there a better way to do this?

I'm drawing 2D, concave, sometimes multicontoured, sometimes self intersecting polygons with OpenGL. Here is a sample: Right now, I take the points which if connected would result in the polygon's outline. Then I put these into the GLUTesselator where triangles come out. I then make texture coordinates and texture the polygon. The abs...

No Stackoverflow: auto object inside while loop

I was going through someone's code where I came across a thread: while(TRUE) { ...... STRUCT_MSG_SYS_HEADER sysHdr; ..... .... } There are five threads like this, My point is that "STRUCT_MSG_SYS_HEADER sysHdr;" will lead to stackoverflow after some time or days... (Not tested though). So I decided to write a simple sample appl...

Execises on pointers and arrays

Hello, gcc 4.4.3 c89 I start a new c programming job in the next 4 weeks. And I want to brush up on my pointer and arrays skills for some advanced techniques. My new job requires the use of this very much. I have been browsing the Internet for some possible questions and answers for pointer and arrays exercises. But have not really fo...

What could be the fastest and least painful way to learn LISP for a C developer?

I have been working as a C developer on Linux platform for sometime now. Recently finished K & R and did a little study of implementing OOP in C. Beside that I have studied C++ and Java. All of it has been on Linux platform. Now I plan to learn LISP. I have gone through LISP discussions directed towards beginners on SO, especially What’...

Struct pointer errors

int d() {return 0;} int i() {return 7;} struct a { int(*b)(); }c={d}; typedef struct e{ struct a f; }g; main() { struct e *h; h->f.b = i; } I am getting segmentation fault when I try to run this program. Can anyone justify the reason? And I also tried like int d() {return 0;} int i() {return 7;} struct a { int(*b)(); }c={d}; typ...

directly execute binary resource

lpBuffer is a pointer to the first byte of a (binary)resource. How can I execute it straight away without dumping it to a temporary file? HMODULE hLibrary; HRSRC hResource; HGLOBAL hResourceLoaded; LPBYTE lpBuffer; hLibrary = LoadLibrary("C:\\xyz.exe"); if (NULL != hLibrary) { hResource = FindResource(hLibrary, MAKEINTRESOURCE(104)...

Problem with encryption & decryption of binary files

How can I encrypt & decrypt binary files in C using OpenSSL? I have a test program that encrypts and then decrypts the input it's given. I executed my test program for text files, and the output is the same as the input, but when I execute my test program on a binary file the output is not the same as the input. ...

Please, help to find source with examples by glib

Hi everybody! I've looked for a lot of sites but didn't find any examples by glib. I've seen only discription types and functions... Perhaps do you know a good source? PS. Thanks for your answers! =) ...

Issue in pausing the posix thread for specific time interval in c

I have to call one method in seperate thread (i am using posix thread)which updates some value after every 5 second for which have written below code. void *threadEntry(void *) { while(1) { updateValue();//to update some values usleep(5000000);//pause for 5 second } } pthread_t thread1; pthread_create(&thread1,NULL,thread...

Shortest C code to reverse a string..

Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the reversed string.. (not using a string reverse library function either)? char * reverse_str(char * s) { char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s...

Do you know about %#x, in C language format string.

KdPrint(( "Unknown IoControlCode %#x\n", io_stack->Parameters.DeviceIoControl.IoControlCode )); It's weird. What does sharp mean? ...