Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
无法连接两个char*指针值?_C_Arrays_String_Pointers - Fatal编程技术网

无法连接两个char*指针值?

无法连接两个char*指针值?,c,arrays,string,pointers,C,Arrays,String,Pointers,我只想问一个问题,如果我们有两个字符串,但它们应该在给定的代码中 #include<stdio.h> #include<stdlib.h> #include <string.h> char *getln() { char *line = NULL, *tmp = NULL; size_t size = 0, index = 0; int ch = EOF; while (ch) { ch = getc(std

我只想问一个问题,如果我们有两个字符串,但它们应该在给定的代码中

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

char *getln()
{
    char *line = NULL, *tmp = NULL;
    size_t size = 0, index = 0;
    int ch = EOF;

    while (ch) {
        ch = getc(stdin);
        if (ch == EOF || ch == '\n')
            ch = 0;
        if (size <= index) {
            size += index;
            tmp = realloc(line, size);
            if (!tmp) {
                free(line);
                line = NULL;
                break;
            }
            line = tmp;
        }
        line[index++] = ch;
    }

    return line;
}
char * combine(char *a,char *buffer){
   int stop_var;
   int newSize = strlen(a)  + strlen(buffer) + 1; 
   char * newBuffer = (char *)malloc(newSize);
   strcpy(newBuffer,a);
   strcat(newBuffer,buffer); // or strncat
   //printf("Final String %s",newBuffer);
   free(a);
   a = newBuffer;
   return newBuffer;
}
char * fun(char *str)
{
    char *str1;
    str1=getln();
    str=combine(str,str1);
    return str;
}
int main(void)
{
    char *str;
    str= getln();
    printf("Initial String %s \n",str);
    str=fun(str);
    printf("Final String %s \n",str);
}
所以,我想知道,在上面的情况下,字符串是固定的,是否还有其他方法可以运行。我知道“我们如何在同一个字符指针中实现最终值”

请参阅附注: 与这个问题相关的问题很多,我已经尝试了所有的解决方案。 我看了下面的解决方案

  • 我在互联网上搜索了更多的解决方案,但没有一个解决方案符合我的条件。所有这些解决方案都使用第三个变量并显示其值。我希望值在同一个变量中。即使我们正在创建任何额外的变量,它的值最终也应该分配给
    char*str

    请提供我任何建议,这样做在c语言只


    与我的问题不同,因为在这个问题中,他们检查字符串文字的行为。在我的例子中,我希望为变量str分配连接值。其解决方案更改指向数组的指针,但我无法更改,因为它的值在我的工作中使用了许多函数

    函数
    combine()
    在其第一个参数上调用
    free()
    。在
    fun()
    调用中,它是从指向代码中的字符串文本的
    fun()
    传递的


    不能对字符串文本调用
    free()
    ,这是未定义的行为。我建议您重新构造代码,使
    combine()
    不再在其第一个参数上调用
    free()
    。释放内存应该是调用者的责任。

    我建议使用成对的分配/解除分配函数:

    char * combine_allocate(const char *a, const char *b) {
        char* result = malloc(...)
        combine ...
        return result;
    }
    
    void combine_deallocate(char* p) { free(p); }
    
    拥有:

    char* a = initial_allocate();
    char* b = initial_allocate();
    char* c = combine_allocate(a, b);
    initial_deallocate(a);
    initial_deallocate(b);
    // ... use c
    combine_deallocate(c)
    
    您可以省略(并且应该)字符串文本的initial\u allocate/initial\u deallocate。
    这可能有点太冗长了,但面向对象的C没有做其他事情(有更好的函数名)。

    strcat()实际上有什么问题?@NathanOliver为什么这是重复的?这与修改字符串文字没有直接关系(只是通过
    free()
    )间接进行)。而且您已经广泛使用realloc,那么为什么要使用combine呢?只需获取您的输入字符串,realloc第一个字符串足够大,可以同时容纳这两个字符串,然后strcat…如果我从combine函数中删除了“free()”,那么代码就会抛出分段错误。如果有任何其他直接或间接修改文本的方式,请建议我。@KrishnasaiGudavalli您不能修改字符串文本。没有办法做到这一点。如果你想要写的东西,你可以创建一个数组并用字符串文字来初始化它。好的,如果我考虑你的情况,那么对于固定字符串也不起作用。感谢您的宝贵建议此解决方案不是通用解决方案,因为它依赖于编译器,因此不适用于较旧的编译器版本。@KrishnasaiGudavalli这是老式的,太好了。请给我发送工作代码,它可以满足我更改字符串文字的要求。@KrishnasaiGudavalli-No@KrishnasaiGudavalli编写自己的代码。堆栈溢出不是代码编写服务。问“你能给我发代码吗”是解决问题的最简单方法。
    char* a = initial_allocate();
    char* b = initial_allocate();
    char* c = combine_allocate(a, b);
    initial_deallocate(a);
    initial_deallocate(b);
    // ... use c
    combine_deallocate(c)