将 char 存储在 char 指针中

发布于 2024-09-11 19:38:28 字数 902 浏览 5 评论 0原文

我有一个 *char 全局变量。我的主函数头读取为 int main(int argc, char* argv[argc]){...}。这两行代码必须保持原样。我的 main 函数的第一个参数是一个 *char 类型的数字,我使用 atoi(...); 将其转换为 char。我基本上是将 ASCII 值更改为其相应的字符。现在我想将这个局部变量字符存储到作为 char 指针的全局变量中。我知道问题与内存分配有关,但我不知道如何解决这个问题。

我的代码:

char* delim;
int main(int argc, char* argv[argc])
{
  char delimCharacter;
  if (isdigit(*(argv[3])) == 0) delim = argv[3]; //you can pass in a character or its ascii value
  else {   //if the argument is a number, then the ascii value is taken
    delimCharacter = atoi((argv[3]));
    printf("%s\t,%c,\n", argv[3], delimCharacter);
    //sprintf( delim, "%c", delimCharacter );  // a failed attempt to do this
    *delim = delimCharacter;
    //strncpy(delim, delimCharacter, 1);                // another failed attempt to do this
  }
  //printf("%s\n",delim);

这会产生段错误。

I have a global variable that is a *char. My main function header reads as int main(int argc, char* argv[argc]){...}. These two lines of code have to remain the way they are. The first argument of my main function is a number of type *char, that I convert to a char using atoi(...);. I am basically changing the ASCII value to its corresponding character. Now I want to store this local variable character I have into the global variable that is a char pointer. I know the problem is related to allocation of memory, but I am not sure how to go about this.

My code:

char* delim;
int main(int argc, char* argv[argc])
{
  char delimCharacter;
  if (isdigit(*(argv[3])) == 0) delim = argv[3]; //you can pass in a character or its ascii value
  else {   //if the argument is a number, then the ascii value is taken
    delimCharacter = atoi((argv[3]));
    printf("%s\t,%c,\n", argv[3], delimCharacter);
    //sprintf( delim, "%c", delimCharacter );  // a failed attempt to do this
    *delim = delimCharacter;
    //strncpy(delim, delimCharacter, 1);                // another failed attempt to do this
  }
  //printf("%s\n",delim);

This yields a seg fault.

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

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

发布评论

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

评论(4

宣告ˉ结束 2024-09-18 19:38:28

在开始使用它们之前,您需要验证您拥有(至少)3 个参数。

if (argc < 4)
{
    printf("Need 3 args");
    exit(1);
}

然后你需要分配一些内存来放置角色。

delim = malloc(2);
// TODO: Should check the result of malloc before using it.
*delim = delimCharacter;
delim[1] = 0; // Need to NULL terminate char*

You need to verify you have got (at least) 3 arguments before you start using them.

if (argc < 4)
{
    printf("Need 3 args");
    exit(1);
}

Then you need to allocate some memory to put the character in.

delim = malloc(2);
// TODO: Should check the result of malloc before using it.
*delim = delimCharacter;
delim[1] = 0; // Need to NULL terminate char*
站稳脚跟 2024-09-18 19:38:28

您正在取消引用未初始化的指针。 delim 在进入 else 块时永远不会被初始化。

You're dereferencing an uninitialized pointer. delim never gets initialized when it goes into the else block.

桃气十足 2024-09-18 19:38:28
char delim[] = ","; // anything really, as long as as it's one character string

...


delim[0] = delimCharacter;
char delim[] = ","; // anything really, as long as as it's one character string

...


delim[0] = delimCharacter;
傲鸠 2024-09-18 19:38:28

除了您的内存问题之外,我认为您对 atoi 的作用感到困惑。它解析数字的字符串表示形式并返回等效的 int 值,例如“10000”=> 10,000。我认为你认为它会给你一个字符的 ASCII 值,例如“A”=>65。

由于您有一个 char * ,并且您(我认为)假设它包含单个字符,因此您可以简单地执行以下操作:

delimCharacter = *(argv[3]);

但是,似乎确实不需要使用中间步骤将此值分配给 char 变量。如果最终目标是让 delim 指向作为分隔符的 char,那么似乎这就是您需要做的:

delim = argv[3];

这不仅删除了不必要的代码,而且意味着您将不再需要需要为 delim 指向分配额外的内存。

我还将 delim 声明为 const char *,因为我认为没有理由更改它。

In addition to your memory issue, I think you are confused about what atoi does. It parses a string representation of a number and returns the equivalent int value, e.g. "10000" => 10,000. I think that you think it will give you the ASCII value of a character, e.g. "A" =>65.

Since you have a char *, and you are (I think) assuming that it contains a single character, you could simply do this:

delimCharacter = *(argv[3]);

However, there really seems to be no need to use the intermediate step of assigning this value to a char variable at all. If the end goal is to have delim point to the char that is the delimiter, then it seems this is all you need to do:

delim = argv[3];

Not only does this remove unnecessary code, but it means you would no longer need to allocate additional memory for delim to point to.

I would also declare delim as a const char * since I assume there is no reason to change it.

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