Results 1 to 3 of 3

Thread: c++ strcat()

  1. #1

    c++ strcat()

    For some reason this code prints out a bunch of junk. What am I doing wrong with strcat?

    Result:


    \345rc/9\345s,\217\345r\2741\345s,\217\345r\2747\345s,\217\345r\274c\345s,\217\345r\274/3\345s,\217\345r\274c\345s,\217\345r\2740\345s,\217\345r\2743\345s,\217\345r\274/b\345s,\217\345r\2743\345s,\217\345r\274b\345s,\217\345r\2746\345s,\217\345r\274/9\345s,\217\345r\2745\345s,\217\345r\2742\345s,\217\345r\2741\345s,\217\345r\274/9\345s,\217\345r\274b\345s,\217\345r\2741\345s,\217\345r\2740\345s,\217\345r\274/0\345s,\217\345r\2742\345s,\217\345r\2746\345s,\217\345r\274\345\345s,\217\345r\274/\274\345s,\217\345r\274\345\345s,\217\345r\274\345\345s,\217\345r\274s\345s,\217\345r
    char * get_hash_dir ( char *url ) {

    char *ret = (char *)malloc(32+20*sizeof(char));
    char *hash = (char *)malloc(32*sizeof(char)+1);
    strcpy(ret,"c/");
    int i =0;
    int count; char c;

    hash = md5(url);



    while ( i < 32 ) {

    count = 0;

    while ( count < 4 ) {
    c = (char)hash[i];
    strcat(ret,&c);

    i++;
    count++;

    }
    strcat(ret,"/");
    i++;
    }

    return ret;
    }

  2. #2
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    I'm really not sure what you're trying to do here...but I'll tell you the problem all the same.

    On this line:
    Code:
     c = (char)hash[i];
    you dereference 'hash' (the [] implicitly dereferences) and copy the contents of that cell into a new local variable. This is important, since on the next line you get the address of that new local, not the function-scope string, just a single char. When you strcat on the following line, the internal pointer runs off into nowhere past that single variable until it hits a 0 or the end of your program's memory area. You want to do this instead of those two lines:
    Code:
    strcat(ret, hash + i);
    But I still don't think this is going to do what you want. That inner loop is going to duplicate a 32bit string for each iteration, then rotate a bit and move on. Anyway..you should probably look into the 'for' loop too .

  3. #3
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •