I'm trying to figure out how to pass an int using a char pointer. It fails once the int value is too large for the char. This is what I'm trying to figure out:
int i = 20;
/*some other code/assignments*/
args = (char *)&i;
How would I do something like this if i was a bigger value, like 400?
Thanks in advance.
Best How To :
Programs simply do not take integers as arguments, they take strings. Those strings can be decimal representations of integers, but they are still strings. So you are asking how to do something that simply doesn't make any sense.
Twenty is an integer. It's the number of things you have if you have eighteen and then someone gives you two more. Twenty cannot be a parameter to an executable.
"20" is a string. It can represent the number twenty in decimal. But it's a sequence of two characters, the decimal digit '2' followed by the decimal digit '0'.
You pass strings to executables, not integers.
When you enter a command like
tail -f 20, the number twenty is not one of the arguments. They are the string "tail", the string "-f", and the string "20" (the digit '2' followed by the digit '0'). There are no numbers in there, just strings (though one of them happens to represent a number).
You can do this:
int i = 10;
sprintf(parameter, "%d", i);
// parameter now contains the string "10"
args = parameter;