简体   繁体   中英

Write strcat() function with pointers

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.

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;
}
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;
}

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);

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;
}

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;    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM