c

C/C++ counting the number of decimals?

Lets say that input from the user is a decimal number, ex. 5.2155 (having 4 decimal digits). It can be stored freely (int,double) etc. Is there any clever (or very simple) way to find out how many decimals the number has? (kinda like the question how do you find that a number is even or odd by masking last bit). ...

How do I use SDL to blit an image in multiply mode?

In Pygame, there is a special_flags argument to surface.blit. This let you combine pixels in ways different than simply copying them. For example, the multiply mode lets you multiply the pixels together. How do I achieve this using SDL from C? I've looked at the SDL documentation, and I don't see any obvious ways of doing this. ...

dereferencing pointer to integer array

I have a pointer to integer array of 10. What should dereferencing this pointer give me? Eg: #include<stdio.h> main() { int var[10] = {1,2,3,4,5,6,7,8,9,10}; int (*ptr) [10] = &var; printf("value = %u %u\n",*ptr,ptr); //both print 2359104. Shouldn't *ptr print 1? } ...

jagged array in c

Is there such a thing as a jagged array in C or C++? When I compile this: int jagged[][] = { {0,1}, {1,2,3} }; I get this error: error: declaration of `jagged' as multidimensional array must have bounds for all dimensions except the first ...

Preventing buffer overflow in C/C++

Many times I have problems with Buffer Overflow. int y[10][10][10]; ... y[0][15][3] = 8; How can I prevent this problem? Is there any good tool that can help me? ...

C/C++ add input to stdin from the program ?

Is that even possible ? Lets say that the code has a lot of scanf lines. Instead of manually running and adding values by hand when debugging, is it possible to "feed" stdin with data so that when the scanf starts reading, it will read the inputted data without any need to interact with the terminal. ...

Did i set my file pointer wrong?

Did i set my file pointer wrong? ...

Regular expressions in C: examples?

Hello. I'm after some simple examples and best practices of how to use regular expressions in ANSI C. Man regex.h does not provide that much help. Thank you. ...

How do C/C++ compilers work?

After over a decade of C/C++ coding, I've noticed the following pattern - very good programmers tend to have detailed knowledge of the innards of the compiler. I'm a reasonably good programmer, and I have an ad-hoc collection of compiler "superstitions", so I'd like to reboot my knowledge and start from the basics. Can anyone recommen...

Is there a strtol equivalent that does not require a null-terminated string?

Hi, Is there a standard C function similar to strtol which will take a char* and a length for a non-null-terminated string? I know that I could copy out the string into a null-terminated region, but for efficiency reasons that is undesirable. Thanks. ...

Algorithms in C

Hi, Can anyone please suggest to me the best place or link to learn algorithms in C? How do you know when and where to use the implementation of algorithms by just looking into the problems. Thanks and regards, Maddy ...

Pointer to pointer confusion

Edit: The reason queue is 2d is because I need a pointer of Command so that cmd can equal NULL. NULL == (void *). This is where I get confused though, and why I've come here. :) To help try and figure out another problem I have in Python, I'm implementing a small test program in C. While I know a little, apparently I'm confused. I'm try...

String length between pointers

Hi, I ran into a little problem and need some help: If I have an allocated buffer of chars and I have a start and end points that are somewhere inside this buffer and I want the length between these two point, how can I find it? i.e char * buf; //malloc of 100 chars char * start; // some point in buff char * end; // some point after s...

How can I tell if a file is open elsewhere in C on Linux?

How can I tell if a file is open in C? I think the more technical question would be how can I retrieve the number of references to a existing file and determine with that info if it is safe to open. The idea I am implementing is a file queue. You dump some files, my code processes the files. I don't want to start processing until the pr...

Adding section to ELF file

I need to be able to add an arbitrary section to an ELF file. I cannot use GPL code in this program, so BFD is out of the question. I can use libelf/gelf to read sections, but the documentation is fairly sparse for these, and I cannot figure out how to add a section. Does anybody know how to do this? I would rather not write my own ELF c...

How do I create an array of strings in C?

I am trying to create an array of strings in C. If I use this code: char (*a[2])[14]; a[0]="blah"; a[1]="hmm"; gcc gives me "warning: assignment from incompatible pointer type". What is the correct way to do this? edit: I am curious why this should give a compiler warning since if I do printf(a[1]);, it correctly prints "hmm". ...

Application receiving mysterious SIGINTs

We have a small daemon application written in C for a couple of various UNIX platforms (this problem is happening in SunOS 5.10), that basically just opens up a serial port and then listens for information to come in via said port. In this particular instance, the daemon appears to read a single transmission (like a file's worth of data...

Returning multiple values from a C function

Important: Please see this very much related question: Return multiple values in C++. I'm after how to do the same thing in ANSI C? Would you use a struct or pass the addresses of the params in the function? I'm after extremely efficient (fast) code (time and space), even at the cost of readability. EDIT: Thanks for all the answers. Ok...

Newbie Thread Question (FFTW)

I'm using the threaded version of FFTW (a FFT library) to try to speed up some code on a dual CPU machine. Here is the output of time w/ only 1 thread: 131.838u 1.979s 2:13.91 99.9% Here it is with 2 threads: 166.261u 30.392s 1:52.67 174.5% The user times and the CPU load percentages seem to indicate that it is threading pretty eff...

NSString newbie question about setting a char from it.

Ok, I know this is a very basic question, but my head is swimming in NSString vs C string nonsense. Basically I have a NSString the just contain 1 character for example the letter D How do I take that 1 character NSString and assign it to a variable of char So I have... char mychar; NSString *myMode = [[NSString alloc] initWithFo...