Simple string question

This is a discussion on Simple string question within the C Programming forums, part of the General Programming Boards category; Okay, I'm stumped. Can anyone see what's wrong this bit of code?: Code: int c, r , t; char *str; ...

  1. #1
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16

    Simple string question

    Okay, I'm stumped. Can anyone see what's wrong this bit of code?:
    Code:
    int c, r , t;
    char *str;
    char[10] tmp;
     
    for (r=0; r<array->rows; r++) {
       for (c=0; c<array->columns; c++) {
    	  t = array2d_get(array, c, r);
    	  sprintf(tmp, "%d ", t);
    	  str = strcat(str, tmp);
       }
    }
     
    printf("%s", str);
    The function "array2d_get" simply returns an int, and "array" is a structure defined earlier. When I try to run this code I get random crashes and unpredictable output, can anyone see the problem? The crashes all go away if I remove the strcat line...

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > char[10] tmp;
    This isn't even valid C, so how you managed to get anything to run is beyond me.

    Plus str is not pointing anywhere either, so you're just writing data to some unknown location.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    > char[10] tmp;
    This isn't even valid C, so how you managed to get anything to run is beyond me.
    Ah, that'd be a stupid mistake then. I was copying the code from one screen to another and obviously messed up.

    Plus str is not pointing anywhere either, so you're just writing data to some unknown location.
    Oh okay, that makes some kind of sense. I apologise for my ignorance: I'm trying to teach myself C from a textbook that for some reason barely touches on string manipulation.

    Essentially what I'm trying to do is print the contents of a two dimensional array to a string. Since I don't know the exact length of the string I'm unsure how I would go about doing this, aside from creating a ludicrously big char array and filling it in as I go.

    I'll keep looking...

  4. #4
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    Okay, I seem to have got a working method, although it does seem a little long-winded for something as simple as string concatenation:

    Code:
    int c,r;
    char *str;
    char tmp[10];
     
    //The initial size of the string buffer
    int strSize = (100 * sizeof(char));
     
    //Allocate memory for the string buffer
    str = (char *) malloc(strSize);
    strcpy(str, "");
     
    //Add one element of the array at a time
    for (r=0; r<array->rows; r++) {
       for (c=0; c<array->columns; c++) {
    	 //Allocate more memory to the string buffer if required
    	 if (strlen(str) > (strSize - 25)) {
    		 strSize += (100 * sizeof(char));
    		 str = realloc(str, strSize);
    	 }
     
    	 t = array2d_get(array, c, r);
    	 sprintf(tmp, "%d ", t);
    	 str = strcat(str, tmp);
       }
     
       //Add a newline character for each row
       str = strcat(str, "\n");
    }
     
    return str;
    This seems to work well enough but I doubt it's the best way; can anyone give me any useful tips (Aside from optimising it)? I'd imagine someone with more experience (i.e. more than a week ) could come up with a better solution...

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    That's pretty good going

    Just a couple of points.

    > str = (char *) malloc(strSize);
    There is no need to cast the result of malloc in C.
    See the FAQ for details.

    > str = realloc(str, strSize);
    When you call realloc, you should assign to a temporary first, just in case it returns NULL.
    Code:
    void *temp = realloc(str, strSize);
    if ( temp != NULL ) {
      str = temp;
    } else {
      // some error
      free( str );
    }
    > str = strcat(str, tmp);
    You don't need to assign anything here. strcat always returns the first argument.
    The string data will have been appended whether you do the assignment or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    Cool, I'll make those changes. Thanks very much for the info.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    In strcat(str, tmp) you're passing the memory address of the string, so changes made in that function will propagate out to calling function scope. Meaning, str in the calling function is also changed. This is a property of most C-char*-but-called-string handling functions which modify their arguments, like fgets(), _scanf(), strcpy() etc.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  2. Simple string question?
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 11:47 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21