简体   繁体   English

我可以为字符指针数组分配一个整数吗?

[英]Can i assign a integer to a character pointer array?

int k=5;
char* result = (char *)malloc(100 * sizeof(char));
result[count] = k;

Considering the above code, if I print the contents of result[count] it prints smiley symbols.考虑到上面的代码,如果我打印result[count]的内容,它会打印笑脸符号。 I tried like below,but of no use.我试过如下,但没有用。

result[count]=(char)k;

Can anyone help me?谁能帮我?

I am percepting that malloc(100*sizeof(char)) will create 100 blocks each of size of character contiguously.我认为malloc(100*sizeof(char))将连续创建100个字符大小的块。 Please correct me if I am wrong?如果我错了,请纠正我?

I'll disregard all the fluff about arrays and malloc as it is irrelevant to the problem you describe.我将忽略有关数组和 malloc 的所有内容,因为它与您描述的问题无关。 You seem to essentially be asking "What will this code print:"您似乎本质上是在问“此代码将打印什么:”

char c = 5; 
printf("%c", c);

It will print the symbol 5 in your symbol table, most likely a non-printable character.它将在您的符号表中打印符号 5,很可能是一个不可打印的字符。 When encountering non-printable characters, some implementations choose to print non-standard symbols, such as smileys.当遇到不可打印的字符时,一些实现选择打印非标准符号,例如笑脸。

If you want to print the number 5, you have to use a character literal:如果要打印数字 5,则必须使用字符文字:

char c = '5';
printf("%c", c);

or alternatively use poor style with "magic numbers":或者使用带有“幻数”的糟糕风格:

char c = 53;   // don't write code like this
printf("%c", c);

It is a problem of character representation.这是字符表示的问题。 Let's begin by the opposite first.让我们先从相反的开始。 A char can be promoted to an int , but if you do you get the representation of the character (its code ).可以将char提升为int ,但如果这样做,您将获得字符的表示(其code )。 Assuming you use ASCII:假设您使用 ASCII:

char c = '5';
int i = c; // i is now 0x35 or 53 ASCII code for '5'

Your example is the opposite, 5 can be represented by a char, so result[count] = k is defined (even if you should have got a warning for possible truncation), but ASCII char for code 5 is control code ENQ and will be printed as on a windows system using code page 850 or 437.您的示例正好相反,5 可以用字符表示,因此定义了result[count] = k (即使您应该收到可能被截断的警告),但代码 5 的 ASCII 字符是控制代码 ENQ,将是在 Windows 系统上使用代码页 850 或 437 打印为

A half portable (not specified, but is known to work on all common architectures) way to do the conversion (int) 5 -> (char) '5' is to remember that code '0' to '9' are consecutive (as are 'A' to 'Z' and 'a to 'z'), so I think that what you want is:进行转换(int) 5 -> (char) '5'的半可移植(未指定,但已知适用于所有常见体系结构)方法是记住代码 '0' 到 '9' 是连续的(如是'A'到'Z'和'a到'z'),所以我认为你想要的是:

result[count] = '0' + k;

First, let's clean up your code a bit:首先,让我们稍微清理一下代码:

int k=5;
char* result = malloc(100);
result[count] = k;

sizeof(char) is always equal to 1 under any platform and you don't need to cast the return of malloc() in C. sizeof(char) 在任何平台下总是等于 1 并且你不需要在 C 中转换 malloc() 的返回值。

This said, as long as your count variable contains a value between 0 and 99, you're not doing anything wrong.这就是说,只要您的count变量包含 0 到 99 之间的值,您就没有做错任何事情。 If you want to print the actual character '5', then you have to assign the ASCII value of that character (53), not just the number 5. Also, remember that if that array of chars has to be interpreted as a string, you need to terminate it with '\\0':如果要打印实际字符 '5',则必须分配该字符的 ASCII 值 (53),而不仅仅是数字 5。另外,请记住,如果必须将该字符数组解释为字符串,你需要用 '\\0' 终止它:

int k=5, i;
char* result = malloc(100);
for (i=0; i<98; i++) {
    result[i] = ' ';
}
result[98] = 53;
result[99] = '\0';
printf("%s\n", result);

The elements in the array you're trying to allocate are all the size of a char, 1 byte.您尝试分配的数组中的元素都是一个字符的大小,1 个字节。 An int is 4 bytes thus your issues. int 是 4 个字节,因此您的问题。 If you want to store ints in an array you could do:如果要将整数存储在数组中,可以执行以下操作:

int result[100];

Followed by storing the value, but honestly from the way your questioned is posed I don't know if that's actually what you're trying to do.其次是存储价值,但老实说,从你提出问题的方式来看,我不知道这是否真的是你想要做的。 Try rephrasing the post with what you're trying to accomplish.尝试用您想要完成的内容重新表述帖子。

Are you trying to store 5 as a character?您是否尝试将 5 存储为一个字符?

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

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