if i understand this correct this would mean that if i wrote
line = tmp;
line1 in main would take the value of line, witch i tried and it doesn't. |
That doesn't work because line is a pointer. Here's an example:
1 2 3 4 5 6 7
|
int ar1[3] = {1,2,3}; // two arrays
int ar2[3] = {4,5,6};
int* ptr; // a pointer
ptr = ar1; // make it point to the first array
ptr = ar2; // make it point to the second array
|
Notice that NONE of this code changes ar1 or ar2. ar1 is still {1,2,3} and ar2 is still {4,5,6}; What we're changing here is which array the pointer points to.
This is what your function is doing. You have a pointer 'line' that points to 'line1' (your array in main).
If you say
line = tmp;
you're not changing line1 at all, instead you're just changing 'line' so that it points to 'tmp' instead of to 'line1'.
To copy the string from 'tmp' to 'line', you need to copy each character. There's a standard function
strcpy
that does this for you, but it requires that your string be null terminated (has a 0 character to mark the end of the string).
So you need to do 2 things:
1) put a null terminator at the end of your 'tmp' string
2) copy tmp to line with strcpy
Also -- it's worth noting that you don't even need to pass a 'size' parameter here. You can tell where the end of the C string is by finding the null terminator. That is how all the functions like strcpy, strlen, etc work. They just step through the string looking for the null.