array argument in c pthread code example

Example 1: how to pass an array value to a pthread in c

void* my_Func(void *received_arr_Val){
	
	int single_val = (int *)received_arr_Val;
    printf("Value: %d\n", single_val);
	//Now use single_val as you wish
}
//In main:
	int values[n];
    pthread_create(&thread, NULL, my_Func, values[i]);
	//i is the index number of the array value you want to send
	//n is the total number of indexes you want (array size)

//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192

Example 2: 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:  %d\n", 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

Tags:

C Example