将字符复制到字符*

发布于 2024-12-03 00:54:18 字数 673 浏览 0 评论 0原文

我有一个关于 C 编程的问题。 我有一个循环,在每次迭代时生成一个新字符。我想将每个生成的字符附加到字符串(char *) 这是我的代码:

char *string[20];
string[0]=0;

for (p = 0; p < length(message) - 1; p = p + 2)
{
...
char cc="";
cc = (char) strtol(pp, pp, 16);
char *tt[2];
tt[0]="";
*tt=(char *)cc;
strcat(string,cc);
}

我知道 strcat 使用 char*。所以我尝试复制我想要附加到新指针的字符的内容。 我可能做错了……我经验不足。 谢谢

strtol 的原因: 我有一个 char* 消息,其中包含一个十六进制数字。我想处理该十六进制数,但是当我尝试执行例如移位 (<<16) 时,会将十六进制字符的十进制 ascii 值移位 16 位(乘以 2^16 ),而不是移位字符本身。 因此,我将十六进制数字转换为字符,这样当我尝试进行移位时,它将移位正确的值。我知道这很奇怪。 你能帮我想出更好的办法吗?

unsigned char *octets0"3F214365876616AB15387D5D59";
crc ^= ((*octets++) << 16);

I have a question for C programming.
I have a loop where I generate at each iteration a new char. Each generated char I want to append to a string (char *)
This is my code:

char *string[20];
string[0]=0;

for (p = 0; p < length(message) - 1; p = p + 2)
{
...
char cc="";
cc = (char) strtol(pp, pp, 16);
char *tt[2];
tt[0]="";
*tt=(char *)cc;
strcat(string,cc);
}

I know that strcat uses char*. So I tried to copy the content of the char i wanna append to a new pointer.
I have probably done sth wrong...i'm not very experienced.
Thank you

Reason for strtol:
I have a char* msg, that holds a hexa number. I want to process that hexa number but when I try to do for instance shifting (<<16) is shifts 16 bits (multiplies by 2^16 ) the decimal ascii value of the hexa character, instead of shifting the character itself.
So, I converted the hexa number to a character, so that when I try to do shifting it will shift the correct value. I know it' wierd.
Cann you help me come up with sth better?

unsigned char *octets0"3F214365876616AB15387D5D59";
crc ^= ((*octets++) << 16);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2024-12-10 00:54:18

几个问题。

第一个问题是您创建了一个 char 指针数组,而不是一个 char 数组。您想要:

char string[20];

在 C 中,数组隐式“降级”为指向其第一个元素的指针,因此如果您仅传递 string 它就是一个 char* ( string = = &string[0]

从那里...不知道 pp 是什么,但是 strtol() 返回一个 long int 您试图分配给char - 这不会像你想象的那样工作。你少了大约7个字节。

(编辑为“工作”定义为“不是你所想的”)

Several issues.

First problem is that you've created an array of char pointers, not a char array. You want:

char string[20];

In C, an array implicitly "degrades" into a pointer to its first element, so if you pass just string it's a char* ( string == &string[0] )

From there ... no idea what pp is, but strtol() returns a long int which you're trying to assign to a char - that's not going to work the way you think. You're short about 7 bytes.

(Edited to define "work" as in "Not what you think")

┾廆蒐ゝ 2024-12-10 00:54:18

尽量不要在 char*char 之间进行转换——它不会做任何有用的事情,而且你的编译器无论如何也不应该让你这样做。如果你真的想使用 strcat,那应该是

char cc; // no ="" here!
...
tt[0] = cc;
strcat(string, tt)

但是按照 cnicutar 上面发布的内容会容易得多(我也打算在这里输入类似的内容)。

Try not to cast between char* and char – it does not do anything useful and your compiler should not let you do that, anyway. If you really want to use strcat, that should be

char cc; // no ="" here!
...
tt[0] = cc;
strcat(string, tt)

But it would be much easier to just do what cnicutar posted above (I was going to type something like that here, too).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文