Like Tree1Likes
  • 1 Post By laserlight

Strcat with pointers and Segmentation Fault

This is a discussion on Strcat with pointers and Segmentation Fault within the C Programming forums, part of the General Programming Boards category; I'm trying to code a strcat with pointers: Code: #include <stdio.h> void Strcat(char* s, char* t) { while(*s++ != '

Like Tree1Likes
  • 1 Post By laserlight

Thread: Strcat with pointers and Segmentation Fault

') ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Strcat with pointers and Segmentation Fault

    I'm trying to code a strcat with pointers:

    Code:
     
    
    #include <stdio.h> 
    
    void Strcat(char* s, char* t) 
    { 
       while(*s++ != '\0')
       ; 
       while( (*s++ = *t++) ) 
       ;      
    }     
    
    int main(void) 
    {  
       char* s1 = "Thames"; 
       char* s2 = "Thames";     
      
       Strcat(s1, s2);
       
       printf("The concatenated string is %s\n", s1); 
       return 0;
          
    }
    debug
    Code:
      
       gdb -q strcat2
    Reading symbols from /home/ethereal/C/Strings/strcat2...done.
    (gdb) break 5
    Breakpoint 1 at 0x400500: file strcat2.c, line 5.
    (gdb) break 7
    Breakpoint 2 at 0x400516: file strcat2.c, line 7.
    (gdb) run
    Starting program: /home/ethereal/C/Strings/strcat2 
    
    Breakpoint 1, Strcat (s=0x400680 "Thames", t=0x400680 "Thames") at strcat2.c:5
    5       while(*s++ != '\0')
    (gdb) c
    Continuing.
    
    Breakpoint 2, Strcat (s=0x400687 "", t=0x400680 "Thames") at strcat2.c:7
    7       while( (*s++ = *t++) ) 
    (gdb) c
    Continuing.
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400522 in Strcat (s=0x400687 "", t=0x400680 "Thames") at strcat2.c:7
    7       while( (*s++ = *t++) )

  2. #2
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,216
    Not surprising. You need adequate space for the result.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       char s1[100] = "Thames";
       const char *s2 = "Thames";
       
    
       strcat(s1, s2);
    
       puts(s1);
    
       return 0;
    }
    
    /* my output:
    ThamesThames
    */

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Not surprising. You need adequate space for the result.
    I think I've been trying to do magic with that code : \

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I'm getting only Thames and not ThamesThames as an output. I know it has something to do with

    Code:
     
      while(*s++ != '\0')
       ;
    because of that debug I posted before. However, I thought the expression was correct.
    [/code]

    Code:
     
    #include <stdio.h> #include <stdio.h> 
    
    #ifndef MAXSIZE 
      #define MAXSIZE 1000
    #endif  
    
    
    void Strcat(char* s, const char* t) 
    { 
       while(*s++ != '\0')
       ; 
       while( (*s++ = *t++) ) 
       ;      
    }     
    
    int main(void) 
    {  
       char  s1[MAXSIZE] = "Thames"; 
       const char* s2 = "Thames";     
      
       Strcat(s1, s2);
       
       printf("The concatenated string is %s\n", s1); 
       return 0;
          
    }     
    
    
    #ifndef MAXSIZE 
      #define MAXSIZE 1000
    #endif  
    
    
    void Strcat(char* s, const char* t) 
    { 
       while(*s++ != '\0')
       ; 
       while( (*s++ = *t++) ) 
       ;      
    }     
    
    int main(void) 
    {  
       char  s1[MAXSIZE] = "Thames"; 
       const char* s2 = "Thames";     
      
       Strcat(s1, s2);
       
       printf("The concatenated string is %s\n", s1); 
       return 0;
          
    }
    Last edited by thames; 12-01-2012 at 06:29 PM.

  5. #5
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,216
    Oh, well, after *s++ != '\0' is actually true, the postfix increment has moved the pointer one past the zero terminator. In effect it isn't over written in the next loop.

  6. #6
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    I see. I fixed the problem. Many thanks.

    Code:
     
    #include <stdio.h> 
    
    #ifndef MAXSIZE 
      #define MAXSIZE 1000
    #endif  
    
    
    void Strcat(char* s, const char* t) 
    { 
       while(*s != '\0')
        *s++;
       
       while( (*s++ = *t++) ) 
       ;      
    }     
    
    int main(void) 
    {  
       char  s1[MAXSIZE] = "Thames"; 
       const char* s2 = "Thames";     
      
       Strcat(s1, s2);
       
       printf("The concatenated string is %s\n", s1); 
       return 0;
          
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,787
    I observed this:
    Code:
    while(*s != '\0')
        *s++;
    It is unnecessary to dereference s if your intention is only to increment it, i.e.,
    Code:
    while (*s != '\0')
        s++;
    thames likes this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using strcat(Segmentation Fault)
    By cloudsword in forum C Programming
    Replies: 15
    Last Post: 03-30-2010, 11:27 PM
  2. Segmentation fault on strcat
    By paxmanchris in forum C Programming
    Replies: 9
    Last Post: 04-24-2007, 10:04 PM
  3. strcat causing segmentation fault?
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 09:42 AM
  4. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 10:29 AM
  5. segmentation fault - pointers/functions
    By p1c1078 in forum C Programming
    Replies: 15
    Last Post: 04-22-2003, 05:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21