Help : Segmentation Fault in C

View previous topic View next topic Go down

Help : Segmentation Fault in C

Post by BIT0112-Rokon on Thu Mar 31, 2011 11:50 pm

Its very weirdoooo.. I couldn't figure it out. Can anyone please find it out what is the problem here in the C code given bellow:

Code:


#include<stdio.h>

char * xstrcat(char *destination, const char *source);
int main()
{
    char* str=xstrcat("Harris","LovesC");
    printf("\n Concatinated Strings:%s",str);

    return 0;
}

/* Function to concatenate string source to end of destination and return destination */
char *xstrcat(char *destination,  const char *source)
{
    char *str = destination;
    if (destination == NULL || source == NULL)
        return destination; /* we need not have to do anything */
    while (*destination!='\0')
          destination++;
    while (*source!='\0')
    {
          *destination = *source;
          destination++;
          source++;
      }
    return str;
}



_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Help : Segmentation Fault in C

Post by BIT0102-Mohaimin on Sat Apr 02, 2011 4:12 am

In the second loop, you are trying to access an index of destination which is not allocated for it. This is the reason of segmentation fault I suppose. This case is equivalent to ArrayIndexOutOfBoundException in Java.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum