Hi All,
I have an int that I need to pass as a member of argv to execv(const char
*path, char *const argv[])
Why do you think you want to pass an int to execv?
Since argv is an array of pointers, there is no portable way to store
an int as one of the elements. Some implementations will allow you to
assign an int value to a char* with the result being some
implementation defined value. To find out about this, you need to
post in a newsgroup where your system is topical. In this group, we
discuss only the standard features of the language.
I can't figure out how to do the assigning of the int. Currently I have:
int var;
var = 10;
args[0] = (char*)&var;
Surely you meant argv[0]. That assigns the address of the int to the
first element of the array. While this is guaranteed to be portable,
it is different than what you said you wanted to do. To pass the int
as the first element, you would use
argv[0] = (char*)var;
which at best will result in an implementation defined value.
So now the question is what did you really mean? And why don't you
post to a group where execv is topical.
and in the executed prog:
printf("var is now = %d\n", *argv[0]);
*argv[0] is a char. While the value is promoted to int when it is
passed to printf, only the value of the first byte of the int is used
to determine the value.
If you cast the value in argv[0] back to an int*, then you can
dereference it to get the original int, as in
printf("... %d\n", *(int*)argv[0]);
Clearly this is wrong, it complies and runs but does not maintain the value,
I guess due to different stacks etc.
Don't guess. Think about what the code is actually doing, not what
you hope it will do.
What different stacks do you think you have? Why do you think you
have stacks at all? Does any of your C reference material mention
stacks? Does the word stack appear in the standard (n1124 is
available on the web)?
How hard can it be to pass an int? How can I go about this?
Passing an int is really easy. Using a function which is not
expecting an int can be tricky. Other than the prototype, do you have
an documentation for execv that describes what the parameters are used
for?
By any chance, is execv expecting a string which contains a bunch of
digits that sscanf or strtol (or even atoi) could extract an integer
value from?
Remove del for email