array question...again....

M

michael

Hi All,

I have an int that I need to pass as a member of argv to execv(const char
*path, char *const argv[])

I can't figure out how to do the assigning of the int. Currently I have:

int var;
var = 10;

args[0] = (char*)&var;

and in the executed prog:

printf("var is now = %d\n", *argv[0]);

Clearly this is wrong, it complies and runs but does not maintain the value,
I guess due to different stacks etc.

How hard can it be to pass an int? How can I go about this?
 
M

michael

michael said:
Hi All,

I have an int that I need to pass as a member of argv to execv(const char
*path, char *const argv[])

I can't figure out how to do the assigning of the int. Currently I have:

int var;
var = 10;

args[0] = (char*)&var;

and in the executed prog:

printf("var is now = %d\n", *argv[0]);

Clearly this is wrong, it complies and runs but does not maintain the
value, I guess due to different stacks etc.

How hard can it be to pass an int? How can I go about this?

I just used sprintf().... don't know why I didn't think of it before...
 
J

Jens Thoms Toerring

michael said:
michael said:
Hi All,

I have an int that I need to pass as a member of argv to execv(const char
*path, char *const argv[])

I can't figure out how to do the assigning of the int. Currently I have:

int var;
var = 10;

args[0] = (char*)&var;

and in the executed prog:

printf("var is now = %d\n", *argv[0]);

Clearly this is wrong, it complies and runs but does not maintain the
value, I guess due to different stacks etc.

How hard can it be to pass an int? How can I go about this?
I just used sprintf().... don't know why I didn't think of it before...

Just make sure you allocate enough memory for what you sprintf()
to;-) And, of course, argv[0] can't be an argument, argv[0] is
supposed to be the name of the program.

Regards, Jens
 
B

Barry Schwarz

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
 
B

Barry Schwarz

michael said:
Hi All,

I have an int that I need to pass as a member of argv to execv(const char
*path, char *const argv[])

I can't figure out how to do the assigning of the int. Currently I have:

int var;
var = 10;

args[0] = (char*)&var;

and in the executed prog:

printf("var is now = %d\n", *argv[0]);

Clearly this is wrong, it complies and runs but does not maintain the
value, I guess due to different stacks etc.

How hard can it be to pass an int? How can I go about this?

I just used sprintf().... don't know why I didn't think of it before...

And if you would stop changing the title every time, people wouldn't
be wasting their time answering a question your not interested in, as
I just wasted 10 minutes doing for your previous post. You never
needed to pass an int and now you have figured out how not to.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top