Thread: Just a tiny little problem...

  1. #16
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Daniel check this one:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SIZE 4
    int main()
    {
    char strings[SIZE][SIZE]={"Xbc", "Zbc", "Hbc", "Dbc"};
    
    printf("Before Sort:\n\n");
    print_str_array(strings,SIZE);
    
    str_bsort(strings, SIZE);
    
    printf("\nAfter Sort:\n\n");
    print_str_array(strings,SIZE);
    
    }
    
    
    int str_bsort(char strings[0][SIZE], int asize)
    {
    int i, j;
    char temp[SIZE];
    
    for(i=0; i<asize; i++)
    
    for(j=0; j<asize-1; j++)
    if(strcmp(strings[ j ], strings[ j+1 ])>0)
    {
    strcpy(temp, strings[ j ]);
    strcpy(strings[ j ], strings[ j+1 ]);
    strcpy(strings[ j+1 ], temp);
    }
    
    
    }
    
    
    int print_str_array(char strings[0][SIZE], int asize)
    {
    int i;
    
    for(i=0; i<asize; i++)
    printf("%s\n",strings[i]);
    
    }
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just a quick fix on your code zahid
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 4
    
    void str_bsort(char strings[][SIZE], int asize);
    void print_str_array(char strings[][SIZE], int asize);
    
    int main(void)
    {
      char strings[SIZE][SIZE]={"Xbc", "Zbc", "Hbc", "Dbc"};
    
      printf("Before Sort:\n\n");
      print_str_array(strings,SIZE);
    
      str_bsort(strings, SIZE);
    
      printf("\nAfter Sort:\n\n");
      print_str_array(strings,SIZE);
    
      return EXIT_SUCCESS;
    }
    
    void str_bsort(char strings[][SIZE], int asize)
    {
      int i, j;
      char temp[SIZE];
    
      for(i=0; i<asize; i++)
        for(j=0; j<asize-1; j++)
          if(strcmp(strings[ j ], strings[ j+1 ])>0){
            strcpy(temp, strings[ j ]);
            strcpy(strings[ j ], strings[ j+1 ]);
            strcpy(strings[ j+1 ], temp);
          }
    }
    
    void print_str_array(char strings[][SIZE], int asize)
    {
      int i;
      for(i=0; i<asize; i++)
        printf("%s\n",strings[i]);
    }
    It may be different on your compiler, but on mine functions won't accept an array size of 0. And you forgot to prototype your functions.

    -Prelude
    My best code is written with the delete key.

  3. #18
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    why does a double subscripted array will solve the problem?

    more importantly, what is the problem with the array of strings?

    thnx
    Last edited by Nutshell; 01-24-2002 at 08:12 PM.

  4. #19
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    also, my books also says that using a double-subscripted array to store strings is not as efficient as an array of pointers, if most of the strings are shorter than the longest string.

  5. #20
    Registered User
    Join Date
    Jan 2002
    Posts
    6
    why does a double subscripted array will solve the problem?

    more importantly, what is the problem with the array of strings?
    You haven't specifically said what errors are occuring, but I'm inclined to think that your program is segfaulting when it attempts to overwrite the different strings.

    The following...
    Code:
    char *strings[ ] = { "XYE", "BCD", "CDE", "DEF" };
    ...for me, creates an array of pointers to characters, as opposed to an array of strings (which would be an array of character arrays).

    Now, your string literals must be kept somewhere in your program, but because you haven't allocated memory for the string literals, they are being kept somewhere else. I don't know where, but I suspect that they are being retained in the code (?) instead of being on the heap where you can manipulate them.

    If they are being kept in the code, then you can't change them, or write to them at all. I tried the following program which compiles with no errors...

    Code:
    #include <stdio.h>
    int main(void)
    {
            char *blah[]={"blah", "blahblah"};
    /*        blah[0][1]='h';  */
            printf("blah1:%s\nblah2:%s\n", blah[0], blah[1]);
            printf("sizeof(blah1)==%d\nsizeof(blah2)==%d\n", sizeof(blah[0]), sizeof(blah[1]));
            printf("sizeof(blah[0][1])==%d\nsizeof(blah)==%d\n", sizeof(blah[0][1]), sizeof(blah));
            return 0;
    }
    and gives me...

    Code:
    [~/misc/tries@poseidon] Milord? gcc -o try2 try2.c
    [~/misc/tries@poseidon] Milord? try2 
    blah1:blah
    blah2:blahblah
    sizeof(blah1)==4
    sizeof(blah2)==4
    sizeof(blah[0][1])==1
    sizeof(blah)==8
    [~/misc/tries@poseidon] Milord?
    Where the size of blah1 and blah2 is 4 bytes, ie. the size of a pointer address whilst the size of blah[0][1] is 1 byte, ie. the size of a char.

    Should I uncomment the second line in main, however, the program segfaults before any output which suggests that the program cannot change any of the strings because the memory that they reside in is protected.

    If you want to write over the memory that the strings reside in, you would need to dynamically allocate them if you didn't wish to use a double array.

  6. #21
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ok, so when i save strings in an array of pointers, i can't modify it right?

    Thnx

  7. #22
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Nutshell
    ok, so when i save strings in an array of pointers, i can't modify it right?

    Thnx
    Yup.....just as Prelude said earlier

  8. #23
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    also, my books also says that using a double-subscripted array to store strings is not as efficient as an array of pointers, if most of the strings are shorter than the longest string.
    This is a memory vs proccesor usage issue. It can take less memory, but it takes more work to access the strings. It really all depends.

    Now consider the two following initialiations....
    Code:
    char * pChar = "Hello World!";
    char aChar[] = "Hello World!";
    Both create an array in memory, but there are two important differences....

    1) You can change the value of pChar, you can't change the value of aChar.
    2) You can change the memory pointed at by aChar. Changing the memory pointed at by pChar is undefined.

    Since your array is an array of char *s, the effect of changing the memory pointed at by them is undefined (incidentally, it ran fine on my computer... *shrug*).

    Even if it did work, consider what would happen if you actually had strings that were different length. Say you try to swap the contents of a 5 character string and a 3 character string... one of the strings wouldn't have enough room. Therefore, you really need to just use an array of strings, all of which have the same length... which is basically a 2d array, so that you can move the largest string into any part of the array.




    Still, there's a better way to do it....
    Code:
    if ( strcmp( a[ j ], a[ j + 1 ] ) > 0 ) {
                strcpy( hold, a[ j ] );
                strcpy( a[ j ], a[ j + 1 ] );
                strcpy( a[ j + 1 ], hold );
             }
    Okay, so a[j] points at one string, and a[j+1] points at another, and you want to swap them. Above you do so by moving the contents of the string.
    Code:
    if ( strcmp( a[ j ], a[ j + 1 ] ) > 0 ) {
                hold = a[ j ];
                a[ j ] = a[ j + 1 ] );
                a[ j + 1 ] = hold );
             }
    In this segment of code, the swap is done by just swapping the addresses of the strings. I believe this way should work.

    It's kinda like if two people want to swap furniture, they can either move the furniture between their houses, or they can just swap houses.

    Also, instead of keeping a size variable, you're probably better off using something like this...
    Code:
    bubbleSort_strings( strings, sizeof(strings) / sizeof(strings[0]));
    As long as strings is an array, this will give you the number of elements in the array.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #24
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Study the bubble sort syntax! I hate your guys ugly syntax!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void bubbleSort_strings( char *a[], const int size );
    
    int main()
    {
       const int size = 4;
       int i;
       char *strings[ size ] = { "DEF", "CDE", "BBB", "ABC" };
    
       bubbleSort_strings( strings, size );
    
       printf( "Sorted list of strings are: \n" );
    
       for ( i = 0; i < size; i++ )
          printf( "%s ", strings[i] );
    
       return 0;
    }
    
    
    void bubbleSort_strings( char *a[], const int size )
    {
       int i,ii;
       char * temp;
    
       for ( i = 0; i < size ; i++ )
       {
          for ( ii = i + 1; ii < size ; ii++ )
    	  {
             if ( strcmp( a[ i], a[ ii ] ) == 1 ) 
    		 {
                temp = a[ i ];
                a[ i ] = a[ ii ];
                a[ ii ] = temp;
             }
    	  }
    	}
    }
    You only have to switch the array indexes.
    Last edited by Troll_King; 01-25-2002 at 04:22 AM.

  10. #25
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hey thnx trollking and questionC for the explanation, now i get it.

    many thnx

    Last edited by Nutshell; 01-25-2002 at 04:25 AM.

  11. #26
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    so what if you are going to create a two dimensional array based on an input of an fscanf? such as this
    PHP Code:
    fscanf(fPtr,"%d",&nDimension);
    char chArray[nDimension][nDimension]; 
    and then fill it using nested for loops. how would you go about accomplishing this?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  12. #27
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    This thread is a bit old (to say the least!)....if you ewant to continue discussion, please start a new thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM