0

I am new with pointers on C and I am trying to write a function like strcat() but without using it. I developed the following function:

char cat(char *a, char *b) {
 int i=0,cont=0,h=strlen(a)+strlen(b);
 char c[h]; //new string containing the 2 strings (a and b)

 for(i;i<strlen(a);++i) {
  c[i] = *(a+i); //now c contains a      
 }

 int j = i;

 for(j;j<strlen(b);++j) {
  c[j] = *(b+cont); //now c contains a + b
  cont++;       
 }

 return c; // I return c  
}

And this is how I call the function:

  printf("\Concatenazione: %c", cat(A,B));

It is now working because the final result is a weird character. How could I fix the function? Here there's the full main.

4
  • 1
    strcat doesn't return a char... Commented Dec 10, 2013 at 13:35
  • Listen to your compiler's warnings; gcc says, return makes integer from pointer without a cast. You cannot return a character array from a function that returns char. Commented Dec 10, 2013 at 13:36
  • 1
    I do not understand why 4 of 5 answers are using malloc. strcat copies source to dest and returns dest. malloc should not apply here. Commented Dec 10, 2013 at 14:09
  • @Duck i agree, the char array should be declared before calling the function and should be passed to the function by a pointer. Commented Dec 10, 2013 at 14:42

5 Answers 5

2
char * strcat(char *dest, const char *src)
{
    int i;
    int j;

    for (i = 0; dest[i] != '\0'; i++);
    for (j = 0; src[j] != '\0'; j++) {
        dest[i+j] = src[j];
    }

    dest[i+j] = '\0';

    return dest;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you are passing dest in, then you don't need to return it. But the idea of this question (in my understanding) was to concatenate two strings into a new string, which is not what this does. This actually edits the first string passed into the routine!
@drew_w, maybe. OTOH OP said he wanted to recreate strcat. OTOH his implementation either purposely created something else (a new string) or his attempt just went awry and most of the answers followed that objective. This answer comes closest to what strcat does and the behavior expected.
2

From your implementation it appears that your version of strcat is not compatible with the standard one, because you are looking to allocate memory for the result, rather than expecting the caller to provide you with enough memory to fit the result of concatenation.

There are several issues with your code:

  • You need to return char*, not char
  • You need to allocate memory dynamically with malloc; you cannot return a locally allocated array.
  • You need to add 1 for the null terminator
  • You need to write the null terminator into the result
  • You can take both parameters as const char*
  • You can simplify your function by using pointers instead of indexes, but that part is optional.

Here is how you can do the fixes:

char *cat(const char *a, const char *b) {
    int i=0,cont=0,h=strlen(a)+strlen(b);
    char *c = malloc(h+1);
    // your implementation goes here
    c[cont] = '\0';
    return c;
}

2 Comments

@Marco Thanks! (Feel free to edit when you see such stupid mistakes).
Yeah, I have to use C
1

You are returning a POINTER to the string, not the actual string itself. You need to change the return type to something like "char *" (or something equivalent). You also need to make sure to null terminate the string (append a '\0') for it to print correctly.

Taking my own advice (and also finding the other bug, which is the fact that the second for loop isn't looping over the correct indices), you end up with the following program:

#include <stdio.h>

char *cat(char *a, char *b) {
  int i = 0, j = 0;
  int cont = 0;
  int h = strlen(a) + strlen(b) + 1;

  char *result = (char*)malloc(h * sizeof(char));

  for(i = 0; i < strlen(a); i++) {
    result[i] = a[i];
  }

  for(j = i; j < strlen(b)+ strlen(a); j++) {
    result[j] = b[cont++];
  }

  // append null character
  result[h - 1] = '\0';
  return result;
}

int main() {
   const char *firstString = "Test First String. ";
   const char *secondString = "Another String Here.";
   char *combined = cat(firstString, secondString);

   printf("%s", combined);

   free(combined);
   return 0;
}

Comments

1

c is a local variable. It only exists inside the function cat. You should use malloc.

instead of

char c[h];

use

char *c = malloc(h);

Also, you should add the null byte at the end. Remember, the strings in C are null-ended.

h = strlen(a) + strlen(b) + 1;

and at the end:

c[h - 1] = '\0';

The signature of cat should be char *cat(char *a, char *b);

Comments

1

You will get an error of

expected constant expression

for the code line char c[h];. Instead you should be using malloc to allocate any dynamic memory at run-time like::

char* c ;
c = malloc( h + 1 ) ; // +1 for the terminating null char
// do stuff
free( c ) ;

Your corrected code::

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

char* cat(char *a, char *b) {
 int i=0,cont=0,h=strlen(a)+strlen(b), j;
 char *c;

 c = malloc( h+1 ) ;
 for(i;i<strlen(a);++i) {
  c[i] = *(a+i);      
 }

 j = 0 ;

 for(j;j<strlen(b);++j) {
  c[i] = *(b+cont);
  i++ ;
  cont++;      
 }

 c[i] = 0 ;

 return c;    
}

int main() {

  char A[1000],B[1000];
  char * a ;

  printf("Inserisci la stringa 1: \n");
  gets(A);
  printf("Inserisci la stringa 2: \n");
  gets(B);
  a = cat(A,B) ;
  printf("\nConcatenazione: %s", a);

  free(a) ;

 getch();
 return 0;    
}

Comments

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.