how to pass an array to a thread 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

4
2
Macbrush 90 points

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

4 (2 Votes)
0
0
0

                                    //Runnable example
#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);
	//Now use single_val 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[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);
	}
}
//To run: $ gcc [C FILE].c -lpthread -lrt
// $ ./a.out

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

0
0
4.4
5
PixelPower 95 points

                                    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

4.4 (5 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source