convertin string to integer using atoi() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • martin paul
    New Member
    • Jan 2007
    • 15

    convertin string to integer using atoi() function

    Sir please consider the following code......
    int getint(char *);
    int main ()
    {
    char name[80];
    printf("enter the string\n");
    scanf("%s",name );
    getint(name);
    }
    int getint(char *ptr)
    {
    int x=0;
    x=atoi(*ptr);
    printf("%d\n",x );
    }
    WHEN I RUN THIS ON LINUX O/S I AM GETTING "SEGMENTATI ON" FAULT
    COULD YOU PLEASE HELP ME WITH THE PROBLEM
    THANK YOU...
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are passing the wrong parameter type to atoi, if you where to include the correct headers the compiler would pick up this mistake for you.

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Char *ptr represent's ur string so it's simmiliar to ptr[].When u want to print whole array u don't specify array element instead u put it like ptr.The same is with this pointer.So:

      x=atoi(ptr);

      should work.


      Savage

      Comment

      • jesusdiehard
        New Member
        • Apr 2007
        • 18

        #4
        try atoi(ptr) instead of atoi(*ptr)

        Comment

        • askcq
          New Member
          • Mar 2007
          • 63

          #5
          that works fine for ascii to integer conversion...
          example ...if the sting is "123"
          and not "123abc"

          even I need to know how to convert a string to integer and vice versa

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The string 123abc is not an integer.

            Comment

            • martin paul
              New Member
              • Jan 2007
              • 15

              #7
              Hello Sir ...I've changed the code as you suggested. If I am giving input string as "martin" the output is zero for me....Could there be any problem in the code...
              Please help me !!

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by martin paul
                Hello Sir ...I've changed the code as you suggested. If I am giving input string as "martin" the output is zero for me....Could there be any problem in the code...
                Please help me !!
                What did you expect it to output? The string "martin" contains no digits, therefore the conversion to an int stops on the 'm' and the function returns 0.

                Comment

                • martin paul
                  New Member
                  • Jan 2007
                  • 15

                  #9
                  Sir Could you tell me what actually atoi(const char *) fuction does if possible with an example....
                  Thank you..

                  Comment

                  • martin paul
                    New Member
                    • Jan 2007
                    • 15

                    #10
                    Sir suppose lets take two strings string 1 and string 2
                    string1="arun"
                    string2="1234"
                    In the memory wouldnt the accii values of each character be stored..

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #11
                      Originally posted by martin paul
                      Sir Could you tell me what actually atoi(const char *) fuction does if possible with an example....
                      Thank you..
                      Atoi converts string to integer.It stop working when it comes to first invalid or '\0' character and returns 0.

                      Example:

                      string=123abc;

                      number=atoi(str ing);

                      number=123;

                      Savage

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by martin paul
                        Sir suppose lets take two strings string 1 and string 2
                        string1="arun"
                        string2="1234"
                        In the memory wouldnt the accii values of each character be stored..
                        atoi does not convert char into it ascii decimal value and assagning it to the
                        number specifed.It searches for specific interval of numbers

                        From: 48
                        To: 57
                        ,inclusive.

                        Savage

                        Comment

                        Working...