I have written a function to slice an array in c but it returns addresses I think. I did work correctly once but I screwed up somewhere, any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
int slice_array(int *input_array, int *sliced_array, int n){
int i;
for(i=0; i < n; i++) {
sliced_array[i] = input_array[i];
return 0;
}
};
struct nbrs_ind {
float value;
int index;
};
//I also want to write a function which can slice the members of struct nbrs_ind (i.e. just slice first n indices nbrs_ind)
int main () {
int k=4;
int i;
int a[7] = {1,2,3,4,6,5,7};
int *ptr_a = a;
int b[k];
int *ptr_b = b;
slice_array(a,b,k);
for(i=0;i<k;i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
~
Update, slicing the array works fine but I want to so the same with struct so far I have written the following code. I get he following error:
assignment from incompatible pointer type [enabled by default]
ptr_A = &A;
#include <stdio.h>
#include <stdlib.h>
//This function works fine in drivers program
void slice_array(int *input_array, int *sliced_array, int n){
int i;
for(i=0; i < n; i++) {
sliced_array[i] = input_array[i];
}
};
struct nbrs_ind {
float value;
int index;
};
//Need to make the slice of nbrs_ind struct using following function.
void slice_struct(struct nbrs_ind *input_struct, struct nbrs_ind *sliced_struct, int n){
int i;
for(i=0; i < n; i++) {
sliced_struct[i].index = input_struct[i].index;
sliced_struct[i].value = input_struct[i].value;
}
};
int main () {
int k=3;
int i;
int a[7] = {1,2,3,4,6,5,7};
float c[7] = {2.3,10,3,5,6.4,7.3,1};
int *ptr_a = a;
int b[k];
int *ptr_b = b;
struct nbrs_ind A[7]; // Declare 7 struct of nbrs_ind
//Initilize the delare structs
for (i=0;i<7;i++){
A[i].index = i;
A[i].value = c[i];
}
//How do I make a pointer to the struct so I can pass it to slice_struct function
// I need to be able to do something like slice_struct(A,B,n);
struct nbrs_ind *ptr_A ;
ptr_A = &A;
slice_array(a,b,k);
for(i=0;i<k;i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
~
~
~
#include <stdio.h>
#include <stdlib.h>
int slice_array(int *input_array, int *sliced_array, int n){
int i;
for(i=0; i < n; i++) {
sliced_array[i] = input_array[i];
}
return 0;
};
struct nbrs_ind {
float value;
int index;
};
//I also want to write a function which can slice the members of struct nbrs_ind (i.e. just slice first n indices nbrs_ind)
int main () {
int k=4;
int i;
int a[7] = {1,2,3,4,6,5,7};
int *ptr_a = a;
int b[k];
int *ptr_b = b;
slice_array(a,b,k);
for(i=0;i<k;i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
Related
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct a{
int length;
};
static int a2(int a[]){
int y = 0;
int x = 0;
for (int i=0; i<a.length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a));
return 0;
}
when I run this code I receive the following error "error: request for member 'length' in something, not a structure or union" can anyone help me to understand the error and how to rectify the code? Thanks
The name of structure and the name of variables are not related.
The argument a is a pointer (int a[] in function arguments has the same meaning as int* a) and it doesn't have members.
You have to pass the length of array to pass to functions aside from (the pointer to the first element of) the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int a2(int a[], int a_length){
int y = 0;
int x = 0;
for (int i=0; i<a_length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a, sizeof(a) / sizeof(*a)));
return 0;
}
Suppose I have this piece of code, with 2 structs and a big function that receives this 2 structs as parameters:
typedef struct
{
int field_A;
int field2_A;
} A;
typedef struct
{
int field_B;
int field2_B;
} B;
void function_need_refactor(A *a, B *b)
{
for(i = 0; i < SIZE; i++)
{
do_something(a->field_A);
}
for(i = 0; i < SIZE; i++)
{
do_something(b->field2_B);
}
}
How can I replace the both for loops to a single function? I thought about using void pointer and an identifier for each struct, but couldn't come up with an answer. Is there a clean way to refactor this or is it impossible?
void refactored_function(void* my_struct, char type_identifier)
{
//code to identify the type
for(i=0; i < SIZE; i++)
{
do_something((cast)my_struct->????);
}
}
There are two modifications that come to mind here. First of all, both loops rely on the same counter and the counter is independent of the items in the loop body... Thus, logically, you should at least be able to do this:
typedef struct
{
int field_A;
int field2_A;
} A;
typedef struct
{
int field_B;
int field2_B;
} B;
void function_need_refactor(A *a, B *b)
{
for(i = 0; i < SIZE; i++)
{
do_something(a->field_A);
do_something(b->field2_B);
}
}
But you may also be able to refactor do_something() to operate on both:
typedef struct
{
int field_A;
int field2_A;
} A;
typedef struct
{
int field_B;
int field2_B;
} B;
void function_need_refactor(A *a, B *b)
{
for(i = 0; i < SIZE; i++)
{
do_something(a->field_A, b->field2_B);
}
}
That way, you're only using one loop total, and the function is handling both fields on each loop iteration.
my C question is how to properly take the binary array I get in binaryConversion() and copy it to integer.binary, within makeInt().
I tried to make binaryConversion() return a pointer which then would be dereferenced and copied over to the new array, but I am almost certain this is wrong in some way.
This is my failed attempt:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
struct Integer{
int decimal;
int binary[32];
};
int * binaryConversion(int num){
int bin_buffer[32];
int * ptr = bin_buffer;
unsigned int mask = INT_MIN;
for(int i = 0; i < 32; i++){
if(mask & num == 1){
bin_buffer[i] = 1;
}
else{
bin_buffer[i] = 0;
}
mask >>= 1;
}
return ptr;
}
struct Integer makeInt(int num){
struct Integer integer;
int * ptr = binaryConversion(num);
for(int i = 0; i < 32; i++){
integer.binary[i] = *(ptr+i);
}
}
and it must work with this driver code:
struct Integer makeInt(int);
void binaryTester(int array[], int test[], int size){
int i;
for(i = 0; i < size; i++){
assert(array[i] == test[i]);
}
printf("\n");
}
int main(){
int size = sizeof(int) * 8;
int array[size], i;
struct Integer test;
printf("\n\t=========Test #7: Conversion of 2===========\n\n");
test = makeInt(2);
int test1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
printf("\n\t\t....Converting 2 Passed\n");
}
Thanks
This code is similar to what I am attempting to do, however I am getting errors saying that I am passing incompatable types
#include <stdio.h>
struct numbers{
int num;
};
void fillArray(struct numbers* a[]);
int main(void)
{
struct numbers array[4];
fillArray(&array);
for(int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
}
void fillArray(struct numbers* a[])
{
for(int i = 0; i < 4; i++)
{
a[i]->num = i;
}
}
The function parameter
void fillArray(struct numbers* a[]);
is adjusted to
void fillArray(struct numbers ** a);
On the other hand the type of the argument in this call
fillArray(&array);
is struct numbers( * )[4]. The types struct numbers ** and struct numbers ( * )[4] are incompatible.
There is no need to pass a pointer to the array because elements of the array are already passed indirectly if you will pass just the array that is implicitly converted to pointer to its first element.
So what you need is to declare and define the function like
void fillArray( struct numbers a[] )
// or
// void fillArray( struct numbers *a )
{
for(int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
and call it like
fillArray( array );
Take into account that the function depends on magic number 4. It is better to define the function such a way that it could deal with arrays of various numbers of elements.
So I would define the function like
void fillArray( struct numbers a[], size_t n )
// or
// void fillArray( struct numbers *a, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
a[i].num = i;
}
}
and call it like
fillArray( array, 4 );
Here is demonstrated how the program can look in whole
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray( struct numbers a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
a[i].num = i;
}
}
#define N 4
int main(void)
{
struct numbers array[N];
fillArray( array, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", array[i].num );
}
putchar( '\n' );
return 0;
}
Its output is
0 1 2 3
To use an array with a different number of elements it is enough to change the value of the macro name N. Thus the program and its function do not depend on the magic number 4.
You probably want this:
#include <stdio.h>
struct numbers {
int num;
};
void fillArray(struct numbers a[]);
int main(void)
{
struct numbers array[4];
fillArray(array);
for (int i = 0; i < 4; i++)
{
printf("%d\n", array[i].num);
}
}
void fillArray(struct numbers a[])
{
for (int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
Your function wants an array of pointers, but you have a single array, so
void fillArray(struct numbers* a);
or
void fillArray(struct numbers a[]);
Moreover using a[i] you are already dereferencing the pointer, so you need . not -> operator.
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(struct numbers *a);
int main(void)
{
struct numbers array[4];
fillArray(array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(struct numbers *a)
{
for (int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
Finally using c array decays to pointer to first item of array, so
fillArray(&array);
must be
fillArray(array);
or
fillArray(&array[0]);
Lastly you should pass size of array to your function, instead of using fixed numbers. You can do it using pointer and size
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(size_t size, struct numbers *a);
int main(void)
{
struct numbers array[4];
fillArray(sizeof(array)/sizeof(array[0]), array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(size_t size, struct numbers *a)
{
for (size_t i = 0; i < size; i++)
{
a[i].num = i;
}
}
Or using VLAs
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(size_t size, struct numbers a[size]);
int main(void)
{
struct numbers array[4];
fillArray(sizeof(array)/sizeof(array[0]), array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(size_t size, struct numbers a[size])
{
for (size_t i = 0; i < size; i++)
{
a[i].num = i;
}
}
It is sufficient to only mention the array name. The compiler will transform it into a pointer to the first element.
So instead of
fillArray(&array);
you write
fillArray(array);
Though this answers your question, of course the function being called must have a compatible definition, which in your case is not so.
I am trying to access a structure with a pointer to an integer , from main. But the program crashes. It needs to be built with "std=c99" option as it is the requirement in a test.
The code is as follows:
#include <stdio.h>
#include <malloc.h>
struct Results{
int *A;
int N;
};
struct Results solution(int A[], int N, int K) {
struct Results result;
// write your code in C99 (gcc 4.8.2)
int* T = (int*) malloc(N*sizeof(int));
result.A = A;
result.N = N;
int count = 0;
while(count < K)
{
for(int i = 0; i < N; i++)
{
if(i > 0)
{
T[i] = A[i-1];
}
else
{
T[0] = A[N-1];
}
}
count++;
for(int i = 0; i < N; i++)
{
A[i] = T[i];
}
};
for(int i = 0;i < N; i++)
{
A[i] = T[i];
}
return result;
}
struct Results solution(int A[], int N, int K);
void main()
{
int B[5] = {3,8,9,7,6};
struct Results st;
solution(B,sizeof(B),1);
}
The trouble is at line:
" solution(B,sizeof(B),1);"
What am I doing wrong?
Please help.
You see sizeof(B) would give the number of elements in B times the size of an int, use sizeof(B) / sizeof(B[0]) instead.