Thread: Stuck - Need a little help

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Stuck - Need a little help

    Hi I've tried for a few hours to figure this out - but the solution is escaping me.

    This is a snippet of code for a program I am writing in C. iServerListenPort has been previously assigned.

    Here is the code that does not work. It crashes after entering the IP address

    Code:
    char *serverip;
    printf("Please enter a IP address of the server you wish to connect to\n");
    scanf("%s",&serverip);
    printf("\nConnecting to %s Port %d\n", serverip, iServerListenPort);
    if(ClientSocketObject.Connect(serverip , iServerListenPort))
    {
        printf("Client Connected\n");
    }
    This code Does work
    Code:
    char *serverip = "192.168.2.179";
    printf("\nConnecting to %s Port %d\n", serverip, iServerListenPort);
    if(ClientSocketObject.Connect(serverip , iServerListenPort))
    {
       printf("Client Connected\n");
    }
    Here is the definition for socketobject.connect
    Code:
    bool Connect(char* szServerAddress, int iPort)
    Thank-You very much for any help!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Probably because scanf() has a nasty habit of stopping it's scan of char's whenever it encounters something it doesn't expect.

    Check what scanf is actually scanning into that variable, I'll bet it's not what you were expecting.

    You may want to use something like fgets(yourstringbuffer, sizeof(yourstringbuffer) - 1, stdin).

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Try
    char serverip[100];
    Not
    char *serverip;

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    Ah - Scanf was messing with my variable-- Thank-You very much Adak and Esbo - solved my problem..

    I'm going to be much more careful with scanf going forward.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    The problem was you declared a variable pointer but it was not pointing at any 'free space'
    it was pointing at address 0000 which is where scanf tries to store what you type in.
    It is likely it is illegal to write there or that you will overwrite something important.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And the fact you're most likely reading the line-break (\n) into the address.

    If you use fgets() ensure you lop the trailing \n off, see the FAQ.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    scanf("%s",&serverip);
    Perpahs you dont need to send the address of the string. It should have been just the string. shich as serverip.

    Code:
    char server[100];
    
    fgets(server,strlen[server],stdin);
    ssharish2005

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Adak View Post
    Probably because scanf() has a nasty habit of stopping it's scan of char's whenever it encounters something it doesn't expect.

    Check what scanf is actually scanning into that variable, I'll bet it's not what you were expecting.

    You may want to use something like fgets(yourstringbuffer, sizeof(yourstringbuffer) - 1, stdin).
    Why -1? fgets() includes the NULL terminator... Your just wasting 1 entire byte :O!

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets(server,strlen[server],stdin);
    Uh-Oh!..

    strlen is a function so strlen()

    and NEVER call it on the not initialized string...

    should be
    fgets(server,sizeof server,stdin);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The problem was you declared a variable pointer but it was not pointing at any 'free space'
    it was pointing at address 0000 which is where scanf tries to store what you type in.
    It is likely it is illegal to write there or that you will overwrite something important.
    No, it was an uninitialized pointer. There's a big difference between a NULL pointer and an uninitialized pointer. In this case, the pointer was pointing at some random location in memory. The result is the same, though. Segmentation fault.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM