Thread: Segmentation fault on strcpy to structure member

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

    Segmentation fault on strcpy to structure member

    I'm getting a segmentation error because of the strcpy on the last line, no idea why.

    Code:
    Code:
    void parse_initial_message (const char * message, Initial_parse *ip)
    {
        char temp_str[MESSAGE_MAX_LENGTH];
        char * temp_talker_ID;
        strcpy(temp_str, message); //we stored the message in temporary copy
        temp_talker_ID = strtok(temp_str, MSG_DELIMITER);
        strcpy(ip->talker_ID, temp_talker_ID);
    
        return;
    }
    Here is the structure
    Code:
    struct Initial_parse {
        char talker_ID[7];
        char message_ID[MESSAGE_ID_MAXLEN];
        char * data_field;
        int checksum;
    };

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Works for me.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define MESSAGE_MAX_LENGTH 100
    #define MESSAGE_ID_MAXLEN 10
    #define MSG_DELIMITER ","
    
    typedef struct Initial_parse {
        char talker_ID[7];
        char message_ID[MESSAGE_ID_MAXLEN];
        char * data_field;
        int checksum;
    } Initial_parse;
    
    void parse_initial_message (const char * message, Initial_parse *ip)
    {
        char temp_str[MESSAGE_MAX_LENGTH];
        char * temp_talker_ID;
        strcpy(temp_str, message); //we stored the message in temporary copy
        temp_talker_ID = strtok(temp_str, MSG_DELIMITER);
        strcpy(ip->talker_ID, temp_talker_ID);
    
        return;
    }
    
    int main ( ) {
      Initial_parse ip;
      parse_initial_message("hello,world",&ip);
      printf("%s\n",ip.talker_ID);
    }
    Obviously, you've screwed up the memory somewhere else in the code.

    What you posted is where the problem shows up, not where the problem was caused.

    Maybe you call it with a string which is too long, or is missing the delimiter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcpy not overwriting (segmentation fault)
    By Sergi in forum C Programming
    Replies: 2
    Last Post: 10-17-2019, 05:45 PM
  2. quadtree structure segmentation fault
    By propedro in forum C Programming
    Replies: 4
    Last Post: 04-16-2013, 05:21 PM
  3. Append or strcpy segmentation fault
    By Eramol in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2012, 08:30 AM
  4. Structure Segmentation Fault
    By GouSan in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2007, 01:06 AM
  5. Segmentation Fault on -> and strcpy line
    By 6thmercury in forum C Programming
    Replies: 5
    Last Post: 04-12-2005, 06:10 PM

Tags for this Thread