c

easy c question: compare first char of a char array

How can I compare the first letter of the first element of a char**? I have tried: int main() { char** command = NULL; while (true) { fgets(line, MAX_COMMAND_LEN, stdin); parse_command(line, command); exec_command(command); } } void parse_command(char* line, char** command) { int n_args = 0,...

realloc - converting int to char

I'm converting an array of integers into a char by iterating through the whole array, and then I'm adding the resulting string to ncurses's method new_item. For some reason I'm doing something wrong the way I reallocate memory, thus I get the the first column as: -4 Choice 1 0 Choice 1 4 Choice 2 1 Choic...

What is the rationale for difference between -> and . in c/c++ ?

I know the difference between the member operator (.) and the member by pointer operator (->). Why did the C designers create a different operator for this access? Why can't the compiler figure it out on its own? If you always used a . does any case exist where it is ambiguous whether you mean a member or a member by pointer? edit: I'...

problem with fifos linux

I am having problem debugging why n_bytes in read_from_fifo function in client.c doesn't correspond to the value written to the fifo. It should only write 25 bytes but it tries to read a lot more (1836020505 bytes (!) to be exact). Any idea why this is happening? server.c: #include <stdio.h> #include <stdlib.h> #include <string.h> #inc...

How do I get callgrind to dump source line information?

I'm trying to profile a shared library on GNU/Linux which does real-time audio processing, so performance is important. I run another program which hooks it up to the audio input and output of my system, and profile that with callgrind. Looking at the results in KCacheGrind, I get great information about what functions are taking up mo...

questions about name mangling in C++

I am trying to learn and understand name mangling in C++. Here are some questions: (1) From devx When a global function is overloaded, the generated mangled name for each overloaded version is unique. Name mangling is also applied to variables. Thus, a local variable and a global variable with the same user-given name still get dist...

resizing an array with C

So I need to have an array of structs in a game I'm making - but I don't want to limit the array to a fixed size. I'm told there is a way to use realloc to make the array bigger when it needs to, but can't find any working examples of this. Could someone please show me how to do this? Thanks! ...

Which header files are necessary to run this code snippet?

It's from here,but fails when compiling: int main(int argc, char **argv) { struct hostent { char *h_name; // main name char **h_aliases; // alternative names (aliases) int h_addrtype; // address type (usually AF_INET) int h_length; // length of address (in octets) char **h_addr_list; // altern...

Is it possible to use Boehm garbage collector only for the part of the program?

I've read article in LinuxJournal about Boehm-Demers-Weiser garbage collector library. I'm interesting to use it in my library instead of my own reference counting implementation. I have only one question: is it possible to use gc only for my shared library and still use malloc/free in the main application? I'm not quite understand how...

pop()ing element

In C, when making a pop function for a stack, do I need to rearrange every index, or would I just be able to remove the top index and everything shift up 1 place on it's own? ...

How are open source projects in C/C++ carried out exactly without .sln/.project files?

Seems most open source projects in C/C++ only provide the source code,i.e. nginx Is this a convention that anyone interested in joining the developing team should figure out the .sln/.project files himself to qualify?? ...

array and point problem

Here, I have a bad program. Its outputs confusing me, anyone can tell me why ? #include <stdio.h> #include <stdlib.h> int main() { int i = 0; char *a_result[10]; char *b_result[10]; for (i = 0; i < 10; i++) { char a_array[10]; char *b_array = malloc(10*sizeof(char)); int j = 0; for...

SWIG interface file questions

I am writing a C/C++ extension module for other languages and I am using SWIG to generate the bindings. I have two questions Can I include more than 1 header file in the declaration part of the interface file e.g.: /* Declarations exposed to wrapper: */ > %{ > #define SWIG_FILE_WITH_INIT > #include "a.h" > #include "b.h" > #include ...

How do I know whether c89 or c99 is applied by cl.exe?

I just came across this term in this post, how do I check which rule is actually used by my compiler? Or is it possible to specify a rule for cl.exe? ...

Simulating O_NOFOLLOW (2): Is this other approach safe?

As a follow-up question to this one, I thought of another approach which builds off of @caf's answer for the case where I want to append to file name and create it if it does not exist. Here is what I came up with: Create a temporary directory with mode 0700 in a system temporary directory on the same filesystem as file name. Open fil...

Help with optimizing C# function via C and/or Assembly

I have this C# method which I'm trying to optimize: // assume arrays are same dimensions private void DoSomething(int[] bigArray1, int[] bigArray2) { int data1; byte A1, B1, C1, D1; int data2; byte A2, B2, C2, D2; for (int i = 0; i < bigArray1.Length; i++) { data1 = bigArray1[i]; data2 = bigArray2...

Difference between char a[]="string"; char *p="string";

Possible Duplicates: What is the difference between char s[] and char *s in C? What is the difference between char a[]="string"; and char *p="string";? ...

Struct with pointer to a function

Hello, In a C struct I have defined a function pointer as follows: typedef struct _sequence_t { const int seq[3]; typedef void (* callbackPtr)(); } sequence_t; I want to initialize a var of that type globally with: sequence_t sequences[] = { { { 0, 1, 2 }, toggleArmament }, }; And I keep getting error telling me that there a...

Porting Django's templates engine to C

Hi folks, I recently wrote a simple and tiny embedded HTTP server for my C++ app (QT) and I played a little bit with Ry's http-parser and loved it. This guy is crazy. So I told to myself: "Hey! Why not port the django template engine to C?" That'd be awesome! I know, it won't be an easy task (not at all, I know) but I'd really love to...

realloc(): invalid next size

I'm having a problem with the realloc function. I'm using C only (so no vector) with LibCurl. The problem I'm having is that I'm getting the following error (realloc(): invalid next size) on the 12th iteration of the write_data function (the function I pass to Curl as a callback, it is called each time libcurl has some data to pass back ...