3

I have an error in my strjoin code. The path of my code, is to allow a string, and return a string ended with a '\0' by concatening two strings.

#include "libft.h"

char *ft_strjoin(const char *s1, const char *s2)
 {
    char *s3;

    s3 = malloc(sizeof(char) * (s1 + s2)); // I've an error in this line

    s3 = ft_strcat((char *)s1, s2);
    return (s3);
}

My error :

invalid operands to binary expression ('const char *' and 'const char *')

I don't know why I get this error. I've searched on the internet, and I don't find how to fix it.

6
  • Perhaps you wanted (strlen(s1)+strlen(s2)) instead of (s1+s2)? Commented Jan 13, 2015 at 14:57
  • 1
    What exactly is the purpose of this code (s1 + s2)? Commented Jan 13, 2015 at 14:58
  • Good thank you very much for your quick response ! It work Commented Jan 13, 2015 at 14:59
  • s3 = malloc(ft_strlen(s1) + ft_strlen(s2)+1);ft_strcpy(s3, s1);s3=ft_strcat(s3, s2); Commented Jan 13, 2015 at 15:00
  • @CoolGuy you need +1 for the NUL Commented Jan 13, 2015 at 15:01

2 Answers 2

6

You can't add two pointers together. If you want to allocate enough space for the result string, you must use strlen and add 1 for the NUL terminator

char* s3 = malloc(strlen(s1) + strlen(s2) + 1);

Think about something like this:

int* a = /*...*/;
int* b = /*...*/;
a + b; // what is the significance of this?

Adding two pointers doesn't make sense, the result is meaningless. C-strings are arrays of characters but that doesn't give them special rules as far as the pointers go. You have to use the library facilities provided, or roll your own (as it seems you are doing).

I'm not sure you know what you're doing with the calls to the cat function. The use of library str functions would be as follows:

// after the malloc
strcpy(s3, s1); // copy s1 into s3
strcat(s3, s2); // append s2 onto the end of s3

Your need for a cast in the call to you cat function is a giveaway that you're doing this wrong. Don't use casts unless you are completely sure you need them and understand why and what they are doing.

The way you have it set up will effectively append s2 onto the end of s1 and then assign s1 to s3. This will leak the block you just allocated, possibly overrun a buffer, and modify the previous contents of s1.

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

Comments

0

s1 and s2 are pointers. You can't sum pointers, you can only create their difference (which is integer).

1 Comment

@DanS Thanks! Of course, you can convert them to integer, and then you can add these integers, although I would doubt how would it help. Behind the bars, obviously pointer arithmetics maps to integer arithmetics, these restrictions are to avoid crap/unreadable code.

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.