如何在C中更新char*数据类型的全局变量值

huangapple go评论58阅读模式
英文:

How to update my global variable value in C for data type Char *

问题

我有一个全局变量 ->

static char *password = "superman";

和一个局部变量 ->

const char *wifipsk = "batman";

如何在C中将我的全局变量密码从superman更新为batman。谢谢。

英文:

I have a global variable ->

static char *password = "superman";

and a local variable ->

const char *wifipsk = "batman";

How can I update my global variable password from superman to batman in C. Thank you.

答案1

得分: 2

这段代码中包含的问题可能比你想象的要多得多!

首先:你将字符串文字赋值给了非const指针。虽然从技术上讲,这是合法的,因为字符串文字的类型是char数组(起源于const关键字还不存在的时代,当引入时,C的设计者选择不破坏现有的代码),但它们仍然是不可变的,写入它们会导致未定义的行为。因此,你应该始终将字符串文字只分配给const指针,而不是其他任何东西:

static char const* password = "superman";

如果你想用其他文字替换这个密码,可以通过简单的赋值来实现:

password = wifipsk;

不过,我假设你希望能够通过在运行时检索的字符串来更改密码,例如通过用户输入。

然后迅速出现了一个问题:如何管理所需的内存?

最稳定的方式是将密码存储在数组中:

static char password[256] = "superman"; // 现在非const是有效的

现在,你可以存储长度不超过某个合理限制的密码(这里是255个字符加上终止的空字符)。

每当你读取或生成一个新密码(但确保不超过最大密码长度!),你可以将它简单地复制到静态数组中,如果字符串长度已知,可以使用memcpy(如果字符串长度已知),strcpy(字符串长度至少不超过最大值)或strncpy(没有任何保证)。你甚至可以直接读取或生成到这个密码数组中,跳过从中间数组复制的步骤(再次强调:不要超过大小限制!) - 如果这适合你的用例(反例:如果你首先要在可能拒绝之前验证新密码,那么你不希望丢失旧密码)。

英文:

This little piece of code contains much more trouble than you might possibly suspect!

At very first: You assign string literals to pointers to non-const. While legal as string literals are of type array of char (originating from times where const keyword simply did not yet exist and when introduced C designers opted for not breaking existing code) they are still immutable and writing to results in undefined behaviour. So you should always assign string literals only to pointers to const, never to anything else:

static char const* password = "superman";

If you want to exchange this password by other literals you can do so by simple assignment:

password = wifipsk;

I assume, though, that you want to be able to exchange the password by a string you retrieved during runtime, e.g. by user input.

Then quickly the question arises: How to manage the memory required for?

The most stable way is to store the password in an array instead:

static char password[256] = "superman"; // NOW non-const is valid

Now you can store passwords with length up to some reasonable limit (here: 255 characters + terminating null character).

Whenever you read or generate a new password (but make sure maximum password size is not exceeded!) you can just memcpy (if string length is already known anyway), strcpy (string length at least not exceeding the maximum) or strncpy (no guarantees at all) it into the static array. You could even read or generate into this password array directly, skipping the step of copying from an intermediate array (again: stay below size limit!) – if that's suitable for your use case (counter example: if you first want to validate a new password before and possibly reject it; then you don't want to lose the old password).

答案2

得分: 0

wifipsk 是与字符串字面值关联的。字符串字面值的生命周期与程序相同。

因此,您可以安全地进行赋值,但您的代码将不会 const correct

password = wifipsk;

英文:

wifipsk is assigned with reference to the string literal. String literals lifetime is the same as the program.

So you can safely assign, but your code will not const correct

password = wifipsk;

huangapple
  • 本文由 发表于 2023年5月24日 23:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定