It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In the function doubleadd i want the result as the summation of x and function add
#include <stdio.h>
int add(int a,int b)
{ return (a+b); }
int doubleadd(int x,int y=(*add)(int a,int b))
{ return (x+y); }
void main()
{
void (*ptr)(int,int);
ptr=add;
int y=ptr(5,7);
printf("%d",y);
y=doubleadd(3,ptr(5,7));
}
please help me with this problem
You don't need to do all that mess! This will be fine:
y = doubleadd(3, add(5,7));
and the prototype of doubleadd is
int doubleadd(int x, int y) { ... }
you can pass complex expressions as parameters, too
In the unlikely event you want to turn your C code into some bastard child of a functional language:
#include <stdio.h>
typedef int (*addfun)( int a, int b );
int add(int a,int b) {
return a + b;
}
int doubleadd(int x, addfun f, int a, int b ) {
return x + f( a, b );
}
int main() {
addfun fn = add;
int y = doubleadd(3, fn, 5, 7 );
printf( "%d\n", y );
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have some code and, I want to use Neon instruction to change it, but I really don't know how to complete it... Can anyone help me?
void add(int n,float *a,float *b,float t) {
int i, size = (n+2) * (n+2);
for(i = 0; i < size; i++)
a[i] += t * b[i];
}
By using NEON intrinsics.. something like this:
void add(int n,float *a,float *b,float t) {
int i, size = (n+2) * (n+2);
float32x4_t temptt = vdupq_n_f32(t);
for(i = 0; i < size; i+=4) {
float32x4_t temp1 = vld1q_f32(a+i);
float32x4_t temp2 = vld1q_f32(b+i);
temp1 = vmlaq_f32(temp2, tempt, temp1);
vst1q_f32(a + i, temp1);
}
}
This does 4 iterations at once, and would only work when size is a multiple of 4.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I decided to rewrite what I was doing going backwards to basics:
#include <stdio.h>
int main () {
int a;
int b;
int c;
int d;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
I call it me.c and I compile it with gcc me.c and run it with ./a.out.
I get this as a result:
hc$ ./a.out
ta-dah: 32767 1477090280 0 0
This is a very simple program, and I am not sure where the error is. Any suggestions?
You dont initialize the values, so it prints garbage values (whatever happens to be on the stack memory at that point in (memory)spacetime.
You should initialize them first, if you want to be able to read from them.
int a = 3;
int b = 42;
int c = 1337;
int d = 0;
Example
Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.
Either initialize the variables:
#include <stdio.h>
int main () {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
Output: ta-dah: 1 2 3 4
Or set them to static:
#include <stdio.h>
int main () {
static int a;
static int b;
static int c;
static int d;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
Output: ta-dah: 0 0 0 0
Unless you initialize the variables with values you will see whatever value happens to be at the memory address used to store the variable's value.
Change it to:
#include <stdio.h>
int main () {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
And you will get:
ta-dah: 1 2 3 4
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'd like to know how many element I have between two pointers to a tab, with a function.
For example in this code, nb would be 20.
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(&t[10], &t[30]);
return (0);
}
Have you got a idea?
Try
int finding_number_element(int *a, int *b)
{
return (b - a);
}
if a and b will be pointers to the same array this should work.
If you need something that doesn't care about the data type of t
#define ele_size(x) sizeof(x[0])
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(ele_size(t),&t[10], &t[30]);
return (0);
}
int finding_number_element(unsigned int ui_ele_size, void *a, void *b)
{
unsigned int ui_pointer1 = (unsigned int)a;
unsigned int ui_pointer2 = (unsigned int)b;
return ((b - a)/ui_ele_size);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Write a function that will return a value in which i-th byte of x has been replaced by b:
unsigned replace_f (unsigned x, int i, unsigned char b){
}
EX: replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678
unsigned replace_f (unsigned x, int i, unsigned char b){
unsigned char *place = (unsigned char*)&x;
place[sizeof(int)-i] = b;
return x;
}
Assume little endian
Didn't try out, but this could work:
unsigned replace_f (unsigned x, int i, unsigned char b){
char *c;
c = (char *)&x;
c[i] = b;
return x;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working in a small program using Language C.
I want to create a function which transform a decimal number to binary.
So i Have that:
#include< stdio.h>
int *binaire(int nb,int base)
{
int d,i=0,j;
int *tab= (int*) malloc(sizeof(int));
while(nb>0){
tab[i]=(nb%base);//inverser les bits
nb=nb/base;
i++;
}
return tab;
}
please Help
Thxs
sizeof returns the size in bytes, not in bits. May be you are looking for this:
int *tab= (int*) malloc(sizeof(int) * 8);
EDIT: As pointed in the comments, there are other problems here. Please check them too.
EDIT2: The previous code contains a bug in calculation. I am assuming that you are trying to store the bits in an int array. So the array will contain sizeof(int) * 8 integers, all either zero or one. So the code will be:
int arraySize = sizeof(int) * 8;
int *tab = (int *) malloc(arraySize * sizeof(int));
In fact you don't need to use full int to store a single bit. One byte character is enough to store a bit.
Somethign along those lines:
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef struct value_
{
int* digits;
int nb_digits;
} value;
value* base_convert(int nb,int base) ;
value* make_value(int nb, int base)
{
value* v = malloc(sizeof(value));
if(v)
{
v->nb_digits = 1+round(log(nb)/log(base));
v->digits = malloc(sizeof(int)*v->nb_digits);
if(v->digits) base_convert(v, nb, base);
}
return v;
}
void destroy_value(value* v)
{
if(v->digits) free(v->digits);
free(v);
}
void base_convert(value* v, int nb,int base)
{
int i;
for(i=0;i<v->nb_digits;++i)
{
v->digits[v->nb_digits-i-1]=(nb%base);//inverser les bits
nb=nb/base;
}
return v;
}
int main()
{
int i;
value* v = make_value(137,2);
for(i=0;i<v->nb_digits;++i) printf("%d",v->digits[i]);
destroy_value(v);
return 0;
}
This needs :
a get_digit function to mask the rough access to value::digits
Number of digits is computed using log to be generic. The abse 2 version can be computed in a faster way using bitshifts.