@Quzah: Ah, I see. Sorry, I didn't realize this was a recently discussed topic. I've read through your discussion with Mr.Lnx and also took a look at the link you posted within the Bubble Sort Topic.
I think I understand. So in C there is only passing by value. In the following example:
Code:
int foo(int *ptr, int num) //Line B
....
foo(&bar, bar2); //Line A
At Line A the memory address of bar is being passed into foo as a value. bar2 is having it's value passed as a value. Both 'values' have been copied so they can be used by foo.
In Line B, because the first parameter is int *ptr, the memory address of bar is now being pointed to by ptr. And bar2's value was copied to int num.
Is that accurate?
@CommonTater: Let me start off by clarifying. When I say skim I actually mean: read a little bit, if irrelevant jump down a few lines or a paragraph, read a little bit, if irrelevant jump down a few lines. Continue until relevant information is found. So I suppose I should of said Selectively Read instead of skimmed.
I think I understand the memory address as just a number. Check the above example to see if my understanding is correct.
I see so the problem I was having was not with "passing by reference"/pointer but that the pointer type isn't quite right. Since pthread_create casts the parameter as void*, it must be cast to an int* prior to dereferencing a value. I did a little experimenting
Code:
...
fiblist[0] = 13;
fiblist[1] = 17;
fiblist[2] = 23;
...
void *fibseq(void *fibpts) {
printf("fibpts%d - %d\n", *((int*)fibpts), fibpts);
fibpts++;
printf("fibpts%d - %d\n", *((int*)fibpts), fibpts);
fibpts++;
printf("fibpts%d - %d\n", *((int*)fibpts), fibpts);
fibpts++;
printf("fibpts%d - %d\n", *((int*)fibpts), fibpts);
fibpts++;
printf("fibpts%d - %d\n", *((int*)fibpts), fibpts);
fibpts += 4;
printf("fibpts%d - %d\n", *((int*)fibpts), fibpts);
*((int*)fibpts) = 300;
printf("ptr%d - %d\n", *ptr, ptr);
ptr++;
printf("ptr%d - %d\n", *ptr, ptr);
ptr++;
*ptr = 65;
printf("ptr%d - %d\n", *ptr, ptr);
OutPut:
fibpts 13 - 2673856
fibpts 285212672 - 2673857
fibpts 1114112 - 2673858
fibpts 4352 - 2673859
fibpts 17 - 2673860
fibpts 23 - 2673864
ptr 13 - 2683856
ptr 17 - 2683860
ptr 65 - 2683864
So tell me if this is correct. I was baffled at first as to why fibpts++ increments only one byte instead of to the next int array element like with the ptr. But since fibpts is originally declared as a void*, when the value of fibpts (the memory address of fiblist[0]) is incremented by one it isn't cast specifically as an int and the memory address is incremented by one number which corresponds to one byte. (No different than if I took the memory address and just added one)
Since ptr is of int and each int in a 32 bit enviornment is 4 bytes, when incrementing by one it automatically add's one 4 byte int, so it moves down 4 memory address spaces.
Does that sound right?
@Salem: Well for now this is just a program for myself to help understand how to use threads which turned into learning about pointers. Of course if this were to be used by others outside of just me, then I would include some error checking.