c

Why don't more C programs embed Perl?

I know there is a way to call Perl routines from C. As shown here http://search.cpan.org/dist/perl/pod/perlcall.pod#NAME But, still I do not see a widespread use of this by C programmers. Has any one used this ...ever? or any idea what are the reasons that it is not used so much? ...

Wrapping a C Library with Objective-C - Function Pointers

Hey guys, I'm writing a wrapper around a C library in Objective-C. The library allows me to register callback functions when certain events occur. The register_callback_handler() function takes a function pointer as one of the parameters. My question to you gurus of programming is this: How can I represent an Objective-C method call /...

C Static Array Initialization - how verbose do I need to be?

To initialize an int array with all zeros, do I need to use: int foo[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; Or, will this work: int foo[10] = {0}; ...

Why does start_routine for pthread_create return void* and take void*

The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); I understand it all except that the function pointer for start_routine is of the form void* (fpointer) (void...

Pointer Implementation Details in C

I would like to know architectures which violate the assumptions I've listed below. Also I would like to know if any of the assumptions are false for all architectures (i.e. if any of them are just completely wrong). sizeof(int *) == sizeof(char *) == sizeof(void *) == sizeof(func_ptr *) The in-memory representation of all pointers fo...

Multiple arguments to function called by pthread_create()?

I need to pass multiple arguments to a function that I would like to call on a separate thread. I've read that the typical way to do this is to define a struct, pass the function a pointer to that, and dereference it for the arguments. However, I am unable to get this to work: #include <stdio.h> #include <pthread.h> struct arg_struct {...

Preprocessor examples in C language

I want some examples of C preprocessor directives, such as: #define pi 3.14 #define MAX 100 I know only this. I want to know more than this, more about preprocessor directives. ...

Reflection Support in C

I know it is not supported, but I am wondering if there are any tricks around it? Any tips? Thank you ...

How to increase/decrease an Unsigned Char?

I am trying to modify pixel values (8 bits per channel RGBA) by numerically increasing/decreasing the values by a certain amount. How can I do this in Objective-C or C? The following code generates a "Error: EXC_BAD_ACCESS" everytime. // Try to Increase RED by 50 for(int i = 0; i < myLength; i += 4) { //NSLog prints the values...

Time complexity of the program

#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf("%ld",&n); n=100000; j=2; start=clock(); printf("\n%ld",j); for(j=3;j<=n;j+=2) { for(i=3;i*i<=j;i+=2) if(j%i==0) break; if(i*i>j) prin...

Autocorrelation returns random results with mic input (using a high pass filter)

Hello, Sorry to ask a similar question to the one i asked before (FFT Problem (Returns random results)), but i've looked up pitch detection and autocorrelation and have found some code for pitch detection using autocorrelation. Im trying to do pitch detection of a users singing. Problem is, it keeps returning random results. I've got s...

Var-Args: Last named parameter not function or array?

This question is about vararg functions, and the last named parameter of them, before the ellipsis: void f(Type paramN, ...) { va_list ap; va_start(ap, paramN); va_end(ap); } I was reading in the C Standard, and found the following restriction for the va_start macro: The parameter parmN is the identifier of the rightmost para...

Need help in making this star program using for loop in C

I am a beginner in C and want to create a program using for loop which gives the following output: * * * * * * * * * * * * * * * * * * * ...

Is there a defined evaluation order for &= and |= ?

If you have a C function which returns an integer, you could write a statement like this: MyInt &= MyFunc(); ...where we're using the bitwise-AND assignment operator. The question is: is MyFunc() guaranteed to be executed, even if MyInt equals zero? Likwise, if we used the bitwise-OR assignment operator (|=), would MyFunc() always b...

dlsym/dlopen with runtime arguments

Hello, I am trying to do something like the following enum types {None, Bool, Short, Char, Integer, Double, Long, Ptr}; int main(int argc, char ** args) { enum types params[10] = {0}; void* triangle = dlopen("./foo.so", RTLD_LAZY); void * fun = dlsym(triangle, ars[1]); <<pseudo code>> } Where pseudo code is ...

Getting an address using both square brackets and ampersand

Consider: int a[2] = {0,1}; int *address_of_second = (&a[1]); I assume this works because it's translated to &*(a+1) and then the & and * cancel each other out, but can I count on it, or is it compiler-specific? That is, does the C standard have anything to say about this? Is this a decent way to write? Personally I think that writi...

How can I see the assembly code that is generated by a gcc (any flavor) compiler for a C/C++ program?

I am trying to optimize a lot of multiplications and pointer arithmetics and would like to see what the compiler does underneath when I put in optimization flags. --Edit-- How to restrict it to a specific function or a code block? --Edit_2-- How to let gcc generate a less verbose assembly-code? ...

Can a C function be used as a selector in Cocoa?

I want to start a new thread using a C function, not an objective-C method. I tried [NSThread detachNewThreadSelector: @selector(func) toTarget: nil withObject: id(data)]; where I have void func(void *data) { // ... } and data is a void *, but I get a runtime crash in objc_msgSend, called from -[NSThread initWithTarget:selector...

is there a simple way (macro?) to tell structure alignment?

I understand the structure alignment is 'implementation specific', but just wondering if there is any simple way to calculate the structure alignment, for example: typedef struct { char c; int i; } test; if sizeof(test) - (sizeof(char) + sizeof(int)) == 0 means alignment is 1 byte; if sizeof(test) - (sizeof(char) + sizeof(int...

How best to handle large buffers in a layered protocol stack?

Hi, I am working on a simple protocol stack for a small embedded system (multidrop, rs485 type stuff). In this stack, losely models after OSI layers: Application Network Datalink physical (serial driver) Each layer has its own header / footer portion that wraps the payload of the layer above it. I will be using my own buffer pool ...