How to retrieve array values in c from another function - arrays

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

Related

Passing a 2d array to a function if I don't know the dimensions

I am trying to write a C function to add two arrays. The function should work with any array sizes and it should receive a reference to both arrays and the number of rows and the number of columns and it should return a pointer to the first element of the resulting array.
How would I do that? When I try to pass a two dimensional array to a function I get an error?
#include<stdio.h>
void function(int r, int c,int a[][]){
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d, ",a[i][j]);
}
printf("\n");
}
}
int main(){
int array[2][2] = {{1,2},{4,5}};
function(2,2,array);
return 0;
}
Assuming C99, or C11 with an implementation that doesn't define __STDC_NO_VLA__, you could use the variable length array (VLA) notation and could write:
void function(int r, int c, int a[r][c])
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
printf("%d, ", a[i][j]);
putchar('\n');
}
}
Or something equivalent to that. The dimensions must be defined before they're used in the array specification.
If you don't have access to even a C99 compiler but only a C90 compiler, then you have to pass a pointer to the first element of the array and the sizes and you perform the array index calculation explicitly.
void function(int r, int c, int *a)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
printf("%d, ", a[i * c + j]);
putchar('\n');
}
}
Now you call the function like this:
int main(void)
{
int array[2][2] = { { 1, 2 }, { 4, 5 } };
function(2, 2, &array[0][0]);
return 0;
}

How to allocate memory and assign values in a function for an array of pointers?

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

how to implement (PHP Function)array_map funciton in c?

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

passing 2d array in c using pointer to pointer by typecasting

How to pass a 2D array as a parameter in C?
I was searching to pass a 2d array to a function in c and I came across the above site. I understood the first and second way of passing 2d array, but I got
confused in the 3rd method, specifically, how is it even working that way?`
3) Using an array of pointers or double pointer
In this method also, we must typecast the 2D array when passing to function.
#include <stdio.h>
// Same as "void print(int **arr, int m, int n)"
void print(int *arr[], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3;
int n = 3;
print((int **)arr, m, n);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
`
The above code works fine on codeblocks.
When calling print() from main(), we pass arr as argument by typecasting it to pointer to pointer , but in the function print() it dereferences only once to print the values. printf("%d ", *((arr+i*n) + j));
Shouldn't it be *((*arr+i*n) + j));, I tried compiling this statement , it compiles but doesn't execute.
2) Using a single pointer
In this method, we must typecast the 2D array when passing to function.
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print((int *)arr, m, n);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
`
The 2nd method and the 3rd method only differ in the type of argument passed in the print() function while rest of the code is same. So what is actually the difference between the working of the functions?
easiest way to pass 2D array,
function
void PrintArray(unsigned char mat[][4]){
int i, j;
printf("\n");
for(i = 0;i<4;i++){
for(j = 0;j<4;j++)
printf("%3x",mat[i][j]);
printf("\n");
}
printf("\n");
}
main
int main(){
int i,j;
//static int c=175;
unsigned char state[4][4], key[4][4], expandedKey[176];
printf("enter the value to be decrypted");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%x",(unsigned int *)&state[j][i]);
PrintArray(state);
return 0;
}

why we must typecast the 2D array when passing to function using single pointer?

Hi i was trying to pass 2d array of integer using single pointer.I came to know that i must
typecast array before passing to the function
Can anyone please explain why we need to typecast before pass my code is below?
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print((int *)arr, m, n); // here why i need to typecast and pass?
return 0;
}
Because arr without the cast decays into int (*)[3] (pointer to a block of 3 ints) and your function is expecting int * (pointer to 1 int)..
Since there is no conversion between int (*)[3] and int*, you must employ a cast to force it.
Just pass a 2D array to your function :
void print(int a[][3], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", a[i][j]);
}
Generally, typecasting will change the properties of a variable. In main function, arr is a 2D array but print function treats arr as a pointer.So typecasting is required
Example for property change:
In main function, if you increment the arr + 1 12 bytes will get incremented, but in case of print function arr + 1 will increment only 4 bytes.

Resources