简体   繁体   English

atoi来自char *在C中返回0

[英]atoi from char * returning 0 in C

Very new to C so please bear with me. 对C来说很新,所以请耐心等待。 I have a function that takes in 3 char * variables, opcode , arg1 and arg2 . 我有一个函数,它接受3个char *变量, opcodearg1arg2

arg1 and arg2 can either be (1) a string containing the name of a variable holding an unsigned int, or (2) an actual integer. arg1和arg2可以是(1)包含保存unsigned int的变量名称的字符串,或(2)实际整数。

Either way, I need to get the actual integer out. 不管怎样,我需要得到实际的整数。 I am trying to use atoi so far but it seems to be returning 0 for the first case. 我到目前为止尝试使用atoi但是它似乎在第一种情况下返回0。

ie

sscanf(instruction, "%s %s %s", opcode, arg1, arg2);

sum = atoi(arg1) + atoi(arg2);

I can't post the entire code as it doesn't belong to me, but hopefully the above demonstration helps a little? 我不能发布整个代码,因为它不属于我,但希望上面的演示有点帮助?

As I said in my comment, you can't expect to retrieve the value of a named variable at run-time, in C. Variable names are not something that is kept when the code runs, then it's pure machine code and all variables have been "cooked down" to memory addresses. 正如我在评论中所说,你不能期望在运行时检索命名变量的值,在C中。变量名不是代码运行时保留的东西,那么它是纯机器代码,所有变量都有被“煮熟”到记忆地址。 The names are no longer around. 名字不再存在。

The atoi() function converts a string holding an integer, such as "4711" , into the actual value, 4711 . atoi()函数将包含整数的字符串(例如"4711" )转换为实际值4711 That's all it does, it cannot retreive values "by name" in any way. 这就是它所做的一切,它无法以任何方式“按名称”来回溯价值。

If you want to have a mechanism to map variable names to values when the program runs, you are going to have to create that mechanism yourself, since the language does not have one . 如果您希望有一种机制在程序运行时将变量名称映射到值,那么您将不得不自己创建该机制,因为该语言没有 For instance, you could create some kind of array of structures that let you associate names with values, but that would of course require you to initialize that array. 例如,您可以创建某种结构数组,以便将名称与值相关联,但这当然要求您初始化该数组。

Something like this: 像这样的东西:

int foo = 12;
char *vname = "foo";

print("the value of %s is %d\n", vname, get_variable_value(vname));

simply is not going to happen in C, the function get_variable_value() cannot exist . 根本不会在C中发生 ,函数get_variable_value() 不能存在

Variable names are used only at compile-time. 变量名仅在编译时使用。 Once the program is compiled, all information regarding what names you used are lost 1 . 程序编译完成后,所有关于您使用的名称的信息都将丢失1 Therefore, you have no way of accessing a variable contents by a "string" containing its name at runtime. 因此,您无法通过在运行时包含其名称的“字符串”访问变量内容。 2 2

1 more precisely, they are available to the linker or possibly debugger, but that's not something you could use (or count on) 1更准确地说,它们可用于链接器或可能的调试器,但这不是你可以使用的(或依靠)

2 I can think of a (terrible) way to do so with macros or use a map of all your variable names to their addresses, but I believe your program needs to be fixed by design. 2我可以想到一种(可怕的)使用宏的方法或使用所有变量名称到他们地址的地图,但我相信你的程序需要通过设计来修复。

You must create a data structure in which you put the variables and the values of them. 您必须创建一个数据结构,在其中放置变量及其值。 Look for "hash map" in the libraries that you use. 在您使用的库中查找“哈希映射”。

Then you can use isdigit(arg[0]) to check whether the argument is a number or a variable name. 然后你可以使用isdigit(arg[0])来检查参数是数字还是变量名。 Pseudocode: 伪代码:

sum = toInt(arg1) + toInt(arg2)

int toInt(char * arg) {
    return isdigit(arg[0]) ? atoi(arg) : map->lookupValue(arg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM