Whats wrong with the Output?

This is a discussion on Whats wrong with the Output? within the C Programming forums, part of the General Programming Boards category; Hi, I created a function which performs similarly to strcat(). Code: #include<stdio.h> #include<string.h> char xstrcat(char *s,char *q,int x); int main() ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Whats wrong with the Output?

    Hi, I created a function which performs similarly to strcat().
    Code:
    #include<stdio.h>
    #include<string.h>
    char xstrcat(char *s,char *q,int x);
    int main()
    {
      char a[]="Shabbir",b[]="Hussain";
      char *c[50];
      int x;
      
      x=strlen(a);
      xstrcat(a,b,x);
      printf("\n\n&#37;s\n\n",a);
      return 0;
    }
    
    char xstrcat(char *s,char *q,int x)
    {
      int i;
      for(i=0;i<=x;i++){
        if(*s=='\0'){
          *s=' ';
          s++;
          while(*q!='\0'){
    	*s=*q;
    	++s;
    	++q;
          }
        }
        else{
          ++s;
        }
      }
      *s='\0';
      return 0;
    }

    I got the desired Output, but also something else.
    Code:
    shabbirhussain@shabbirhussain:~/c$ ./c
    
    
    Shabbir Hussain
    
    *** stack smashing detected ***: ./c terminated
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0xb7ead138]
    /lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x0)[0xb7ead0f0]
    ./c[0x8048495]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7dd6450]
    ./c[0x80483a1]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:06 49660      /home/shabbirhussain/c/c
    08049000-0804a000 rw-p 00000000 08:06 49660      /home/shabbirhussain/c/c
    0804a000-0806b000 rw-p 0804a000 00:00 0          [heap]
    b7dbf000-b7dc0000 rw-p b7dbf000 00:00 0 
    b7dc0000-b7f09000 r-xp 00000000 08:06 286938     /lib/tls/i686/cmov/libc-2.7.so
    b7f09000-b7f0a000 r--p 00149000 08:06 286938     /lib/tls/i686/cmov/libc-2.7.so
    b7f0a000-b7f0c000 rw-p 0014a000 08:06 286938     /lib/tls/i686/cmov/libc-2.7.so
    b7f0c000-b7f0f000 rw-p b7f0c000 00:00 0 
    b7f16000-b7f20000 r-xp 00000000 08:06 269344     /lib/libgcc_s.so.1
    b7f20000-b7f21000 rw-p 0000a000 08:06 269344     /lib/libgcc_s.so.1
    b7f21000-b7f24000 rw-p b7f21000 00:00 0 
    b7f24000-b7f25000 r-xp b7f24000 00:00 0          [vdso]
    b7f25000-b7f3f000 r-xp 00000000 08:06 269299     /lib/ld-2.7.so
    b7f3f000-b7f41000 rw-p 00019000 08:06 269299     /lib/ld-2.7.so
    bf8a6000-bf8bb000 rw-p bffeb000 00:00 0          [stack]
    Aborted
    Is there something wrong with the compiler? What does this whole thing mean?
    Last edited by shabbirhussain; 08-26-2008 at 07:43 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you are packing both your names + a space in the array that has only just got enough room for the one name in variable a.
    Code:
    char a[]="Shabbir"
    will have exactly the right space to hold 'S', 'h', 'a', 'b', 'b', 'i', 'r', '\0' - which means that when concatenate your strings, you go over into space that doesn't belong to the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    And how do I avoid this problem?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by shabbirhussain View Post
    And how do I avoid this problem?
    Make the destination string long enough, e.g.
    Code:
    char a[100]="Shabbir"
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by shabbirhussain View Post
    And how do I avoid this problem?
    You have a variable called 'c' that is big enough and you don't even use it anywhere. You should remove the '*' though since you don't need 50 char* pointers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Oh I forgot to remove 'c'. I was experimenting with it. Thanks All.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 08:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 11:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 12:56 PM

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