0

First, I'm not sure how to set an integer pointer to an array. Second, is this the proper way to set pthread_create arguments?

Here's my argument struct:

typedef struct args {
    int *arr;
    int number;
} args;

I created a pointer to the struct:

args *arguments = (args *)malloc(sizeof(args));

I need to set each element of args.arr to argv (command line argument) as an integer. I don't quite understand how to set each element of args.arr:

for(i = 1; i < argc; i++)
    arguments->arr[i] = atoi(argv[i]); // Edit: Segmentation fault on this line

I created an array of threads:

pthread_t threads[4];

..and pass the arguments to each function call:

for(i = 0; i < 4; i++)
    pthread_create(&threads[i], NULL, func, arguments);
1
  • Please don't say "this doesn't work" without saying what doesn't work. It's utterly unhelpful. Commented Oct 20, 2013 at 22:21

2 Answers 2

1

arr is an int pointer, so you need to allocate it first:

arguments->arr = malloc(argc * sizeof(int));

Then you can assign properly:

for(i = 1; i < argc; i++)
    arguments->arr[i] = atoi(argv[i]);
Sign up to request clarification or add additional context in comments.

Comments

1

It does not look like you have initialized the value of arguments->arr to point at a valid int.

Comments

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.