When I print out my order[]
array (or even the value of the previous index order[num-1])
, every index is filled with the same value. Is this because when I pthread_join
, I am changing all_png
by reference and therefore when I set order[num] = all_png[0]
, when the value of all_png
changes, so does every value of order[] which I had previously stored?
pthread_t *p_tids = malloc(sizeof(pthread_t) * 1);
char *all_pngs[1] = {0};
int num;
char *order[50] = {0};
while(array_is_full(order) == 0){
//use thread to call CURL
pthread_create(p_tids, NULL, get_png, NULL);
pthread_join(p_tids[0], (void **)&(all_pngs[0]));
//parse the png name and put it in the correct order
sscanf(all_pngs[0], "%*[^0-9]%d", &num);
if(order[num] == NULL){
order[num] = all_pngs[0];
printf("%s %s\n", order[num-1], order[num]);
}
Also, array_is_full
also works as supposed to and just checks if the array is still filled with 0's or not. If there are still 0's (not full yet) return 0.
The get_png
method simply returns a void pointer to a char*. And fname
is simply a char[]
that holds the png file name (i.e. output_3.png). This function works as it's supposed to.
char *a = malloc(sizeof(char)*256);
a = fname;
return ((void *)a);
The output looks something like this.
(null) ./output_19.png
(null) ./output_16.png
(null) ./output_24.png
(null) ./output_21.png
(null) ./output_38.png
(null) ./output_5.png
(null) ./output_8.png
(null) ./output_7.png
(null) ./output_46.png
(null) ./output_43.png
(null) ./output_35.png
(null) ./output_11.png
(null) ./output_4.png
(null) ./output_48.png
./output_6.png ./output_6.png
./output_44.png ./output_44.png
./output_22.png ./output_22.png
./output_49.png ./output_49.png
(null) ./output_28.png
./output_47.png ./output_47.png
(null) ./output_26.png
(null) ./output_33.png
./output_17.png ./output_17.png
./output_18.png ./output_18.png
(null) ./output_32.png
(null) ./output_14.png
./output_45.png ./output_45.png
(null) ./output_42.png
./output_27.png ./output_27.png
./output_39.png ./output_39.png
(null) ./output_31.png
(null) ./output_30.png
./output_9.png ./output_9.png
As you can see the previous index of order is being updated to the current value of the current index even though I thought I was only setting order[num]
not order[num-1]
(which should have been set previously).
Why is this happening and is there a way to fix this?
Let me know if anything is unclear or if more code is needed. I am quite new to posting on here.
Thanks in advance!