Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用指针编写strcat()函数_C - Fatal编程技术网

使用指针编写strcat()函数

使用指针编写strcat()函数,c,C,我对C语言上的指针很陌生,我正在尝试编写一个类似于strcat()的函数,但没有使用它。我开发了以下功能: 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

我对C语言上的指针很陌生,我正在尝试编写一个类似于strcat()的函数,但没有使用它。我开发了以下功能:

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  
}
  printf("\Concatenazione: %c", cat(A,B));

它现在起作用了,因为最终的结果是一个奇怪的角色。如何修复该函数?这是完整的main。

c
是一个局部变量。它仅存在于功能cat中。您应该使用
malloc

而不是

charc[h]

使用

char*c=malloc(h)

此外,还应在末尾添加空字节。请记住,C中的字符串以null结尾

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

最后:

c[h-1]='\0'


cat的签名应为
char*cat(char*a,char*b)

返回的是指向字符串的指针,而不是实际字符串本身。您需要将返回类型更改为类似“char*”(或类似的内容)。您还需要确保以null结尾字符串(附加“\0”),以便正确打印

根据我自己的建议(同时也找到了另一个bug,即第二个for循环没有在正确的索引上循环),您最终得到了以下程序:

#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;
}
#包括
字符*cat(字符*a,字符*b){
int i=0,j=0;
int cont=0;
inth=strlen(a)+strlen(b)+1;
char*result=(char*)malloc(h*sizeof(char));
对于(i=0;i
从您的实现来看,您的strcat版本似乎与标准版本不兼容,因为您希望为结果分配内存,而不是期望调用方为您提供足够的内存以适应连接的结果

您的代码有几个问题:

  • 您需要返回
    char*
    ,而不是
    char
  • 您需要使用
    malloc
    动态分配内存;不能返回本地分配的数组
  • 您需要为空终止符添加
    1
  • 您需要将空终止符写入结果
  • 您可以将这两个参数作为
    const char*
  • 您可以通过使用指针而不是索引来简化函数,但这部分是可选的
以下是如何进行修复:

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

您将得到一个错误

期望常数表达式

对于代码行
charc[h]。相反,您应该使用
malloc
在运行时分配任何动态内存,如:

char* c ;
c = malloc( h + 1 ) ; // +1 for the terminating null char
// do stuff
free( c ) ;
您更正的代码::

#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;    
}
#包括
#包括
#包括
#包括
字符*cat(字符*a,字符*b){
int i=0,cont=0,h=strlen(a)+strlen(b),j;
char*c;
c=malloc(h+1);

对于(i;我不返回
char
…听编译器的警告;gcc说,return从指针生成整数而不进行强制转换。不能从返回
char
的函数返回字符数组。我不明白为什么5个答案中有4个使用
malloc
strcat
将源代码复制到dest并返回dest、 malloc不应该在这里应用。@Duck我同意,在调用函数之前应该声明
char
数组,并且应该通过指针传递给函数。@Marco谢谢!(当你看到这样愚蠢的错误时,请随意编辑)是的,我必须使用CIf,如果你通过了目的地,那么你就不需要返回。但是这个问题的想法(在我的理解中)是将两个字符串连接成一个新字符串,这不是它所做的。这实际上编辑了传递到例程中的第一个字符串!@drew_w,也许吧。OTOH OP说他想重新创建
strcat
。OTOH他的实现或者是故意创建了其他东西(一个新字符串)或者他的尝试出了差错,大多数答案都遵循了这一目标。这个答案最接近于strcat的功能和预期行为。
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;
}