-
Pointer confusion
im having some trouble with pointers in linux (redhat 8.0 with gcc). i can run this code fine in windows but it just wont work right in linux
Code:
#include <stdio.h>
int main() {
char temp[20] = "testing";
char temp2[20] = {'\0'};
int spot = 2;
copy(temp, temp2, &spot, 5);
printf("%s\n", temp2);
return 0;
}
int copy(char *src, char *dest, int *start, int end) {
int x = 0;
--*start;
while(*start < end) {
dest[x] = src[*start];
++*start;
++x;
}
return 0;
}
copy is just supposed to take a source a dest and some posistion info. it take the start and end and changes dest to be the middle(of start and end).
sorry about asking for help on this same dam function but i just dont get it!!!
-
hmm, maybe you should describe the problem aswell.
I get "esti" from running it, seems right to me i'm to tired to think though :)
furthermore you should declare the copy function by adding
Code:
int copy(char *src, char *dest, int *start, int end);
and i don't really understand why you declare it to return an int when the return value is'nt used.
-
yeah, you should get "esti" from it. and with the return, it just seemed right. it doesnt sound like alot people like void functions and if i get this to work ill add some error checking to it.
but i only get "esti" from Windows(XP Pro) and not Linux(RedHat 8), any ideas?
-
Couldn't see much wrong with your code. I changed it a little, try this version and see how you get on.
Code:
#include <stdio.h>
int copy(char *src, char *dest, int start, int end);
int main(void)
{
char temp[20] = "testing";
char temp2[20] = { '\0' };
int spot = 2;
copy(temp, temp2, spot, 5);
printf("%s\n", temp2);
return(0);
}
int copy(char *src, char *dest, int start, int end)
{
int x = 0;
--start;
while (start < end)
{
dest[x] = src[start];
++start;
++x;
}
return(0);
}