如何编辑以下某个字符? [重复]

时间:2023-01-25 21:41:04

This question already has an answer here:

这个问题在这里已有答案:

So I have the following code:

所以我有以下代码:

char *something = (char *) calloc(LENGTH, sizeof(char));

The length is defined as 10. I'm imaging it like this in memory:

长度定义为10.我在内存中像这样成像:

| [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | \0 |

How would I change [1] without defining the whole char? And then be able to define [2], and so on...

如何在不定义整个字符的情况下更改[1]?然后能够定义[2],依此类推......

Each change must not affect the previous change!

每次更改都不得影响之前的更改!

Thank you!

谢谢!

2 个解决方案

#1


1  

The length is defined as 10. I'm imaging it like this in memory

长度定义为10.我在内存中像这样对它进行成像

Incorrect. First, there are only 10 bytes (your picture shows 11) and second, all of them are filled with '\0' (which calloc() does).

不正确。首先,只有10个字节(你的图片显示11个),第二个,所有这些都填充'\ 0'(calloc()会填充)。

How would I change [1] without defining the whole char? And then be able to define [2], and so on...

如何在不定义整个字符的情况下更改[1]?然后能够定义[2],依此类推......

By "change" if you mean assigning values then you can index them like:

通过“更改”,如果您的意思是指定值,那么您可以将它们索引为:

something[1] = 'a';
something[5] = 'q';

and so on.

等等。

But do remember, using it as a C-string may not work (for example, printing something using printf("%s", something);) since there are intermediate zero bytes.

但请记住,将它用作C字符串可能不起作用(例如,使用printf(“%s”,某些东西)打印内容;)因为存在中间零字节。

#2


1  

Your code

你的代码

char *something = (char *) calloc(LENGTH, sizeof(char));

gives you 10 (not 11) bytes, all initialised to 0.

给你10个(不是11个)字节,全部初始化为0。

You can change any byte in here you want

您可以在此处更改所需的任何字节

something[1] ='?';

If you use standard routine to e.g. printf this it will of course then find '0' in the first byte and interpret it as the end of a string.

如果您使用标准例程来例如printf这当然会在第一个字节中找到'0'并将其解释为字符串的结尾。

Don't forget to free it when you are done

完成后不要忘记释放它

free(something);

#1


1  

The length is defined as 10. I'm imaging it like this in memory

长度定义为10.我在内存中像这样对它进行成像

Incorrect. First, there are only 10 bytes (your picture shows 11) and second, all of them are filled with '\0' (which calloc() does).

不正确。首先,只有10个字节(你的图片显示11个),第二个,所有这些都填充'\ 0'(calloc()会填充)。

How would I change [1] without defining the whole char? And then be able to define [2], and so on...

如何在不定义整个字符的情况下更改[1]?然后能够定义[2],依此类推......

By "change" if you mean assigning values then you can index them like:

通过“更改”,如果您的意思是指定值,那么您可以将它们索引为:

something[1] = 'a';
something[5] = 'q';

and so on.

等等。

But do remember, using it as a C-string may not work (for example, printing something using printf("%s", something);) since there are intermediate zero bytes.

但请记住,将它用作C字符串可能不起作用(例如,使用printf(“%s”,某些东西)打印内容;)因为存在中间零字节。

#2


1  

Your code

你的代码

char *something = (char *) calloc(LENGTH, sizeof(char));

gives you 10 (not 11) bytes, all initialised to 0.

给你10个(不是11个)字节,全部初始化为0。

You can change any byte in here you want

您可以在此处更改所需的任何字节

something[1] ='?';

If you use standard routine to e.g. printf this it will of course then find '0' in the first byte and interpret it as the end of a string.

如果您使用标准例程来例如printf这当然会在第一个字节中找到'0'并将其解释为字符串的结尾。

Don't forget to free it when you are done

完成后不要忘记释放它

free(something);