3

I'm trying to solve a problem found on my C programming book.

#include <stdio.h>
char *f (char s[], char t[], char out[]);
int main(void)
{
    char s[] = "ISBN-978-8884981431";
    char t[] = "ISBN-978-8863720181";
    char out[10];
    printf ("%s\n", f(s,t,out));
    return 0;
}
char *f (char s[], char t[], char out[]) {
    int i;
    for (i=0; s[i] == t[i]; i++)
    out[i] = s[i];
out[i] = '\0';
return &out[0];
}

As you can see from the code, this code uses as return value &out[0]: does this means that the complete array is used as return value?

char *f (char s[], char t[], char out[]);
int main(void)
{
    char s[] = "ISBN-978-8884981431";
    char t[] = "ISBN-978-8863720181";
    char out[10];
    printf ("%s\n", f(s,t,out));
    return 0;
}
char *f (char s[], char t[], char out[]) {
    for (; *(s+=1) == *(t+=1);)
            *(out+=1) = *s;
    *(out+1) = '\0';
    return out;
}

This is my proposed solution, but while the proposed code returns "ISBN-978-88", mine only returns "8". The array is smaller than the lenght of the string, how the proposed code can work without any kind of overflow? Thanks for your responses.

3
  • 2
    You're moving "out" forward. What you return is pointer to last character in your string (because ++out moves it character by character to the end). Commented Jan 8, 2014 at 10:03
  • 1
    with *(out+1)='\0';, you end the char array with NULL terminated character at index 1. Therefore, you can only see only 1 char when printing it out. Commented Jan 8, 2014 at 10:04
  • 2
    What you need to do is copy the initial value of out into another variable and return that in the end. Also, the for loop would typically be written as while(*++s == *++t). Commented Jan 8, 2014 at 10:04

3 Answers 3

3

Your code is too aggressive on side effects: the += 1 operation (which is more commonly denoted simply as ++) should be applied after the copy to the output has been made, not after the comparison.

In addition, you need to save the value of the out buffer before incrementing the pointer, so that you could return a pointer to the beginning of the copied string.

char *orig = out;
for ( ; *s == *t ; s++, t++)
    *out++ = *s;
*out = '\0';
return orig;

Demo on ideone.

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

2 Comments

doesn't actually answer his issue, though I agree with the comments about over-use of side effects.
I didn't take in consideration the use of out as a buffer array and then return orign. Thanks a lot.
2

Your code is returning a pointer to the end of the out array. Not the start. You need to stash the initial value of out and return that.

As an aside, the fact that you can do assignments inside a comparison doesn't mean it's a good idea. That code is going to be very hard to maintain.

2 Comments

It also skips the first character, since it increments both pointers before dereferencing and comparing.
@DrewMcGowen urg, so it does
2

&out[0] is equivalent to out. Since arrays in C are passed by reference, in a sense, yes it does return the entire array.

Your solution only prints "8" because you're returning a pointer into the middle of the array. When it tries to print the string, it has no way of knowing that it's in the middle of the array/string, thus you only get a substring printed.

1 Comment

See others' answers - you basically need to keep the value of out before incrementing it, and return that original value.

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.