Thread: More on pointers and strings

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    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?

  2. #2
    Heraclitus
    Guest

    Question 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?

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    In main() you have this:

    >get_nextword(&temp, &temp2, '_', &Start, 8);

    Here you want to pass the address of temp and temp2. Note that, because temp and temp2 are arrays, you should not put the & in front of the names. This:

    get_nextword (temp,...

    is the same as:

    get_nextword (&temp[0],...

    In both situations you pass the address of the first element of the array.

    Also your call in get_nextword() to copy() should then be:

    copy (src, dest, &start, max);

    Avoid using constructions like this:

    --*start;

    They are legal C, but don't make code very readable. More readable would be:

    (*start)--;

    You don't need the variable max, just use strlen() always instead.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    confused

    well, after checking my copy function I still say it works. ive done this a couple of times here before but its just supposed to take a inputted string and give what is inbetween start and end back.

    get_nextword is like copy but instead of putting in a number/posistion you put in a char to stop it and max is just so you can end it quickly or if where the char is matters.

    and with my pointer nightmare, why does this work? i thought a char was just a pointer to the first char, so the name is like -1 and the first char is 0. what am i missing here?

    Code:
    int main() {
        char temp[20] = "testing";  
        char temp2[20] = {'\0'};
        int spot = 2;
        
        copy(&temp, &temp2, &spot, 5);
        
        printf("%s", temp2);
        
        return 0;
    }
    if i have copy print all its args everything prints out fine except for dest. dest is just blank, but NULLed everything so that makes sense. and the code i just put up does print the right string with no errors/warnings.

    thanks for the replies guys

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    any one?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM
  5. need more help with pointers and strings
    By bgbfflochp in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2002, 08:31 AM