0

I know this is basic stuff, but I'm having one particular problem with this.

I'm implementing a simple shell program, and the exit function needs to pass on the exit value if one is passed on. Therefore, exit 45 should return 45 as a value, but nothing to be printed*.

The commands are passed to a function parser in a char **arglist array. Since exit 45 is two args, I need to print out the last arg.

However,my debug statement printf((int)&arglist[1]) returns a value of 163393752, which is just slightly off from the passed in value. When I changed printf to puts, the correct value was printed out, but exit() requires an integer, not a pointer.

Using this example how can I get 45 from the pointer array?

*according to the professor, quoting his phrasing verbatim.

4
  • 2
    This sounds like homework...is it? Commented Apr 24, 2011 at 4:01
  • Its for a school project. Basically the last bit I need to complete to satisfy all the requirements Commented Apr 24, 2011 at 4:05
  • Homework or not, I think some code needs to be shown. Why describe what's going on when a few lines of code will make so we don't have to guess at the details? And copy/paste is your friend. By the way, it sounds like you need to convert the string "45" to an int. Commented Apr 24, 2011 at 4:06
  • The relevant code is already there in `printf((int)&arglist[0]); Commented Apr 24, 2011 at 4:07

5 Answers 5

2

Look up the atoi function in stdlib.h (or write your own equivalent if you're not allowed to use the library).

Sign up to request clarification or add additional context in comments.

1 Comment

I had completely forgotten that function! Thats what I needed, thanks for the reminder.
1

printf needs a format string; check out the man page

(int)&arglist[1] should be arglist[1], and it's a char * not an int

Comments

0

Is that printf argument passing the address instead of the value? Should it be *arglist[1]? I'm a little rusty in c pointers.

Comments

0

The problem is that &arglist[1] is the same as &(*(arglist+1), which actually prints the address in memory of arglist[1]. If you want to print the value at this point, just print arglist[1] - printf() doesn't usually need the &.

2 Comments

when you need to pass a pointer *value to a function that requires an actual value, &value is the correct operation. So for a pointer array, the inverse is true?
Ah, the value of a pointer is usually with * yes. But array[1] actually does dereferencing for you. Thus, the value at arglist[1] is 42, which you get by calling arglist[1]. The & operator is the address-of operator. In any case, your item at arglist[1] is a string (i.e. char*), so you want to printf("%s\n", arglist[1]) - or, like others have suggested, use atoi() to convert it to an integer
0

I guess you want to do is like this:

./your_program # run your_program
ret_val=$? # gets exit status and stores into $ret_val
./other_program $ret_val # pass exit status of your_program to other_program as a argument

However, it's not a right way to pass value from one program to another. Because only lower 8 bit of exit status of a program is used, see: http://linux.die.net/man/3/exit .

You can pass value from one program to another program use exit iff your purpose is to pass the exit status(8bit number) not the exit value(an integer).

The typical way to pass a value to other program is print the value: eg:

your program:

printf("%d", value);
exit(0);

in shell:

value=`./your_program` # run your program and store output into 'value'
./other_program $value

in other_program: use strtol (atoi is deprecated) to convert string to integer:

int value = stdtol(argv[1], NULL, 10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.