简体   繁体   中英

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. 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. here is the code i am trying to run.

so the function i am talking about is function1 in code. 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. 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' ). 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. 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):

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"). 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* . You're getting a const char* (well, not technically, but it's generally in read-only memory). Never try to modify a char* that was declared in a string literal.

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