Welcome, Guest. Please login or register.

Author Topic: ReadArgs() problem.  (Read 1524 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline elendilTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
ReadArgs() problem.
« 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.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: ReadArgs() problem.
« Reply #1 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);
 

Offline elendilTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
Re: ReadArgs() problem.
« Reply #2 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.
 

Offline CodeSmith

  • Sr. Member
  • ****
  • Join Date: Sep 2002
  • Posts: 499
    • Show only replies by CodeSmith
Re: ReadArgs() problem.
« Reply #3 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.