2

i have the following code

 int arr[5];
        printf("Input values:");
        for (i=0;i<5;i++)
        scanf("%d",&arr[i]);
        pthread_create(&thread1, NULL, &inputfunction, (void *)&arr);
        pthread_join(thread1,NULL);
        return 0;
}

void *inputfunction(void *ptr_value)
{
        int value= *((int *) ptr_value);
        printf("value=%d", value);
//      printf(&(ptr_value));
        return NULL;
}

I want to retrieve all the 5 values I have entered in the array but using this code in the body of the function returns just the first value. I am very confused with pointers and am not able to figure out the way to get the entire array.

please tell me what is the modification I need to make in my code.

Thanks

0

1 Answer 1

3

Try this:

pthread_create(&thread1, NULL, &inputfunction, &(arr[0]));

void *inputfunction(void *ptr_value)
{
    int *values = ptr_value;

    for (int i = 0; i < 5; i++)
         printf("value %i = %i\n", i, values[i]);

    return NULL;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Should cast ptr_value to int* before assigning to values, just to be anal about it.
@ScottHunter: That's unnecessary in C.
@RichaSachdev it's not necessary, but makes the intent clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.