简体   繁体   English

修改一个字符指针字符串数组(char * string[])

[英]Modify a character pointer string array (char * string[])

I've been giving an... interesting... task.我一直在给一个……有趣的……任务。 I've been asked to modify a character array using a pointer.我被要求使用指针修改字符数组。

I'm aware that:我知道:

*char = "something"; *char = "某事"; //is a literal and cannot be modified. // 是文字,不能修改。

char[] = "something"; char[] = "某事"; //can be modified //可以修改

But what about:但是关于:

main.c
------

static char* stringlist01[] = 
{
    "thiy iy a ytring",
    "this is also a string, unlike the first string",
    "another one, not a great one, and not really like the last two.",
    "a third string!",
    NULL,
};

int main()
{
    int ret = 9999;

    printf("////TEST ONE////\n\n");

    ret = changeLetterInString(stringlist01[0]);
    printf("Return is: %d\n\n", ret);

    printf("Stringlist01[0] is now: %s\n", stringlist01[0]);
}

AND

changeLetterInString.c
----------------------

int changeLetterInString(char *sentence)
{    
    if (sentence != NULL)
    {
        // TODO
        // Need to change "thiy iy a ytring"
        // into "this is a string"
        // and save into same location so
        // main.c can see it.
    }

    success = 0;    // 0 is Good
    return success;
}

So far, I've tried:到目前为止,我已经尝试过:

    for (char* p = sentence; *p; ++p)
    {
        if (*p == 'y')
        {
            *p = 's';
        }
    }

And I've tried:我试过了:

sentence[0] = 't'
sentence[1] = 'h'
sentence[2] = 'i'
sentence[3] = 's'   // and so on...

But neither work.但两者都不起作用。

Any help and/or insight would be greatly appreciated.任何帮助和/或见解将不胜感激。

There are subtle differences between the two notations:两种表示法之间存在细微差别:

char string[] = "Hello World";
// vs
char* string = "Hello World";

They look similar enough, right?它们看起来很相似,对吧? However they are different.然而它们是不同的。 The first one is an array of characters, whereas the second one is a pointer to an array of characters.第一个是字符数组,而第二个是指向字符数组的指针。

By default, any string literal will always be const .默认情况下,任何字符串文字都将始终const No way around it.没有办法解决它。 Trying to modify a string literal will usually result in a segfault .尝试修改字符串文字通常会导致segfault

Your code contains an array of pointers to an array of characters.您的代码包含一个指向字符数组的指针数组。 Since you contain pointer references to constant string literals, you cannot modify them.由于您包含对常量字符串文字的指针引用,因此您无法修改它们。 In order to modify them, you need to convert them into an array instead, like how it's done in the first example.为了修改它们,您需要将它们转换为数组,就像在第一个示例中所做的那样。 This will transform the string literal to an array of characters, that can be modified, rather than being a pointer to memory which cannot be modified.这会将字符串文字转换为可以修改的字符数组,而不是指向无法修改的 memory 的指针。

char strs[][] = {
    "Hello",
    "World"
}; // transforms the string literals into character arrays

strs[0][0] = 'X'; // valid

Whereas this would not compile, and caused undefined behaviour而这不会编译,并导致未定义的行为

char* strs[] = {
    "Hello",
    "World",
};

strs[0][0] = 'X'; // trying to modify a pointer to a constant string literal, undefined

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

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