c

lseek() returning 0 when followed by new open()

I have the following bit of code (it's "example" code, so nothing fancy): #include <stdio.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <unistd.h> int main() { char buffer[9]; int fp = open("test.txt", O_RDONLY); if (fp != -1) // If file opened successfully { off_t offset = lseek(fp,...

When is pointer to array useful?

I can declare: int (*ap)[N]; So ap is pointer to int array of size N. Why is this ever useful? If I pass it to function, what useful thing it can do with it that it could not do with just a normal pointer to the array's contents? C FAQ say: 2.12: How do I declare a pointer to an array? Usually, you don't want to. ...

Read instructions being executed

As the title suggests, is there any way to read the machine code instructions as/after they have been executed? For example, if I had an arbitrary block of C code and I wanted to know what instructions were compiled and executed when that block was entered then would there be a way to do that? Thank you in advance for any pointers on the...

Global variable in a different library, C

I am trying a global variable to hold my error message in C. One library called Utils has: #ifndef private_error_h #define private_error_h extern char error[1024]; __declspec(dllexport) void FillError(char* newError); #define GetErr() error #endif File error.c: #include "private_error.h" char error[1024]; void FillError(char* ne...

lvalue and rvalue

Just wonder if a literal string is a lvalue or a rvalue. Are other literals (like for int, float, char etc) lvalue or rvalue? Is the return value of a function a lvalue or rvalue? How do you tell the difference? ...

When to free memory inside a C code ?

Hello, When I alloc memory outside a while loop for example, is it okay to free it inside it ? Are these two codes equivalent ? int* memory = NULL; memory = malloc(sizeof(int)); if (memory != NULL) { memory=10; free(memory); } int* memory = NULL; memory = malloc(sizeof(int)); if (memory != NULL) { memory=10; } free(memory); ...

"We write a .h file for every module in our design" what is the module referring to?

"We write a .h file for every module in our design" what is the module referring to? and when do we write a separate .c file? ...

In C and C++, why is each .h file usually surrounded with #ifndef #define #endif directives?

Why does each .h file starts with #ifndef #define #endif? We can certainly compile the program without those directives. ...

Ugly Macro Interpretation (just 1 line)

#define STRING(s) (((String*)s)-1) what in the world is (((String*)s)-1)? typedef struct String { int length; int capacity; unsigned check; char ptr[0]; } String; ...

Strangest Error I Have Ever Seen, a.k.a. ) *

I'm writing this awesome application, at least I think it awesome, in C with the magnificent blend of GObject and after a while I start getting this very, extremely strange error. I also believe to have noticed it not appearing always. However this might just be the IDE's fault. Anyhow... GCC, apparently, complains: expected ')' before ...

How to recursively traverse directories in C on Windows

Ultimately I want to travel through a folder's files and subdirectories and write something to all files i find that have a certain extension(.wav in my case). when looping how do i tell if the item I am at is a directory? ...

C: scanf add to total

A quick question, is there a way to perform these 3 operations: while(...) { fscanf(input, "%lf %lf", &t, &y); tTotal += t; yTotal += y; } in one operation where t and y add themselves to tArray and yArray respectively, inside the scanf statement? Something of the form fscanf(input, "%lf %lf", ...code..., ...code...); Thanks, Ash....

warning C4047: '=' : 'int' differs in levels of indirection from 'char *'

int utstrlen(char* src){ int length=0 ; if(isOurs(src) ==1){ length = src - 2 * sizeof(int); } return length; } I got the warning C4047: '=' : 'int' differs in levels of indirection from 'char *' error from line length = src - 2 * sizeof(int); can someone explain what's wrong with it? Thank you! ...

Simple image loading libraries

I have an app that uses this library (actually a direct port to D) for some image processing. I'm looking for some other libraries of a similar style to use to load other file types. Things I need/want: Loss less format. Simple C API. Loads data into buffers in a raw pixel format. Open source (as in I can get source files and compile ...

Why are drivers and firmwares almost always written in C or ASM and not C++?

Dear developers, I am just curious why drivers and firmwares almost always are written in C or Assembly, and not C++? I have heard that there is a technical reason for this. Does anyone know this? Lots of love, Louise ...

Effectiveness of GCC optmization on bit operations

Here are two ways to set an individual bit in C on x86-64: inline void SetBitC(long *array, int bit) { //Pure C version *array |= 1<<bit; } inline void SetBitASM(long *array, int bit) { // Using inline x86 assembly asm("bts %1,%0" : "+r" (*array) : "g" (bit)); } Using GCC 4.3 with -O3 -march=core2 options, the C version t...

Fastest way to find out minimum of 3 numbers?

In a program I wrote, 20% of the time is being spent on finding out the minimum of 3 numbers in an inner loop, in this routine: static inline unsigned int min(unsigned int a, unsigned int b, unsigned int c) { unsigned int m = a; if (m > b) m = b; if (m > c) m = c; return m; } Is there any way to speed this up? I am ok ...

why when writting more than once to socket, it blocks?

hi I am facing a problem with socket and I would be glad if you could help ... The problem is that when I send data more than once it blocks, e.g: //--- client --- //.. send(sock, buf_1, sizeof(buf_1), 0); for (x10){ //... send(sock, buf_2, sizeof(buf_2), 0); if (recv(sock, buf_2, sizeof(buf_2), 0)<0) printf("recv_2() fai...

Regular expression for a string literal in flex/lex

I'm experimenting to learn flex and would like to match string literals. My code currently looks like: "\""([^\n\"\\]*(\\[.\n])*)*"\"" {/*matches string-literal*/;} I've been struggling with variations for an hour or so and can't get it working the way it should. I'm essentially hoping to match a string literal that can't conta...

How to get `gcc` to generate `bts` instruction for x86-64 from standard C?

Inspired by a recent question, I'd like to know if anyone knows how to get gcc to generate the x86-64 bts instruction (bit test and set) on the Linux x86-64 platforms, without resorting to inline assembly or to nonstandard compiler intrinsics. Related questions: Why doesn't gcc do this for a simple |= operation were the right-hand sid...