1

Please run it on your machine using gcc and tell if it also gives you a segmentation fault output. I don't think there is any problem with the program. I am a beginner in C. So Help!!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *scat(char *,char *);

void main()
{
    char *s="james";
    char *t="bond";

    char *w=scat(s,t);
    printf("the con: %s\n", w);
    free(w);
}

char *scat(char *s,char *t)
{ 
    char *p=malloc(strlen(s)+strlen(t)+1); 
    int temp=0 , ptr=0;

    while(s[temp]!='\0'){
        p[ptr++]=s[temp++];
    } 
    temp=0;
    while(t[temp]='\0'){
        p[ptr++]=t[temp++];
    }
    return p;
}
4
  • 3
    If you get a segmentation fault, so the program has a problem... Commented Jan 10, 2013 at 19:41
  • You should probably try formatting your code better. Commented Jan 10, 2013 at 19:41
  • 2
    First mistake: "I don't think there is any problem with the program" Commented Jan 10, 2013 at 19:41
  • memcpy() is your friend. Commented Jan 10, 2013 at 19:42

4 Answers 4

1

The second loop has no effect:

while(t[temp]='\0') { // <<== Assignment!!!
    p[ptr++]=t[temp++];
}

This should be a != not =, or better yet, you can drop zero altogether:

while(t[temp]) { // Zero-checking is implicit in C
    p[ptr++] = t[temp++];
}

Since you are not writing to s or t, it's probably a good idea to declare them both const. This would have caught the assignment in the while loop above.

Sign up to request clarification or add additional context in comments.

11 Comments

It should probably be !=. Good catch!
@wildplasser Thanks for catching the == vs. != :)
And since string literals are passed in, that is probably where it crashes.
@DanielFischer Right, so it's probably a good idea to declare both params const.
have run it on your gcc compiler and seen it?
|
1

You don't 0-terminate the string in scat. You could add:

p[ptr] = 0;

Right before returning.

2 Comments

please run it own your gcc compiler and tell me?
Naw man, it could wake Cthulhu because of the undefined behavior. WHOOPS!
0
  1. Please run it on your machine using gcc and tell if it also gives you a segmentation fault output.
  2. I don't think there is any problem with the program.

I don't follow your reasoning. If the program segfaults on your system then it is very likely that the program is at fault.

FYI this is what GCC(*) outputs. It doesn't even compile.

NOTE: It's g++ actually, but it shouldn't make too much of a difference.

7 Comments

was that output with -Wall, mr strawhat?
i copied this same program some a person. his program works and mine not. i rechecked both the programs 10 times but still the problem persist
@Mike it's compiled with -Wall -Werror -Wextra -pedantic-errors
@KundanNegi - Google search or look at the man for gcc, there are a lot of extra flags that will give you more details and help you find errors/warnings with your code
+1 for -Wall & friends, -1 for compiling C code as C++ :) so you're even ;)
|
0

There is no need to emulate the functions that exist in string.h:

#include <string.h>

char *scat(char *s,char *t)
{ 
    char *p;
    size_t len1, len2;
    len1 = strlen(s);
    len2 = strlen(t);

    p=malloc(len1+len2+1);
    if (!p) barf();
    memcpy(p,s, len1);
    memcpy(p+len1, t, len2+1);

    return p;
}

4 Comments

ya i know my friend but it is an exercise i want to try.
talking about functions that exist in string.h: strcat()
@KundanNegi reimplementing functions that are part of the C standard is generally a bad idea. Try exercising the use of said functions with a more complex example instead and you'll learn more useful aspects of the language.
Well, if you really want to avoid library functions, you could roll your own strlen(), too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.