简体   繁体   English

如何在函数内部操作char数组(字符串)的指针,它在c / c ++中作为参数传递

[英]how to manipulate a pointer of char array (string) inside a function where it is passed as a parameter in c / c++

Hello i am new to C language. 你好,我是C语言的新手。 basically i am trying to pass a pointer of char array(string) to a function and inside that function i want to add one character at last of that array. 基本上我试图将一个char数组(字符串)的指针传递给一个函数,在该函数内部,我想在该数组的最后添加一个字符。 here is the code i am trying to run. 这是我想要运行的代码。

so the function i am talking about is function1 in code. 所以我所说的功能是代码中的function1。 the pointer line is one i am trying to manipulate. 指针线是我试图操纵的一个。 i am trying to add one more char to line inside the function1 by using method append. 我试图通过使用方法追加添加一个char到function1内的行。 i tried few other ways too but none of them really worked. 我也尝试了其他一些方法,但没有一个真的奏效。

it will be really helpful if someone can show me how to do it correctly. 如果有人能告诉我如何正确地做这件事,那将会非常有帮助。

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

char* function1(char* );
void append(char* , char );

int main(int argc, char const *argv[])
{
    /* code */

    function1("each line ends with : ");
    return 0;
}

char* function1(char* line)
{
    int pos = 0;
    char* longest = "";


    printf("so far working!\n");
    append(line, '$');
    // here during append something happens and all it tells is segmentation fault core dumped
    printf("not anymore.\n");

    return longest;
}

void append(char* s, char c)
{
    s[strlen(s)] = c;
    s[strlen(s)+1] = '\0';
}

The problem is that not enough space was allocated for your string - when you declare the string, you say "it will be no more than n bytes long" (including the terminating '\\0' ). 问题是没有为你的字符串分配足够的空间 - 当你声明字符串时,你说“它将不超过n个字节长”(包括终止'\\0' )。 Writing bytes in memory that doesn't belong to the string will give rise to the error you see. 在内存中写入不属于字符串的字节将导致您看到的错误。

There is no universal solution when you use C - in your example, you start out with a constant string (defined up front with "each line ends with: " ) and the compiler only allocates that much space. 当你使用C时没有通用的解决方案 - 在你的例子中,你从一个常量字符串开始(在前面定义"each line ends with: " ),编译器只分配那么多空间。 If you think you might need more space, you could do something like this: 如果您认为可能需要更多空间,可以执行以下操作:

char mySpace[101];  // allocates 101 bytes : 100 characters + terminating '\0'
strncpy(mySpace, "each line ends with: ", 100); / copy at most 100 bytes

Now you can increase the length of mySpace by one character (unlike the original string, which was a constant because of the way you created it, you can do what you want with the copy as long as you stay within the limits of the memory you allocated for it): 现在你可以将mySpace的长度增加一个字符(与原始字符串不同,由于你创建它的方式,这是一个常量,只要你保持在内存的限制范围内,你可以用副本做你想做的事情你分配给它):

int length;
length = strlen(mySpace);
mySpace[length] = '$';
mySpace[length+1] = '\0';

Does that make sense? 那有意义吗?

EDIT Just realized another problem: 编辑刚刚意识到另一个问题:

In the code snippet 在代码段中

s[strlen(s)] = c;
s[strlen(s)+1] = '\0';

In the first line you overwrite the terminating '\\0' character of the string: when you then call strlen in the second line, that function tries to find the end of line character (that no longer exists) and it runs off into the distance, all the way to a segmentation fault (which means roughly "the program ended up trying to access memory that did not belong to it"). 在第一行中,您将覆盖字符串的终止'\\0'字符:当您在第二行中调用strlen时,该函数会尝试查找行尾字符(不再存在)并且它会跑到远处,一直到分段错误(这意味着“程序最终试图访问不属于它的内存”)。 When I wrote my code snippet I had instinctively done that the right way - then I looked at your code again and saw the problem stare me in the face. 当我编写我的代码片段时,我本能地以正确的方式完成了 - 然后我再次查看了你的代码,看到问题盯着我。

When you declare a string in C "like this" , you're actually not getting a char* . 当你在C中声明一个"like this"的字符串时,你实际上并没有得到一个char* You're getting a const char* (well, not technically, but it's generally in read-only memory). 你得到一个const char* (好吧,不是技术上的,但它通常是只读内存)。 Never try to modify a char* that was declared in a string literal. 永远不要尝试修改在字符串文字中声明的char*

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

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