如何将实际角色存储到数组中?

时间:2021-12-18 21:31:27

So I want to know how to store an actual character into an array.

所以我想知道如何将实际字符存储到数组中。

    char arr[4] = "sup!";
    char backwards[4];
    backwards[0] = *(arr + 3);

I guess my second question is if I do this, will, if i prompt a printf of backwards[0] using %c, will the actual character appear?

我想我的第二个问题是如果我这样做,如果我使用%c提示向后[0]的printf,是否会显示实际字符?

1 个解决方案

#1


2  

First off, let's fix your array size problem:

首先,让我们修复你的数组大小问题:

Character arrays in C are null-terminated: This means that you need to add space for a trailing \0 to terminate your string. You could use char arr[5] on the first line. (Note that if you are ABSOLUTELY CERTAIN you are NEVER going to use this array of characters with any of the C string handling functions, AND you assigned your characters individual as chars instead of as a string, this is not technically required. But save yourself some debugging time and use up the extra byte.) This is probably the source of your "weird error."

C中的字符数组以空值终止:这意味着您需要为尾随\ 0添加空格以终止字符串。你可以在第一行使用char arr [5]。 (请注意,如果你绝对是某些人,你永远不会在任何C字符串处理函数中使用这个字符数组,并且你将字符分配为字符而不是字符串,这在技术上是不必要的。但是请保存自己一些调试时间并耗尽额外的字节。)这可能是你“奇怪的错误”的根源。

It seems like you know, but the other thing to bear in mind is in C, arrays are zero-based. This means that when you declare and array like char arr[4], you really get

看起来你知道,但要记住的另一件事是在C中,数组是从零开始的。这意味着当你声明和数组像char arr [4]时,你真的得到了

arr[0]
arr[1]
arr[2]
arr[3]

C has no qualms about letting you walk off the end of your array and stomp on data or read in bad values. Here be dragons. Be careful.

C对于让你走出数组末尾并踩踏数据或读取错误值没有任何疑虑。这里是龙。小心。

Now, on to your actual questions:

现在,关于你的实际问题:

1) You assign actual characters using single quotes arr[2]='x'; If you use double quotes, you are assigning a C string, which is null-terminated, as discussed.

1)使用单引号arr [2] ='x'分配实际字符;如果使用双引号,则会分配一个C字符串,该字符串以空值终止,如上所述。

2) Yes, printf with %c should do the trick.

2)是的,带有%c的printf应该可以解决问题。

#1


2  

First off, let's fix your array size problem:

首先,让我们修复你的数组大小问题:

Character arrays in C are null-terminated: This means that you need to add space for a trailing \0 to terminate your string. You could use char arr[5] on the first line. (Note that if you are ABSOLUTELY CERTAIN you are NEVER going to use this array of characters with any of the C string handling functions, AND you assigned your characters individual as chars instead of as a string, this is not technically required. But save yourself some debugging time and use up the extra byte.) This is probably the source of your "weird error."

C中的字符数组以空值终止:这意味着您需要为尾随\ 0添加空格以终止字符串。你可以在第一行使用char arr [5]。 (请注意,如果你绝对是某些人,你永远不会在任何C字符串处理函数中使用这个字符数组,并且你将字符分配为字符而不是字符串,这在技术上是不必要的。但是请保存自己一些调试时间并耗尽额外的字节。)这可能是你“奇怪的错误”的根源。

It seems like you know, but the other thing to bear in mind is in C, arrays are zero-based. This means that when you declare and array like char arr[4], you really get

看起来你知道,但要记住的另一件事是在C中,数组是从零开始的。这意味着当你声明和数组像char arr [4]时,你真的得到了

arr[0]
arr[1]
arr[2]
arr[3]

C has no qualms about letting you walk off the end of your array and stomp on data or read in bad values. Here be dragons. Be careful.

C对于让你走出数组末尾并踩踏数据或读取错误值没有任何疑虑。这里是龙。小心。

Now, on to your actual questions:

现在,关于你的实际问题:

1) You assign actual characters using single quotes arr[2]='x'; If you use double quotes, you are assigning a C string, which is null-terminated, as discussed.

1)使用单引号arr [2] ='x'分配实际字符;如果使用双引号,则会分配一个C字符串,该字符串以空值终止,如上所述。

2) Yes, printf with %c should do the trick.

2)是的,带有%c的printf应该可以解决问题。