I am trying to implement my own atoi function, and I am supposed to pass my variables in after ./a.out.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int myatoi(char array[])
{
        int sum = 0;
        int i = 0;

        while(array[i] != '\0') {
                int number = array[i] - '0';
                sum = 10 * sum + number;
                i++;
                }

        return sum;
}

int main(int argc, char *Name[]) {
/*      char *Name = "Josh";   */
        printf("%d",myatoi(Name));
        return 0;
}

I am getting an error message:

myatoi.c: In function âmainâ:
myatoi.c:20: warning: passing argument 1 of âmyatoiâ from incompatible pointer type

This is in the printf statement. What am I doing wrong?

Also, the program works if I take out the arguments into main and uncomment the first line of main.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

int main(int argc, char *Name[]) {
/* char *Name = "Josh"; */
printf("%d",myatoi(Name));
return 0;
}

One problem here is you're not using the proper value for your function: myatoi(Name).

If you have one argument after your executable, the proper format would be the following: myatoi(Name[1])

The second argument in the main function is a pointer to an array of strings (arguments from the command line). argv[0] would be the command itself, argv[1] would be the first parameter after the command, argv[2] would be the second parameter, etc. .....

int main (int argc, char *argv[])
{
    if (argc == 2)
        {
             myatoi(argv[1]) ;
         }
    else
         {
               printf ("Please, enter a name after the command line!\n") ;
          }
     return 0 ;
}

Okay, so since I am asking for two things in the command line, I must specify that I want the myatoi the second part?

Could I avoid that by taking out the first argument and just asking for one thing in the command line, char *Name[]?

Edited 7 Years Ago by Soileau: grammatical error

I would set your argument to your function to a char pointer: int myatoi(char *pszArg) Since you're wanting to pass an array of characters-- i.e. argv[1] -- to your function you may use a char pointer (*pszArg).

You can then work through the char * as follows:

int i;

for (i = 0 ; pszArg[i] != NULL; i++)
{
   // Code to process your own atoi function.

}

Could I avoid that by taking out the first argument and just asking for one thing in the command line, char *Name[]?

In short--if I understand what you're saying--the answer is no. command.exe Param1 Param2 If you just want the second parameter, then that would be argv[2] or in your case Name[2].

In the above example of using the command line, Name[2] should give you "Param2", and Name[1] should yield "Param1"

how about this...

#include <stdio.h>

int _atoi(char[]);

int main()
{
    printf("%d\n", _atoi("100"));
    printf("%d\n", _atoi("10as20"));
    printf("%d\n", _atoi("fuck"));
    getch();
    return 0;
}

int _atoi(char s[]) 
{
    int i, n=0;
    for(i=0; s[i]>='0' && s[i]<='9'; i++)
        n = 10*n + (s[i] - '0');
    return n;
}
This article has been dead for over six months. Start a new discussion instead.