how to pass an array to a thread in c 3f

Solutions on MaxInterview for how to pass an array to a thread in c 3f by the best coders in the world

showing results for - "how to pass an array to a thread in c 3f"
Garance
03 Jul 2020
1void* my_Func(void *received_arr_Val){
2	
3	int single_val = (int *)received_arr_Val;
4    printf("Value: %d\n", single_val);
5	//Now use single_val as you wish
6}
7//In main:
8	int values[n];
9    pthread_create(&thread, NULL, my_Func, values[i]);
10	//i is the index number of the array value you want to send
11	//n is the total number of indexes you want (array size)
12
13//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Jannik
08 Jun 2019
1void* my_Func(void *received_arr){
2	
3	int *arr = (int *)received_arr;
4	
5	for (int i=0; i<5; i++){
6		printf("Value %d:  %d\n", i+1, arr[i]);
7	}
8	//Now use arr[] as you wish
9}
10//In main:
11	int values[n];
12	pthread_create(&thread, NULL, my_Func, (void *)values);
13
14//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Luis
13 May 2020
1//Runnable example
2#include <stdio.h>
3#include <stdlib.h>
4#include <pthread.h>
5
6void* my_Func(void *received_arr_Val){
7	
8	int single_val = (int *)received_arr_Val;
9    printf("Value: %d\n", single_val);
10	//Now use single_val as you wish
11}
12int main(){
13	int values[5];
14	
15	printf("\nEnter 5 numbers:\n");
16	for (int i=0; i<5; i++){
17		scanf("%d", &values[i]);
18	}
19	pthread_t tid[5];
20	for(int i=0; i<5; i++){
21		pthread_create(&(tid[i]), NULL, my_Func, values[i]);
22	}
23	for (int i=0; i<5; i++){	
24		pthread_join(tid[i], NULL);
25	}
26}
27//To run: $ gcc [C FILE].c -lpthread -lrt
28// $ ./a.out
29
30//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Eoin
27 May 2018
1//Runnable Example
2#include <stdio.h>
3#include <stdlib.h>
4#include <pthread.h>
5
6void* my_Func(void *received_arr){
7	
8	int *arr = (int *)received_arr;
9	
10	for (int i=0; i<5; i++){
11		printf("Value %d:  %d\n", i+1, arr[i]);
12	}
13	//Now use arr[] as you wish
14}
15int main(){
16	int values[5];
17	
18	printf("\nEnter 5 numbers:\n");
19	for (int i=0; i<5; i++){
20		scanf("%d", &values[i]);
21	}
22	pthread_t tid;
23	pthread_create(&tid, NULL, my_Func, (void *)values);
24	pthread_join(tid, NULL);
25}
26//To run: gcc [C FILE].c -lpthread -lrt
27//./a.out
28
29//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192