DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2005
    Posts
    7

    Need help with String input in C language

    I couldn't a forum for C language so I'm going to ask it here because I don't know where else to put it.

    I need help with getting string input from the user and sorting the array of strings.

    Here's my code:
    #include <stdio.h>
    #include <string.h>

    /* This function will take two pointers to
    * chars as arguments and exchange the
    * values to which they point.
    */
    void exchange( char** pStr1, char** pStr2 )
    {
    char* text = *pStr1;
    *pStr1 = *pStr2;
    *pStr2 = text;
    }

    /* This function rearranges the text
    * in the array.
    */
    void sort( char* string_array[], int length )
    {
    int k;
    int test;

    do
    {
    test = 0;
    for( k = 1; k < length; k++ )
    {
    if( strlen( string_array[k] ) < strlen( string_array[k] ) )
    {
    exchange( &string_array[k], &string_array[k-1] );
    test = 1;
    }
    }
    } while ( test );
    }

    /* This function takes inputs from the user's
    * keyboard and stores them in the array.
    */

    int get_input( char* string_array[], int length )
    {
    int k;

    printf( "\nEnter up to five strings \n" );
    printf( "Enter a null string to indicate completion \n" );

    for ( k = 0; k < length; k++ )
    {
    printf( "%d: ", k+1 );
    scanf( "%100s", string_array[k] );
    if( string_array[k] == "" )
    {
    break;
    }
    }

    /* k is number of entries filled */
    return k;
    }

    void print_array( char* string_array[], int length )
    {
    int i;

    for( i = 0; i < 5; i++ )
    {
    printf( "%d: %s\n", i+1, string_array[i] );
    }
    }

    int main()
    {
    char input[100];
    char* string_array = input;
    int text_input;

    text_input = get_input( string_array, 5 );

    printf( "Before sort:\n" );
    print_array( string_array, text_input );

    sort( string_array, text_input );

    printf( "After sort:\n" );
    print_array( string_array, text_input );
    return 0;
    }
    When I compile it, I get errors saying,

    "passing arg 1 of `get_input' from incompatible pointer type"

    There are other errors just like that one, but instead of 'get_input' it's one of the other functions. I don't know what I'm doing wrong, but if someone could point in the right direction I would gladly appreciate it. Also, if possible, point out any other errors that I have made.

    Thanks!

    -Falcone :)

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Your get_input function expects an array of strings, which is basically a pointer to a pointer to char. You're passing only pointer to char, which is an error. You need to declare the string array with two dimensions:

    char str[100][5];

    get_input(str, 5);
    Danny Kalev

Similar Threads

  1. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 11:18 AM
  2. Input string was not in a correct format
    By kenn_rosie in forum .NET
    Replies: 2
    Last Post: 01-11-2006, 09:28 AM
  3. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  4. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 10:50 PM
  5. Deadlock error.. how to remove
    By Chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 12:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links