1 Replies - 182 Views - Last Post: 29 July 2019 - 10:32 AM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 276
  • Joined: 25-May 16

Pass in argv[1] value to a function

Posted 29 July 2019 - 08:49 AM

Hi everyone, I haven't touched the C programming language in a while so this might be a little bit of a noob mistake but I am trying to pass in the value from the command line to another function and I am not getting it quite right.

This is what I have so far

void main(int32 nargs, char *argv[])
{
  int32 priority;

  if(nargs > 2)
   return SYSERR;

  if(nargs == 2)
   priority = returnPositiveInt(argv[1])

  else
   priority = 20;    
}

int32 returnPositiveInt(char number[])
{
  int32 i = 0;

  if(number[0] == '0' or number[0] == '-')
   return 20; // default value
  while(i < sizeof(number))
  {
    if(number[i] < '0' || number[i] > '9')
     return 20; // default value
    i++;
  }

   return atoi(number);
}



However, every time I call the function, I'm not able to successfully pass in the correct value.

What am I missing here?

Is This A Good Question/Topic? 0
  • +

Replies To: Pass in argv[1] value to a function

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2369
  • View blog
  • Posts: 4,496
  • Joined: 30-May 10

Re: Pass in argv[1] value to a function

Posted 29 July 2019 - 10:32 AM

> void main(int32 nargs, char *argv[])
1. main returns int, not void.
2. make nargs just an int, not int32

> 06 return SYSERR;
It needs to be int, because you're returning values.

> priority = returnPositiveInt(argv[1])
You're missing a ; at the end of the line.

> while(i < sizeof(number))
You'll need strlen(), not sizeof() here.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1