将 char 存储在 char 指针中
我有一个 *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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在开始使用它们之前,您需要验证您拥有(至少)3 个参数。
然后你需要分配一些内存来放置角色。
You need to verify you have got (at least) 3 arguments before you start using them.
Then you need to allocate some memory to put the character in.
您正在取消引用未初始化的指针。
delim
在进入else
块时永远不会被初始化。You're dereferencing an uninitialized pointer.
delim
never gets initialized when it goes into theelse
block.除了您的内存问题之外,我认为您对 atoi 的作用感到困惑。它解析数字的字符串表示形式并返回等效的 int 值,例如“10000”=> 10,000。我认为你认为它会给你一个字符的 ASCII 值,例如“A”=>65。
由于您有一个 char * ,并且您(我认为)假设它包含单个字符,因此您可以简单地执行以下操作:
但是,似乎确实不需要使用中间步骤将此值分配给
char
变量。如果最终目标是让delim
指向作为分隔符的 char,那么似乎这就是您需要做的:这不仅删除了不必要的代码,而且意味着您将不再需要需要为 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: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 havedelim
point to the char that is the delimiter, then it seems this is all you need to do: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.