c

Basic C Question

I am reading over the KR book, and am a little stuck. What is wrong with the following? void getInput(int* output) { int c, i; for(i=0; (c = getchar()) != '\n'; i++) output[i] = c; // printf("%c", c) prints the c value as expected output[++i] = '\0'; } When I run the program it never gets out of the loop and I have to c...

Converting a string of numbers into integers

I have a string (char) and I want to extract numbers out of it. So I have string: 1 2 3 4 /0 And now I want some variables, so I can use them as integer: a=1, a=2, a=3, a=4 How can I do that? ...

Initialization of member inside structure

Why can't we initialize members inside the structure ? example: struct s { int i=10; }; Thanks in advance ...

Operating System compile time

This is just a general question - I was sitting and waiting for a bit of software to compile (we use Incredibuild here but can still take 10/15 mins) and it got me wondering, does anyone know how long it took to compile Windows XP or Vista? I did some googling but didn't really find any useful information ...

Strange program hang, what does this mean in debug?

Strange program hang, what does this mean in debug? After attaching windbg I found the following: (1714.258): Access violation - code c0000005 (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. eax=015b5c74 ebx=178a13e0 ecx=dddddddd edx=009a8ca0 esi=09fbf698 e...

How do you compile gnu flex in Visual Studio 2005/2008?

I can't figure this one out. I can download a win32 binary of flex 2.5.4a from gnuwin32, but I'd like to build the latest version (2.5.35) using Visual Studio 2005. I suppose I could build in in cygwin, but where is the fun in that? http://flex.sourceforge.net/ ...

C++ constructs replacing C constructs

After discussing with a newly arrived developer in my team, I realized that there are still, in C++, habits of using C constructs because they are supposed to be better (i.e. faster, leaner, prettier, pick your reason). What are the examples worth sharing, showing a C constructs, compared to the similar C++ construct? For each example,...

How do I programmatically return the max of two integers without...

... using any comparison operators... and without using if, else, etc. ...

Fortran Array to C array. Stupid macro tricks wanted

I have this 'simplified' fortran code real B(100, 200) real A(100,200) ... initialize B array code. do I = 1, 100 do J = 1, 200 A(J,I) = B(J,I) end do end do One of the programming gurus warned me, that fortran accesses data efficiently in column order, while c accesses data efficiently in row order. He suggested that I t...

Solve the memory alignment in C interview question that stumped me

I just finished a test as part of a job interview, and one question stumped me - even using google for reference. I'd like to see what the stackoverflow crew can do with it: The “memset_16aligned” function requires a 16byte aligned pointer passed to it, or it will crash. a) How would you allocate 1024 bytes of memory, and align it to a...

Efficient heap-manager for heavy churn, tiny allocs?

I'm looking for ideas for a heap-manager to handle a very specific situation: Lots and lots of very small allocations, ranging from 12 to 64 bytes each. Anything bigger, I will pass on to the regular heap-manager, so only tiny blocks need be catered for. Only 4-byte alignment is needed. My main concerns are Overhead. The regular libc ...

What is the correct way to declare a pointer to a __far pointer?

On an embedded target I use far pointers to access some parts of the memory map. near pointer (without explicitely specifying __near): unsigned int *VariableOnePtr; Pointer to near pointer: unsigned int **VariableOnePtrPtr; far pointer: unsigned int *__far VariableTwoPtr; What is the correct way to declare a pointer to a far point...

How to declare a structure in a header that is to be used by multiple files in c?

If I have a source.c file and it uses a structure like struct a { int i; struct b { int j; } }; if this structure is to be used by some other file func.c how to do it? shall we open a new header file and declare the structure there and include that header in the func.c? or can we define the total structure in ...

how do you run a c program in cmd

hi friends i am siva sankar reddy s I was just opened the cmd in windows and creted a directory by using "md" command and opened a text file by "notepad " and I did my program in that file and given .c extention to save that file. Now I want to know how to compile and run that c program to get out put. One more thing is I need all ....

Getting absolute path of a file

How can I convert a relative path to an absolute path in C on Unix? Is there a convenient system function for this? On Windows there is a GetFullPathName function that does the job, but I didn't find something similar on Unix... ...

In **portable C**, how to launch a command connecting the command's stdin to the launcher's stdout?

In a C program (p1), how to launch a dynamically constructed command (and its arguments) that reads its standard input from p1's standard output? Note that: A method other than this stdout --> stdin piping is also OK provided it is PORTABLE across Windows and Linux. I cannot use C++, Java, Perl, Ruby, Python, etc here. Also, will t...

C for loop implemented differently than other languages?

I read the following in a review of Knuth's "The Art of Computer Programming": "The very 'practicality' means that the would-be CS major has to learn Kernighan's mistakes in designing C, notably the infamous fact that a for loop evaluates the for condition repeatedly, which duplicates while and fails to match the behavior of most other ...

Difference between files writen in binary and text mode

What translation occurs when writing to a file that was opened in text mode that does not occur in binary mode? Specifically in MS Visual C. unsigned char buffer[256]; for (int i = 0; i < 256; i++) buffer[i]=i; int size = 1; int count = 256; Binary mode: FILE *fp_binary = fopen(filename, "wb"); fwrite(buffer, size, count, fp_binary)...

What's the best way to check if a file exists in C? (cross platform)

Is there a better way than simply trying to open the file? int exists(const char *fname) { FILE *file; if (file = fopen(fname, "r")) { fclose(file); return 1; } return 0; } ...

Counterpart of PHP's isset() in C/C++

PHP has a very nice function, isset($variableName). It checks if $variableName is already defined in the program or not. Can we build similar feature for C/C++ (some kind of symbol table lookup)? ...