c

Need advice on code snippet.

Hey there, this is my first post so please don't be too hard on me. I wrote an "insert" function to insert an integer into an array of integers. It works, but I don't know if it's the best algorithm. Here's my code: int* insert(int *dest, size_t len, unsigned int index, int value) { int x = 0, i = 0; int *stackp = calloc(len+1, sizeof...

Properly getting output from a pipe in C / C++

I am writing C / C++ code to run a shell command and capture the output: static const int BUFFER_SIZE=512; // (...) FILE* pipe; char buffer[BUFFER_SIZE]; // (...) if (!(pipe = popen(cmd.c_str(), "r"))) { out("\nProblem executing command: " + cmd + "\n"); return 1; } while (!feof(pipe)) { int read = fread(buffer, sizeof(char),...

Returning strings from functions that are of unexpected length?

C is always pretty awkward with strings, but it's usually okay to just allocate a char array of size 256 for your string and be on with it. However, what if you want the function to return a string and you don't know the size, since you will be concatenating strings together dozens or hundreds of times? Obviously something like this ...

Using comma to prevent the need for brace pair

Sometimes, when I have a multi-case if, or a very simple for, with only two statements, I will forgo braces, instead using the comma. Is this a bad exploitation of the feature, and is it ugly and bad form? Or is it an acceptable way to save time and space? For example: if (something) b = y, c = z--; instead of: if (something) { ...

Does this trick always work?

Possible Duplicate: Potential Problem in Swapping values of two variables without using a third variable I recently read in a community that we can easily swap two numbers without using third using a XOR trick. m^=n^=m^=n; trick was mentioned. What do you guys think? Is this trick always useful? ...

Relax void * casting in C++

In C, it's not an error to cast pointers to and from void *. A major obstacle in porting to C++ is the need to cast pointers when returning from functions dealing with generic pointers such as malloc, and functions declared in my own code such as void *block_get(Blkno const blkno);. My code however is intended to be compiled by C and C...

sizeof empty structure is 0 in C and 1 in C++ why?

Possible Duplicates: Empty class in C++ What is the size of an empty struct in C? I read somewhere that size of an empty struct in C++ is 1. So I thought of verifying it. Unfortunately I saved it as a C file and used <stdio.h> header and I was surprised to see the output. It was 0. That means struct Empty { }; int main(v...

C++ l10n, i18n solution with GNU gettext, libunistring

Hello, I'm writing a C++ application which shall be locale independent, so I decided to use UTF-16 as my in-memory representation for strings/texts (the application should be as platform independent as possible). For localize the application's messages i want to use GNU's gettext library. Since this library seems to use GNU's libunistri...

Static fields - initialization and changing the value.

Given the files: // file: q7a.h static int err_code = 3; void printErrCode (); ///////////// END OF FILE ///////////////// // file: q7a.c #include <stdio.h> #include "q7a.h" void printErrCode () { printf ("%d ", err_code); } ///////////// END OF FILE ///////////////// // file: q7main.c #include "q7a.h" int main() { err_code = 5; printEr...

Why isn't Visual Studio allowing me to compile this Bresenham code?

I want to make this code run, but I'm still too much of a n00b on C, and OpenGL and VS 2008 to identify on which of them is the fault: #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> #include<math.h> void init(void) { //set display-window background color to white glClearColor(1.0,1.0,1.0,0.0); //set projection paramaters...

How do I get a date or datetime out of an SQLite3 row?

There is no function as sqlite3_column_date which I need: int i = 0; for (NSString *key in [self valuesWithTypes]) { id value; if ([[[self valuesWithTypes] objectForKey:key] isKindOfClass:[NSString class]]) { value = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)] autorelease]; } else if ([[[self v...

Any way to use HTML as an interface to a C/C++ program?

I'm developing a product that is interfaced over USB. I want to write a control app for it, but I cannot program a GUI for poo, so I came up with the idea of using a web page (local to the app's install directory) as the interface to the program. So, the line of communication would be: User --> HTML page (not hosted - it's local) --> C+...

complicated expression involving logical AND (&&)

void main(void) { int x,y,z; x=y=z=1; z = x && y && ++z;//is this fine? } I have lately started reading about sequence points stuffs but I cannot figure out whether the above sample of code is fine or not. I know the && operator introduces a sequence point so I am not very sure about the behavior of the expression z = x && y && ...

Is starting a one-word function with a capital letter a good practise?

I have read in K&R that functions containing more than one word should be capitalized.Should one-word functions like prime(),be written as Prime()?Is this a good practise? ...

How do I write to file in binary in C?

Why does this code not work as expected? #include <cstdio> int main() { char mona[] = "\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x01\x90" "\x00\x00\x02\x5d\x01\x03\x00\x00\x00\x26\xef\xb3\x78\x00\x00\x00\x45\x74\x45\x58" // <snip> "\x00\x49...

Java array initialization list ending with a comma

The comma , items separator used in an array initialization list may end the list in C, this is mentioned in The C Programming Language 2nd ed by Kernighan & Ritchie . e.g. int c[] = { 1, 2, 3, }; This is convenient when the list is long, and one doesn't want to have to change/check the previous line when adding items long long...

C: linked lists confusion

error: dereferencing pointer to incomplete type The problem line is "gl->point[0] = (struct list *)" I read somewhere that I could be storing a declaration. If that is the case I need that explained to me. struct ref { char **name; struct list **point; }; int main ( ) { typedef struct { char **name; s...

My strstr() returning null even if the string to be found is at the index 0

I implemented strstr() myself,the code works fine for all strings but when the string lies in the first index of the string,it returns null. #include<stdio.h> const char* mystrstr(const char *str1, const char *str2); int main() { const char *str1="chal bhai nikal"; const char *str2="c",*result; result=mystrstr(str1,str2); if(*resu...

Why might string concatenation prefix each concatenation with garbage?

I'm pretty new to C, so I apologize if this is pretty standard knowledge.. I have a function like so, where I am appending a bunch of C-style strings together and outputting it: char *example(int n, int days, int years){ char *ret; if (n < 5) { ret = (char*)malloc(sizeof(char)*256); sprintf(ret, "There are %d da...

Using assembly routines with C on DOS

Hello, I've been playing with DOS real mode assembly for a while and now I want to utilize some routines in a C program. I'm using Turbo C 2.01 and TASM 3.0. I'm however unable to modify a variable passed by address, see the _setval routine below. I don't need/want inline assembly. A simple example: foo.c #include <stdio.h> extern voi...