Finding the interval between two pointers to a tab in c [closed] - c

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);
}

Related

arm neon instruction [closed]

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.

How to write a replace function in c [closed]

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;
}

Swapping first and last digit in an integer using bitwise operator [closed]

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 have been asked in an interview to swap the last and first digit in an integer using bitwise operators. Tried a lot but I could not find the solution. How can I do this?
Use int digits = log10(x) to get the number of digits.
Use int first = x / pow(10,digits) to get the first digit.
Use int last = x % 10 to get the last digit.
Put it all together and you have
int swapped = x + (last - first) * pow(10,digits) + (first - last)
A trivial solution:
def swap_digit(n):
x = str(n)
if len(x) < 2:
return x
return int(x[-1] + x[1:-1] + x[0])
EDIT: Added a quick and dirty C solution
#include <stdio.h>
#include <string.h>
int main()
{
int n = 123456789;
char buf[100];
int r = snprintf(buf, sizeof(buf), "%d", n);
char t = buf[0];
buf[0] = buf[r-1];
buf[r-1] = t;
int swap;
sscanf(buf, "%d", &swap);
printf("n = %d, swap = %d\n", n, swap);
return 0;
}
def swap(i):
s = list(str(i))
s[0], s[-1] = s[-1], s[0]
i = int(''.join(s))
return i
print swap(123456789) # 923456781

[C]--binary function [closed]

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.

function within a function in c [closed]

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 );
}

Resources