c

Why is a variable length array not declared not as a pointer sometimes?

I see this in code sometimes: struct S { int count; // length of array in data int data[1]; }; Where the storage for S is allocated bigger than sizeof(S) so that data can have more space for its array. It is then used like: S *s; // allocation s->data[3] = 1337; My question is, why is data not a pointer? Why the length-1 ...

Recursive file delete in C on Linux

I have a C program that, at one point in the program has this: system("rm -rf foo"); Where foo is a directory. I decided that, rather than calling system, it would be better to do the recursive delete right in the code. I assumed a piece of code to do this would be easy to find. Silly me. Anyway, I ended up writing this: #include ...

How does return of pointer work in strcat()

Hey guys I'm trying to figure how pointers are returned by strcat(), so I tried implementing my own strcat() to see how it works. The following is my code for mystrcat(), which works like the real strcat(): char *mystrcat(char *destination, char *source) { char *str = destination; while (*str != '\0') { str++; }...

How can a program measure device size in AIX?

My C program mounts a raw device, then lseeks to its end to find the length. This works well in Linux and Windows, but fails in AIX - apparently you can seek to any location, even past the end. Any idea on how to do it correctly in C? ...

Generating totally random numbers without random function?

Possible Duplicate: True random number generator I was talking to a friend the other day and we were trying to figure out if it is possible to generate completely random numbers without the help of a random function? In C for example "rand" generates pseudo-random numbers. Or we can use something like "srand( time( NULL ) );...

Textbox with a label in a GTK+ program

Hello, I am new to GTK+ programming.I wrote a simple GTK+ program where i display a label and a textbox in a window, the label should be to the left of the textbox and i should be able to specify the horizontal length of the textbox. Below is my code so far,the program runs fine but im unable to align the label to the left of the textbo...

A function in a pthread

Can I call another function in a thread runner function, called by a pthread_create()? Are there any restrictions on such functions? ...

Understanding scanf() in C (Seg Fault)

I don't understand getting input in C. I have the following code which is producing a segmentation fault: int main(int argc, char *argv[]){ while (fgets(buffer, MAX_LEN + 1, input) != NULL) { get_command(t, buffer); } return 0; } and static void get_command(Table *t, char *command) { COMMAND command_name = 0; char *valid_...

Can I use sizeof() or a #define for precision in sprintf of a string?

Lookign at: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ for string (%.Ns) precision. When I use sizeof or a #define length in the precsion it reads it as actual text. Why is this? What are the rules of this? Does it have to be an integer value only? i.e. - buffer[50]; sprintf (buffer, "%.sizeof(buffer)s", stri...

High-performance message passing from C to Erlang

I have a C server (a data feed handler) that has the potential to send millions of tiny messages per second across a few thousand long-lived Erlang processes. In a single day, some of these processes will receive a few thousand messages, while others will receive tens of millions. My interests are threefold: to minimize latency — shor...

Can someone help me understand how to allocate properly in c?

I don't think I properly understand how to allocate memory for what I want to do. I would like my program to store arguments from the command line into an array of stucts called Command which has char **args in it. for example if I run ./test.c echo hello : ls -l I want it to store it as this commands[0].args[0]= echo commands[0]....

How many bytes does a #define string (string literal) take?

#define STR "test1" Why does this take 6 bytes? sizeof(STR) = 6 ...

Can I use two precision '*' in a string format of two strings?

.* The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. #define SUFF ".txt" #define MAX_STR 50 fileName[MAX_STR]; name ="myFile" sprintf( fileName, "%s%s", name, SUFF ); //fileName = "myFile.txt" Now I want to bound the str...

Opening a cash drawer using C/C++ or Java

I need to open a cash drawer using C/C++ or Java. It is a POS-X cash box with a USB connection. I've never done anything like this before. I noticed the cash box was linked to the "COM3" port. I know Java does not have a USB API so I turned to C/C++. ...

C fscanf problem

If I open a file and use fscanf to read a file like this: 2 41 1 50 1 46 .... How do I tell C to read the first number and store it as a variable, then the second as another variable, run a loop, then move on to the next set? ...

In C, does using static variables in a function make it faster?

My function will be called thousands of times. If i want to make it faster, will changing the local function variables to static be of any use? My logic behind this is that, because static variables are persistent between function calls, they are allocated only the first time, and thus, every subsequent call will not allocate memory for ...

Redirecting forked process output to parent process in C

What I am implementing is a (simpler) version of bash. One of the tasks is to accept the command : $ bg <command> <arguments> This will then fork a new process and then run execvp() in the new process using <command> and <arguments> as parameters. The problem is that when I capture the output from the child process, I use pipe(), an...

Simple swap function...why doesn't this one swap?

I'm new to C and still trying to grasp the concept of pointers. I know how to write a swap function that works...I'm more concerned as to why this particular one doesn't. void swap(int* a, int* b) { int* temp = a; a = b; b = temp; } int main() { int x = 5, y = 10; int *a = &x, *b = &y; swap(a, b); printf(“%...

fflush and while loop

Hello, I have been trying to use fflush to make a progress bar. To test fflush, I wrote the small code below. It works as it supposed to when I uncomment "sleep(1);" but it works in an unexpected way if it remains commented out. It prints the first dash, waits than prints all remaining 9 of them and quits. I don't understand why it...

passing variable number of arguments

Can we pass variable number of arguments to a function in c? ...