Thread: String appears to change on its own

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    String appears to change on its own

    Hi i am working on this program. I have looked over it and i cannot determine an explanation for its behavior. Can someone help explain why and how to overcome this issue? Thanks a bunch.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int
    main ()
    {
      char *the_message = "42.77417833333333|-78.31375166666667";
      char latitude[17];
      char longitude[17];
      int lat_counter_x = 0;
      int long_counter_x = 0;
      int long_counter_y = 0;
      int pipe_location;
    
      for (lat_counter_x = 0; lat_counter_x <= strlen (the_message);
           lat_counter_x++)
        {
          if (the_message[lat_counter_x] == '|')
            {
              pipe_location = lat_counter_x;        //shows character location of | - separates lat/long numbers
            }
        }
    
      if (pipe_location)
        {
          for (lat_counter_x = 0; lat_counter_x < (pipe_location - 1);
               lat_counter_x++)
            {
              latitude[lat_counter_x] = the_message[lat_counter_x];
            }
          latitude[lat_counter_x + 1] = '\0';
        }
      printf ("Latitude: %s\n", latitude);  // displays properly 42.77417833333333
      for (long_counter_x = (pipe_location + 1);
           long_counter_x <= strlen (the_message); long_counter_x++)
        {
          longitude[long_counter_y] = the_message[long_counter_x];
          long_counter_y++;
        }
      longitude[long_counter_y] = '\0';
      printf ("Longitude: %s\n", longitude);        // displays properly -78.31375166666667
      printf ("Latitude: %s\n", latitude);  //the number changes .... why? - latitude is now the last char of longitude
      printf ("Longitude: %s\n", longitude);        // displays properly -78.31375166666667
    }
    
    //If i comment out the second for loop, latitude is not affected.
    
    /* Output
    Latitude: 42.7741783333333
    Longitude: -78.31375166666667
    Latitude: 7
    Longitude: -78.31375166666667
    */

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
             1111111111
    1234567890123456789
    
    -78.31375166666667n
    The n is the \0 character. What are the odds that a 17-character array is long enough to hold this? (Hint: not good.)

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why don't you use standard function like strchr(),strcpy() ?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    Why don't you use standard function like strchr(),strcpy() ?
    Beat me to it... I was just going to say that all that could probably be reduced to about 5 lines of code using strtok() to find the pipe symbol and then he doesn't even need to use strcpy. But in this case I agree strchr() might be easier.

    Code:
      char *the_message; //= "42.77417833333333|-78.31375166666667";  
      char *latitude = the_message;
      char *longitude = strchr(the_message,'|');
      the_message[longitude++] = 0;
      printf("Lat = %s  Long = %s\n",latitude,longitude);
    Of course it will grow a bit with error checking....
    Last edited by CommonTater; 01-22-2011 at 10:40 PM. Reason: for the overly critical....

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    LOL, Is it pseudo Code?!
    Modifying string literal is Undefined Behavior.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    LOL, Is it pseudo Code?!
    Modifying string literal is Undefined Behavior.
    True... However; I would suspect in his final application that message comes from a GPS so we can call it a prediction... (See above)

    Moreover... have you ever read my sig at the bottom of every message?
    Last edited by CommonTater; 01-22-2011 at 10:41 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
      char *the_message; //= "42.77417833333333|-78.31375166666667";  
      char *latitude = the_message;
      char *longitude = strchr(the_message,'|');
      the_message[longitude++] = 0;
      printf("Lat = %s  Long = %s\n",latitude,longitude);
    Actually I'm a bit concerned that someone wouldn't know what to do anyway since a pointer is being used as an index here. Maybe the_message[longitude - latitude] = 0; is more correct.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Actually I'm a bit concerned that someone wouldn't know what to do anyway since a pointer is being used as an index here. Maybe the_message[longitude - latitude] = 0; is more correct.
    Got me on that one.... Yep... or *(latitude++) = 0; my mistake.

    Nice catch, btw.
    Last edited by CommonTater; 01-22-2011 at 11:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. how do I change the case of a string?
    By eligilbert12 in forum C Programming
    Replies: 4
    Last Post: 03-11-2010, 10:19 AM
  3. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM