繁体   English   中英

C编程strcat使用指针

[英]C programming strcat using pointer

我是C的初学者。我想用指针创建strcat函数。 我做到了,但不知道它有什么问题。 我使用gcc编译器,它给出了分段错误输出。

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

char scat(char *,char *);

void main()
{
    char *s="james";
    char *t="bond";

    char *q=scat(s,t);
    while(*q!='\0') printf("the concatenated string is %c",*q);
}

char *scat(char *s,char *t)
{
    char *p=s; 
    while(*p!='\0'){
        p++;
    } 
    while(*t!='\0'){
        *p=*t;
        p++;
        t++;
    }
    return p-s-t;
}

这个工作:

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

char *scat(char *,char *);                 /* 1: your prototype was wrong */

void main()
{
    char *s="james";
    char *t="bond";

    char *q=scat(s,t);   
    printf("cat: %s\n", q);               /* 2: you can use %s to print a string */
    free(q);
}

char *scat(char *s,char *t)
{
    char *p=malloc(strlen(s)+strlen(t)+1);    /* 3: you will have to reserve memory to hold the copy. */
    int ptr =0, temp = 0;                   /* 4 initialise some helpers */

    while(s[temp]!='\0'){                  /* 5. use the temp to "walk" over string 1 */
        p[ptr++] = s[temp++];
    }
    temp=0;
    while(t[temp]!='\0'){                   /* and string two */
        p[ptr++]=t[temp++];
    }
    return p;
}

你必须在s的末尾分配新的空间来复制。 否则,你的厕所[将进入内存,你没有访问权限。

你应该在这里了解malloc()

修改字符串文字和s是未定义的行为,并且最终p指向字符串文字:

char* s = "james";

s作为第一个参数传递给分配了本地char* p scat() ,然后:

*p=*t;

在第一次调用时,它试图将空字符覆盖在字符串文字"james"的末尾。

一个可能的解决方案是使用malloc()来分配足够大的缓冲区来包含两个输入字符串的连接:

char* result = malloc(strlen(s) + strlen(p) + 1); /* + 1 for null terminator. */

并将它们复制到其中。 调用者必须记住free()返回的char*

您可能会发现常用的指针问题列表很有用。

因为p一直持续到字符串的末尾,然后它开始前进到非法内存。 这就是你得到分段错误的原因。

这是因为s指向“james \\ 0”,字符串文字并且你不能修改常量。

改变char *s="james"; to char s[50]="james";

您需要了解指针的基础知识。

char *不是字符串或字符数组,它是数据开头的地址。

你不能做一个char * - char * !!

这是一个很好的教程

你将不得不使用malloc

你得到一个分段错误,因为你将指针移动到s的末尾然后开始直接在sp的数据写入内存。 是什么让你相信s后有可写的内存? 将数据写入不可写内存的任何尝试都会导致分段错误,并且看起来s后面s内存不可写(这是预期的,因为“字符串常量”通常存储在只读内存中)。

有些事情看起来不合时宜。

首先请记住,当你想要返回指向函数内创建的东西的指针时,它需要在某处进行malloc。 如果将目标作为参数传递给函数,则会容易得多。 如果您遵循前一种方法,请不要忘记在完成后将其free()

此外,函数scat必须返回声明中的指针,即char *scat ,而不是char scat

最后你不需要那个循环来打印字符串printf("%s", string); 将为您打印字符串(如果它已终止)。

首先,由于以下行,您的代码将处于infinte循环中。 你应该通过包含“p ++; t ++”语句来使用实心大括号。

while(*t!='\0')
 *p=*t;

虽然你喜欢这样,但是你试图改变字符串文字的内容。 这将导致不确定的行为,如分段错误。

用双引号括起来的字符序列称为字符串文字。 它也被称为“字符串”。 字符串的大小是固定的。 创建后,您无法扩展其大小并更改内容。 这样做会导致未定义的行为。

要解决此问题,您需要分配一个新的字符数组,其大小是传递的两个字符串长度的总和。 然后将两个字符串附加到新数组中。 最后返回新数组的地址。

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

char* scat(char *,char *);
void append(char *t , char *s);

int main(void)
{
    char *s="james";
    char *t="bond";

    char *n = scat(s,t);        
    printf("the concatenated string is %s",n);

    return 0;
}

char* scat(char *s,char *t)
{
    int len = strlen(s) + strlen(t);
    char *tmp = (char *)malloc(sizeof(char)* len);

    append(tmp,s);
    append(tmp,t);

    return tmp;
} 


void append(char *t , char *s)
{   
     //move pointer t to end of the string it points. 
    while(*t != '\0'){
        t++;
    }

    while( *s != '\0' ){
        *t = *s;
        t++;
        s++;    
    }       
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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