how to pass an array as an argument to a function in c code example
Example 1: how to pass an array value to a pthread in c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* my_Func(void *received_arr_Val){
int single_val = (int *)received_arr_Val;
printf("Value: %d\n", single_val);
}
int main(){
int values[5];
printf("\nEnter 5 numbers:\n");
for (int i=0; i<5; i++){
scanf("%d", &values[i]);
}
pthread_t tid[5];
for(int i=0; i<5; i++){
pthread_create(&(tid[i]), NULL, my_Func, values[i]);
}
for (int i=0; i<5; i++){
pthread_join(tid[i], NULL);
}
}
Example 2: how to accept an array as function parameter in c
#include <stdio.h>
void displayNumbers(int num[2][2]);
int main()
{
int num[2][2];
printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
scanf("%d", &num[i][j]);
displayNumbers(num);
return 0;
}
void displayNumbers(int num[2][2])
{
printf("Displaying:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d\n", num[i][j]);
}
}
}
Example 3: c function with array parameter
Pass array as function parameters