c

How to compare two structs in c ?

Hi! I know, that if i want to compare two structs than i have to write it for myself, because there isn't any function for this, but i can't figure out how should i do that. I have three structs : primary, secondarystruct, and difference(this should contain the different items). All three has the following members : char * filename, cha...

What does "..." mean in a C function declaration?

What does this mean? void message(int x, int y, ...) I can't understand what ... is. Can anybody explain? ...

how to tell if a stream is closed in C before calling fclose()

I have a failing C program, and i've narrowed it down to a fork()ed child trying to close stdout and stderr, which were closed by its parent process before calling fork() - i assume those streams were passed on to the child process. how can i tell if a stream is closed in C before attempting to close it using something like fclose(stdou...

Where is `%p` useful with printf?

After all, both these statements do the same thing... int a = 10; int *b = &a; printf("%p\n",b); printf("%x\n",b); So, is there some good use of the %p option? ...

booting from a disk/cd/usb

How can I boot my small console from a disk/cd/usb, with the following configuration: The media that I want to use will be completely raw i.e no filesystem on it. When I insert the media in my system or assume that its already inserted, I want to make it boot my own small OS. The procedure that I want to use is that when my system st...

[C++/C] C++ program gets undefined reference to a dynamic C library during linking

I've created a dynamic networking library in C. When I try to link it in my C++ program I get an undefined reference the first time I call one of my functions in the library. What I can't understand is that I created a little test program in C that uses the library and it links just fine. Could someone help with my problem? Here are the...

C multi-line macros

Possible Duplicate: Whats the use of do while(0) when we define a macro? Why do people use do { statement1; statement2; statement3; etc; } while(0); instead of the shorter form {stmt1; stmt2; stmt3; etc; } in C multi-line macros? ...

Can I set the name of a thread in pthreads / linux ?

Is there any way of setting the name of a thread in linux ? My main purpose is it would be helpful while debugging, and also nice if that name was exposed through /proc/ ...

Does program need additional symbols from .so shared library except those declared in header file?

In C programming, I thought that a object file can be successfully linked with a .so file as long as the .so file offers all symbols which have been declared in the header file. Suppose I have foo.c, bar.h and two libraries libbar.so.1 and libbar.so.2. The implementation of libbar.so.1 and libbar.so.2 is totally different, but I think ...

filling an array with random number

Hi there, I trying to fill an array of 20 ints with numbers from 1-20 in random sequence. here's my code: int lookup[20]={0}; int array[20]={0}; srand(time(NULL)); for(int i=0;i<20;++i){ bool done=false; while(!done){ int n=rand()%20; if(lookup[n]==0){ array[i]=n; lookup[n]=1; done...

Measuring process statistics in Linux

I am building programming contest software. A user's program is received by our judging system and is evaluated by compiling it and running it via a fork() and exec(). The parent process waits for the child (submission's process) to exit, and then cleans it up. To give useful information about the program's run, I want to measure the CP...

Order of assignment evaluation (Have I found my first compiler bug?)

This code has an interesting bug: some_struct struct_array1[10] = {0}; some_struct struct_array2[10] = {0} int i; for (i = 0; i < sizeof(struct_array1) / sizeof(struct_array1[0]); struct_array1[i].value = struct_array2[i++].value = 1) ; For most compilers, the above code results in setting the "value" field of all str...

How to write C .so library to subsitute existing C++ .so library?

Hi, all Let me explain the scenario. We have a legacy C++ compiled .so library. The functions in this library are declared with extern "c" {}, so the library can be used by both C and C++ program, plus, for some reason it was created with--static-libgcc option. This old library is very old and hard to maintain. Now we haved managed to...

Buffered reading from stdin using fread in C

I am trying to efficiently read from the stdin by using setvbuf in `_IOFBF~ mode. I am new to buffering. I am looking for working examples. The input begins with two integers (n,k). The next n lines of input contain 1 integer. The aim is to print how many integers are divisible by k. #define BUFSIZE 32 int main(){ int n, k, tmp, ans=...

Invalid preprocessor token warning message

I have a bunch of generated functions from a supplier's tool are required to be defined by me. Since the inner functionality of each and every one of these functions are exactly the same, I figured I could use a macro to make my life easier. Here is the offending warning: pasting "<function_name>" and "(" does not give a valid preproce...

Dealing with data on multiple TCP connections with epoll

I have an application that is going to work like a p2p-software where all peer are going to talk to each other. Since the communication will be TCP i thought that I could use epool(4) so that multiple connections can be handled. Since each peer will send data very often, I thought that I will establish a persistent connection to each pee...

how to get the IP address and port number from addrinfo in unix c

i need to send some data to a remote server via UDP in a particular port and get receive a response from it. However, it is blocking and I get not response. I needed to check if the addrinfo value that I get from the getaddrinfo(SERVER_NAME, port, &hints, &servinfo) is correct or not. how do i get the ip address and port number from this...

c program client server

hi all I have written a client and server c program, which I have taken from example code. I want to write a iterative client and server program, i.e. after client send a string, then the server print that string and then send back a string to client then the client print the string inputted by server, and so on until the client input...

Hot to get random date in range

I have two dates as strings (dd-mm-yyyy). How can i get a random date between these two dates? ...

Why do we follow opposite conventions while returning from main()?

I have gone through this and this, but the question I am asking here is that why is 0 considered a Success? We always associate 0 with false, don't we? ...