More on pointers and strings
after alittle more time and errors i started working on more string and pointer functions, right now im confused.
Code:
#include <stdio.h>
#include <string.h>
int main() {
char temp[20] = "tes_ting";
char temp2[20] = {'\0'};
int Start = 2;
get_nextword(&temp, &temp2, '_', &Start, 8);
printf("%s\n\n", temp2);
return 0;
}
int get_nextword(char *src, char *dest, int sep, int *start, int max) {
char *spot;
if(max == '\0')
max = strlen(src);
copy(&src, &dest, &start, max);
spot = strchr(dest, sep);
dest[*spot] = '\0';
return 0;
}
/* WORKING CODE...WORKING CODE...WORKING CODE...WORKING CODE...WORKING CODE...*/
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;
}
the get_nextword function is just supposed to take a string and return everything before the seperator(sep). the copy function does work so i dont get why this is crashing, its doesnt even give me any warnings (im stuck in DevC in Win XP). any ideas?
Are you sure "WORKING CODE..." means working code?
Your "WORKING CODE", does not appear to be working. :)
Try adding this to the function get_nextword(), in place of the call to copy():
printf("src | dest | start | max\n",src,dest,*start,max);
printf("%s | %s | %d | %d <--- before copy()\n",src,dest,*start,max);
copy(src, dest, &start, max);
printf("%s | %s | %d | %d <--- after copy()\n",src,dest,*start,max);
getchar();
See the results of what copy() does; I don't think it is what you planned. Could you please add some comments, so we can see what you are trying to do exactly?