all,I want to implement array_map function using c language .
how can i do this?
void * x_array_map(void * func, Array * arr){
//TODO
}
Thx!
Here is some reference about function as parameter,
How do you pass a function as a parameter in C?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define param_type int
// prototype
param_type* array_map (param_type (*f)(param_type), param_type* arr, int n);
// dummy function
int cube(int x) {
return x*x*x;
}
int main (){
int a[3] = {1, 2, 3};
int* b = array_map(cube, a, 3);
for (int i = 0; i < 3; ++i) {
printf("%d\n", b[i]);
}
return 0;
}
param_type* array_map (param_type (*f)(param_type), param_type* arr, int n) {
param_type* result = (param_type*) malloc(n*sizeof(param_type));
for (int i = 0; i < n; ++i)
result[i] = f(arr[i]);
return result;
}
Related
I have a problem returning dynamic array pointer with function parameter. I get segfault
#include <stdio.h>
#include <stdlib.h>
void createArray(int *ptr, int n)
{
ptr = malloc(n * sizeof(int));
for(int i = 1; i <= n; ++i)
{
*(ptr + (i - 1)) = i*i;
}
}
int main() {
int *array = NULL;
int n = 5;
createArray(array, n);
for(int i = 0; i < n; ++i)
{
printf("%d", array[i]);
}
return 0;
}
I have to fill my array with i*i, when I is from 1 to n.
I don't get any errors or warnings. Just message about segmentation fault. Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Memory must be allocate in the calling function, but not in called.
This variant works:
#include <stdio.h>
#include <stdlib.h>
void createArray(int *ptr, int n){
int i;
for(i = 1; i <= n; i++) {
*(ptr + (i - 1)) = i*i;
// fprintf(stdout,"%d %d\n", i, *(ptr + (i -1)));fflush(stdout);
}
}
int main() {
int i, n, *array = NULL;
void *pvc;
n = 5;
array = (int *)malloc(n * sizeof(int));
createArray(array, n);
for(i = 0; i < n; i++) {
fprintf(stdout,"%d %d\n", i, array[i]);fflush(stdout);
}
pvc = (void *)array;
free(pvc);
return 0;
}
You can change pointer through function parameters like this:
void createArray(int **ptr, int n)
{
*ptr = malloc(n * sizeof(int));
for(int i = 1; i <= n; ++i)
{
(*ptr)[i - 1] = i*i;
}
}
int main() {
int *array = NULL;
int n = 5;
createArray(&array, n);
Remember to call function like this: createArray(&array, n);
Here's my code:
#include <stdio.h>
int func(int a[]);
int main()
{
int a[5] = {1, 2, 3, 4, 5};
a[] = func(a);
What changes should I make on the above line to get the new values in the array ?
for (i = 0; i < 5; i++)
{
printf("%d ", a[i]);
}
}
int func(int a[])
{
for (i = 0; i < 5; i++)
{
a[i] = a[i] + 1;
}
return a;
}
Thanks in advance!
When you pass an array as an argument of a function, it is not a copy, in fact that argument decays to a pointer to the first element of the passed array, using int func(int a[]); is basically the same as using int func(int *a);, any changes made to the array inside the function will be permanent.
What changes should I make on the above line to get the new values in the array?
Given the above explanation, your function doesn't need to return a:
void func(int a[]) // as no return is needed, the return type should be void
{
for (int i = 0; i < 5; i++)
{
a[i] = a[i] + 1;
}
// no need to return a, it's permanently changed already
}
Consequently the assignment to a in main needs to be replaced by a simple function call:
int main()
{
int a[5] = {1, 2, 3, 4, 5};
func(a); //call the function
for (int i = 0; i < 5; i++)
{
printf("%d ", a[i]);
}
}
The output will be:
2 3 4 5 6
I am having trouble figuring out how to allocate memory for an array of pointers in a function. In this same function I am trying to initialize the arrays with values from another array. I have been trying different things for a while and I cannot figure out where I do and do not need.
#include <stdio.h>
#include <stdlib.h>
void allocate();
void print();
int main() {
int array_length = 10;
int array[array_length] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int **ascending;
int **descending;
allocate(&ascending, &descending, array, array_length);
print(&ascending, &descending, array, array_length);
}
void allocate(int ***ascending, int ***descending, int array[], int array_length) {
*ascending = (int **)malloc(array_length * sizeof(int *));
*descending = (int **)malloc(array_length * sizeof(int *));
int i, first_index = 0;
for (i = 0; i < array_length; i++) {
(*ascending)[i] = &(array[i]);
(*descending)[i] = &(array[i]);
}
}
void print(int **ascending, int **descending, int array[], int array_length) {
int i;
printf("\nAscending\tOriginal\tDescending\n\n");
for (i = 0; i < array_length; i++) {
printf("%d\t\t", ascending[i]);
printf("%d\t\t", array[i]);
printf("%d\t\t", descending[i]);
printf("\n");
}
printf("\n");
}
First of all, variable-size arrays cannot be initialized. You should use a MACRO for array_length.
Then, as per your function definition, the call to print() needs int ** as first two arguments, not int ***. Change the function call to
print(ascending, descending, array, array_length);
also, ascending[i] and descending[i], in this case, are of type int *, you need one more level of dereference to get the int.
That said,
void allocate();
void print();
are bad forward declarations. You should be using the exact signature of the functions for declaration and definition.
A sample working version may look like something
//gcc 4.9.3
#include <stdio.h>
#include <stdlib.h>
#define arraysize 10
void allocate(int ***ascending, int ***descending, int array[], int array_length);
void print(int **ascending, int **descending, int array[], int array_length);
int main(void) {
int array_length = arraysize;
int array[arraysize] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int **ascending;
int **descending;
allocate(&ascending, &descending, array, array_length);
print(ascending, descending, array, array_length);
return 0;
}
void allocate(int ***ascending, int ***descending, int array[], int array_length) {
*ascending = (int **)malloc(array_length * sizeof(int *));
*descending = (int **)malloc(array_length * sizeof(int *));
int i = 0;//, first_index = 0;
for (i = 0; i < array_length; i++) {
(*ascending)[i] = &(array[i]);
(*descending)[i] = &(array[i]);
}
}
void print(int **ascending, int **descending, int array[], int array_length) {
int i;
printf("\nAscending\tOriginal\tDescending\n\n");
for (i = 0; i < array_length; i++) {
printf("%d\t\t", *(ascending[i]));
printf("%d\t\t", array[i]);
printf("%d\t\t", *(descending[i]));
printf("\n");
}
printf("\n");
}
I'm having some issues with very simple situations of passing arrays as pointers into functions and returning them. I thought I had pointers figured but I just can't get my head around it.
Here's the code:
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
system("PAUSE");
return 0;
}
int* getLottoDraw(){
int draw[6];
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}
One example output is "3 2047 4614546 0 25 45". Not what was hoping for.
You are returning a stack address, which end up being destroyed when the function ends.
Stack variables are local variables, their scope is limited to the function they're created.
They're created on the function, and destroyed when the function ends, so if you've try to access this address later you'll get undefined behavior.
You should have a dynamic allocated pointer to be able to access it outside the function, or return by value, copying the content (which can be costly in an array case).
You could do something like:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
free(lotteryDraw);
return 0;
}
int* getLottoDraw(){
int* draw = malloc(sizeof(int)*6);
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}
I want to pass two matrices as argument. These matrices have different size and i don't understand how i have to do this work:
#include <stdio.h>
#include <stdlib.h>
void f(int m[3][], int n);
int main()
{
int A[3][3]={{1,2,3},{4,5, 6},{7,8,9}};
int B[3][2]={{1,2},{3, 4}, {5, 6}};
f(A, 3);
f(B, 2);
return 0;
}
void f(int m[3][], int n)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<n;j++)
printf("%5d", m[i][j]);
}
return;
}
How can I do this?
The only safe way that I know of to do this is to include the matrix dimensions in the parameters, or make some kind of matrix struct
Option A) dimensions as parameters
void f(int **m, int w, int h )
{
int i,j;
for(i=0;i<w;i++)
{
for(j=0;j<h;j++)
printf("%5d", m[i][j]);
}
return;
}
Option B) Use a struct
typedef struct Matrix
{
int w, h;
int** m;
} Matrix;
void f ( Matrix *m )
{
for ( int i = 0; i < m->w; ++i )
{
for ( int j = 0; j < m->h; ++j )
{
printf(%5d", m->m[i][j]);
}
}
}