title: C segmentation fault when assigning values to the char members of my struct
body: <p>I have the following code:</p>
  
  <pre><code>char *temp_sentence, *second_temp_ptr;
      char *token;
      int token_no = 0;
  
      temp_sentence = strdup(sentence);
      second_temp_ptr = temp_sentence;
  
      token = strsep (&amp;second_temp_ptr,",");
  
      while (token != NULL) {
          /*if a sentence has missing data then make that clear by settings it's value to
           * &lt;EMPTY&gt;*/
          if(strlen(token)==0){
              token = "&lt;EMPTY&gt;";
          }
  
          switch(token_no){
          case 0:
              gga_ptr-&gt;sentence_id = token;
              printf("%s,",gga_ptr-&gt;sentence_id);
              break;
          case 1:
              /*atoi converts a string to an int, well a c string anyways so a char* */
              gga_ptr-&gt;time_stamp = atoi(token);
              printf("%d,",gga_ptr-&gt;time_stamp);
              break;
          case 2:
              /*strtod coverts a string to a double, well a c string anyways so a char* */
              gga_ptr-&gt;latitude = strtod(token, NULL);
              printf("%f,",gga_ptr-&gt;latitude);
              break;
          case 3:
              //gga_ptr-&gt;north_south_id = token;
              //printf("%s,",gga_ptr-&gt;north_south_id);
              break;
          case 4:
              gga_ptr-&gt;longitude = strtod(token, NULL);
              printf("%f,",gga_ptr-&gt;longitude);
              break;
          case 5:
              gga_ptr-&gt;east_west_id = token;
              //printf("%s,",gga_ptr-&gt;east_west_id);
              break;
          case 6:
              gga_ptr-&gt;quality = atoi(token);
              printf("%d,",gga_ptr-&gt;quality);
              break;
          case 7:
              gga_ptr-&gt;no_of_satellites = atoi(token);
              printf("%d,",gga_ptr-&gt;no_of_satellites);
              break;
          case 8:
              gga_ptr-&gt;horizontal_dillution = strtod(token, NULL);
              printf("%f,",gga_ptr-&gt;horizontal_dillution);
              break;
          case 9:
              gga_ptr-&gt;altitude = strtod(token, NULL);
              printf("%f,",gga_ptr-&gt;altitude);
              break;
          case 10:
              gga_ptr-&gt;altitude_units = token;
              //printf("%s,",gga_ptr-&gt;altitude_units);
              break;
          case 11:
              gga_ptr-&gt;geodial_seperation = strtod(token, NULL);
              printf("%f,",gga_ptr-&gt;geodial_seperation);
              break;
          case 12:
              gga_ptr-&gt;geodial_seperation_units = token;
              //printf("%c,",gga_ptr-&gt;geodial_seperation_units);
              break;
          case 13:
              /*This is never used in the sentenced given*/
              gga_ptr-&gt;age_of_data_in_seconds = token;
              printf("%f,",gga_ptr-&gt;age_of_data_in_seconds);
              break;
          case 14:
              gga_ptr-&gt;checksum = token;
              printf("%s,",gga_ptr-&gt;checksum);
              break;
          }
          token_no++;
          token = strsep (&amp;second_temp_ptr, ",");
  
      }
  
      exit(1);
      free(temp_sentence);
  
  }
  </code></pre>
  
  <p>and the following gga_sentence structure:</p>
  
  <pre><code>typedef struct gga_sentence{
  
      char *untouched_sentence;
      char *sentence_id;
      int time_stamp;
      double latitude;
      char north_south_id;
      double longitude;
      char east_west_id;
      int quality;
      int no_of_satellites;
      double horizontal_dillution;
      double altitude;
      char altitude_units;
      double geodial_seperation;
      char geodial_seperation_units;
      char *age_of_data_in_seconds;
      char *checksum;
  
  }gga_sentence;
  </code></pre>
  
  <p>The above code works as expected, untill i try to print out the values that represent char members of my structure, so in the case-switch cases 3,5,10,12 produce a segmentation fault, if i remove these, the rest of the structure members print as expected.</p>
  
  <p>function input:</p>
  
  <pre><code>$GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
  </code></pre>
  
  <p>code output (exluding the print statements in cases 3,5,10,12):</p>
  
  <pre><code>$GPGGA,151019,5225.962700,401.162400,1,9,1.000000,38.900000,51.100000,51.100000,0000*72
  </code></pre>
  
  <p>expected output should be the same as the input, but these char members are casuing segmentation faults.</p>
  
  <p>when the tokens are printed:</p>
  
  <pre><code>token: $GPGGA
  token: 151019.000
  token: 5225.9627
  token: N
  token: 00401.1624
  token: W
  token: 1
  token: 09
  token: 1.0
  token: 38.9
  token: M
  token: 51.1
  token: M
  token: &lt;EMPTY&gt;
  token: 0000*72
  </code></pre>
  
  <p>output from gdb where, traceback:</p>
  
  <pre><code>(gdb) where
  #0  0x00007ffff7a5ef90 in _IO_vfprintf_internal (s=&lt;optimised out&gt;, format=&lt;optimised out&gt;, 
      ap=ap@entry=0x7fffffffd8b8) at vfprintf.c:1655
  #1  0x00007ffff7a65ff9 in __printf (format=&lt;optimised out&gt;) at printf.c:34
  #2  0x0000000000400daf in initiate_gga_values (gga_ptr=0x603250, 
      sentence=0x603250 "$GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72\r\n") at stream_1_parser.c:103
  #3  0x0000000000400bee in read_stream_1 (stream=0x603010) at stream_1_parser.c:40
  #4  0x0000000000400a42 in main (argc=1, argv=0x7fffffffdf78) at driver.c:28
  </code></pre>
  
  <p>stream_1_parser.c:40 is a call to the function given and driver.c:28 is a call to stream_1_parser.c:40</p>
  
  <p>Does anybody have any ideas?</p>
  
  <p>Cheers,
  Chris.</p>