在C中填充char *(Filling a char* in C)

编程入门 行业动态 更新时间:2024-10-28 16:16:23
在C中填充char *(Filling a char* in C)

这可能是一个非常简单的解决方案,但对于我的生活我无法弄清楚。 我试图创建一个由0到numPlayers - 1的数字组成的char数组( char* ),我将遍历它来访问它的轮到。 所以,如果numPlayers = 10 ,我想gameState.players是[0,1,2,3,4,5,6,7,8,9] 。 我做错了什么?

printf("How many players will be playing: "); scanf(" %d", &numPlayers); gameState.players = (char*) malloc(numPlayers * sizeof(char)); for (int i = 0; i < numPlayers; ++i) { strcpy(gameState.players[i],(char) i); }

This is probably a very simple solution, but for the life of me I can't figure it out. I'm trying to create a char array (so a char*) consisting of numbers from 0 to numPlayers - 1, which I will iterate through to access whose turn it is. So, if numPlayers = 10, I want gameState.players to be [0,1,2,3,4,5,6,7,8,9]. What did I do wrong?

printf("How many players will be playing: "); scanf(" %d", &numPlayers); gameState.players = (char*) malloc(numPlayers * sizeof(char)); for (int i = 0; i < numPlayers; ++i) { strcpy(gameState.players[i],(char) i); }

最满意答案

首先:

gameState.players = (char*) malloc(numPlayers * sizeof(char));

在C语言中明确的转换是不明智的(它可以隐藏某些细微的错误), sizeof(char)的乘法不需要 - 它总是一个。

真正的问题在于:

strcpy(gameState.players[i],(char) i);

str*函数用于处理C 字符串 (空终止的字符数组)。 你没有一个字符串,而是你有一个字符值,所以它应该更符合以下几行:

gameState.players[i] = i;

你还需要记住:

尽管您使用的是char变量,但所放入的值不是数字的文本表示。 为了得到这个,你需要使用i + '0' (a) 。 字符通常用于(大部分)可打印的东西,对于非字符数据,最好使用像int或unsigned short这样的更具体的数据类型。 如果你使用超过10个项目,这个方案(假设你想要文本表示)将会破坏得很厉害。

(a) “人物” 7和'7'之间有很大的区别。 前者实际上具有值7 (如果使用ASCII,则为ASCII BEL),后者具有值0x37 (再次假设ASCII / Unicode)。

数字字符是唯一保证连续的数字字符,因此您只需通过添加'0'即可将数字值0..9转换为可打印的字符值。

First off:

gameState.players = (char*) malloc(numPlayers * sizeof(char));

The explicit cast is ill-advised in C (it can hide certain subtle errors) and the multiplication by sizeof(char) is never needed - it's always one.

But the real problem lies here:

strcpy(gameState.players[i],(char) i);

The str* functions are meant to work with C strings (null terminated character arrays). You do not have a string, rather you have a character value, so it should be more along the lines of:

gameState.players[i] = i;

You also need to keep in mind:

Though you're using char variables, the value being put in is not the textual representation of the digit. To get that, you would need to use i + '0'(a). Characters are generally meant to be used for (mostly) printable stuff, you would be better off using a more-specific data type like int or unsigned short` for non-character data. This scheme (assuming you want textual representation) is going to break horribly if you ever use more than ten items.

(a) There's a big difference between the "characters" 7 and '7'. The former actually has the value 7 (ASCII BEL, if you're using ASCII), the latter has the value 0x37 (again, assuming ASCII/Unicode).

The numeric characters are the only ones guaranteed to be consecutive so you can convert a numeric value 0..9 to the printable character value simply by adding '0'.

更多推荐

本文发布于:2023-07-28 19:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:char   Filling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!