Answers for "how to pass array in thread in c"

C
3

how to pass an array to a thread in c?

void* my_Func(void *received_arr){
	
	int *arr = (int *)received_arr;
	
	for (int i=0; i<5; i++){
		printf("Value %d:  %dn", i+1, arr[i]);
	}
	//Now use arr[] as you wish
}
//In main:
	int values[n];
	pthread_create(&thread, NULL, my_Func, (void *)values);

//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Posted by: Guest on March-02-2020
0

how to pass an array to a thread in c?

//Runnable Example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void* my_Func(void *received_arr){
	
	int *arr = (int *)received_arr;
	
	for (int i=0; i<5; i++){
		printf("Value %d:  %dn", i+1, arr[i]);
	}
	//Now use arr[] as you wish
}
int main(){
	int values[5];
	
	printf("nEnter 5 numbers:n");
	for (int i=0; i<5; i++){
		scanf("%d", &values[i]);
	}
	pthread_t tid;
	pthread_create(&tid, NULL, my_Func, (void *)values);
	pthread_join(tid, NULL);
}
//To run: gcc [C FILE].c -lpthread -lrt
//./a.out

//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Posted by: Guest on March-02-2020

Code answers related to "how to pass array in thread in c"

Code answers related to "C"

Browse Popular Code Answers by Language