Amiga.org

Amiga computer related discussion => Amiga Software Issues and Discussion => Topic started by: elendil on July 20, 2003, 03:11:23 AM

Title: ReadArgs() problem.
Post by: elendil on July 20, 2003, 03:11:23 AM
Hi.

Can anyone tell me why the following code produces garbled output? Garbled as in enter 2 as an argument and get 251500661 back. Run the program again with same argument and the number changes. I have been reading and reading and trying and trying and afaik this should be how it is supposed to work. string arguments are passed along nicely, btw, just not integers. I am using gcc.

/* Testing readargs. */

#include
#include

#define TEMPLATE   "D=DeviceNameLength/K/N"

LONG arguments[1];
struct RDArgs *rda = NULL;
 
int main(void) {

  if(rda = ReadArgs(TEMPLATE, arguments, NULL)) {
   
    if(arguments[0])
      printf("Device name length chosen: %d\n", (LONG *)arguments[0]);
       
    FreeArgs(rda);
  }
 
  else {
    printf("Wrong arguments. Use argument '?' to see correct usage.\n");
    return 0;
  }
 
  return 1;
}

Sincerely,

-Kenneth Straarup.

PS: solutions in laymans terms, please :)

edit: hm, so spaces aren't very visible, it seems. Or they just didn't get pasted properly. I hope you can make it out, still.
Title: Re: ReadArgs() problem.
Post by: Piru on July 20, 2003, 03:31:25 AM
Quote
printf("Device name length chosen: %d\n", (LONG *)arguments[0]);

/N are *pointer* to LONG value, not LONG itself, or else there would be no way to enter 0.

So, to make it work, reference the pointer (with '*'):

printf("Device name length chosen: %d\n", *(LONG *)arguments[0]);

Or to make it more readable:

LONG *longptr;

longptr = (LONG *) arguments[0];
printf("Device name length chosen: %d\n", *longptr);
Title: Re: ReadArgs() problem.
Post by: elendil on July 20, 2003, 03:59:21 AM
That's splendid!

It is now working just as intended. And from all the possible combinations I must have skipped that one :/

I am still trying to completely understand all this pointer nonsense :)

Thank you.

Sincerely,

-Kenneth Straarup.
Title: Re: ReadArgs() problem.
Post by: CodeSmith on July 20, 2003, 08:12:43 AM
Didn't the compiler give you any warning about casting integers to pointers?  :-?

BTW: pointers are THE key to efficient programming in C (C++ not so much, but still quite a bit).  Take some time to learn all you can about them, they are *very* important.