Thread: Just a tiny little problem...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Just a tiny little problem...

    Hi,

    THis is a program which alphabetizes a list of strings using bubblesort. But theres only one compile error: variable-sized may not be initialized. BUt it's already been initialized. Pls help, other than this the problem worked fine.

    THnx

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void bubbleSort_strings( char *a[], int size );
    
    int main()
    {
       int i, size = 4;
       char *strings[ size ] = { "ABC", "BCD", "CDE", "DEF" };
    
       bubbleSort_strings( strings, size );
    
       printf( "Sorted list of strings are: \n" );
       for ( i = 0; i < size; i++ )
          printf( "%s ", strings[ i ] );
    
       system( "PAUSE" );
       return 0;
    }
    
    
    void bubbleSort_strings( char *a[], int size )
    {
       int pass, j;
       char hold[ 100 ];
    
       for ( pass = 1; pass <= size - 1; pass++ )
          for ( j = 0; j <= size - 2; j++ )
             if ( strcmp( a[ j ], a[ j + 1 ] ) == 1 ) {
                strcpy( hold, a[ j ] );
                strcpy( a[ j ], a[ j + 1 ] );
                strcpy( a[ j + 1 ], hold );
             }
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> int i, size = 4;

    const int size = 4;
    int i;

    Where is system() declared? (And you might want to look in the FAQ sometime to see some reasons why system() is a bad idea!)
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    changed to const, but still with the same compile error. Why ?

    why system() is a bad idea? I checked the FAQ but couldn't find this topic?

    pls help

    thnx

  4. #4

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    any fix for my little problem of my program? Like the compile error ?

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Nutshell
    any fix for my little problem of my program? Like the compile error ?
    Yes, listen to adrian and declare the size as a const variable.

    Also if you must use system() include <stdlib.h>

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    thing is, i already declared it with 'const', still didn't work, same error!

    pls help

    thnx

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 4
    void bubbleSort_strings( char *a[], int size );
    
    int main()
    {
       int i;
       char *strings[ SIZE ] = { "ABC", "BCD", "CDE", "DEF" };
    
       bubbleSort_strings( strings, SIZE );
    
       printf( "Sorted list of strings are: \n" );
       for ( i = 0; i < SIZE; i++ )
          printf( "%s ", strings[ i ] );
    
       //system( "PAUSE" );
       return 0;
    }
    
    
    void bubbleSort_strings( char *a[], int size )
    {
       int pass, j;
       char hold[ 100 ];
    
       for ( pass = 1; pass <= size - 1; pass++ )
          for ( j = 0; j <= size - 2; j++ )
             if ( strcmp( a[ j ], a[ j + 1 ] ) == 1 ) {
                strcpy( hold, a[ j ] );
                strcpy( a[ j ], a[ j + 1 ] );
                strcpy( a[ j + 1 ], hold );
             }
    }
    Hmm..maybe const is different in C as compared to C++ (which I compile in)

    Try a #define as above

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void bubbleSort_strings( char *a[], int size );
    
    int main()
    {
       const int size = 4;
       int i;
       char *strings[ size ] = { "ABC", "BCD", "CDE", "DEF" };
    
       bubbleSort_strings( strings, size );
    
       printf( "Sorted list of strings are: \n" );
       for ( i = 0; i < size; i++ )
          printf( "%s ", strings[ i ] );
    
      // system( "PAUSE" );
       return 0;
    }
    
    
    void bubbleSort_strings( char *a[], int size )
    {
       int pass, j;
       char hold[ 100 ];
    
       for ( pass = 1; pass <= size - 1; pass++ )
          for ( j = 0; j <= size - 2; j++ )
             if ( strcmp( a[ j ], a[ j + 1 ] ) == 1 ) {
                strcpy( hold, a[ j ] );
                strcpy( a[ j ], a[ j + 1 ] );
                strcpy( a[ j + 1 ], hold );
             }
    }
    This compiles and runs for me. (I have commented out your pause, my compiler does not need it). The program does, however, still have problems. Your 4 strings are currently in order, try replacing "ABC" with "XYZ" and see what happens!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Hi Fordy, yes, possibly, I am running this through VC++! Anyway, he will need a compile time resolvable value, #define would definitely be resolvable!

    *** EDIT ***

    My house "C" person seemed to think that const would work for "C" as well.

    NutShell: What compiler/OS are you using?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by adrianxw
    Hi Fordy, yes, possibly, I am running this through VC++! Anyway, he will need a compile time resolvable value, #define would definitely be resolvable!

    *** EDIT ***

    My house "C" person seemed to think that const would work for "C" as well.

    NutShell: What compiler/OS are you using?
    Hmm...weird inconsistancies......

    I compiled this as a Cpp file in DevC++ and it worked with the const...I wondered if it would be different with a C file...didnt bother to find out..

    I can understand the compiler being dubious about using a varible for this, but as C++ promotes the use of const for constants instead of #define I automatically assumed C would do the same....perhaps not

    NUTSHELL:

    Anyway, if you are declaring the contents of the array, you dont need to specify the size of the array.....The compiler will do it for you

    If the size can be changed at runtime, you need to use malloc and create the memory space on the freestore

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Yeah...tried it in C

    Code:
    const int size = 4;
       char *strings[ size] = { "ABC", "BCD", "CDE", "DEF" };
    This is AOK in C++, but the compiler chokes if you compile it as a C file..........

    Seems const is not resolved at compile time as it is in C++.......

    I guess it was altered in C++ to help do away with #define

    I'm sure some C-Fullblood will give the answer to this soon

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Intruiging huh! Have you tried changing one of his strings?

    >>>
    Your 4 strings are currently in order, try replacing "ABC" with "XYZ" and see what happens!
    <<<

    VC++ did not like that at all!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    changed my code to below, still have problems...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void bubbleSort_strings( char *a[], int size );
    
    int main()
    {
       int i;
       const int size = 4;
       char *strings[ ] = { "XYE", "BCD", "CDE", "DEF" };
    
       bubbleSort_strings( strings, size );
    
       printf( "Sorted list of strings are: \n" );
       for ( i = 0; i < size; i++ )
          printf( "%s ", strings[ i ] );
    
       system( "PAUSE" );
       return 0;
    }
    
    
    void bubbleSort_strings( char *a[], int size )
    {
       int pass, j;
       char hold[ 100 ];
    
       for ( pass = 1; pass <= size - 1; pass++ )
          for ( j = 0; j <= size - 2; j++ )
             if ( strcmp( a[ j ], a[ j + 1 ] ) > 0 ) {
                strcpy( hold, a[ j ] );
                strcpy( a[ j ], a[ j + 1 ] );
                strcpy( a[ j + 1 ], hold );
             }
    }

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char *strings[ ] = { "XYE", "BCD", "CDE", "DEF" };
    Okay so far, but there's a bug right here that will creep up on you later.

    >strcpy( a[ j ], a[ j + 1 ] );
    BANG! You're dead, another victim to the access violation bug. You can't assign to a string constant, char *strings[] has the same access rules as char *strings in this case.

    enjoy.

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

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