PDA

View Full Version : Pointer confusion



mart_man00
02-14-2003, 01:17 PM
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



#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!!!

Fredd
02-14-2003, 01:53 PM
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

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.

mart_man00
02-14-2003, 03:12 PM
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?

Hammer
02-14-2003, 06:19 PM
Couldn't see much wrong with your code. I changed it a little, try this version and see how you get on.


#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);
}