problem on atol

This is a discussion on problem on atol within the C Programming forums, part of the General Programming Boards category; Can someone help to check my code, the function atol converts a string representation of a numeric value into a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    problem on atol

    Can someone help to check my code, the function atol converts a string representation of a numeric value into a long value. The function ascii2long that exactly reproduce the function atol.


    Code:
    #include <stdio.h>
    
    long ascii2long(const char s[]) {
    
      long l;
    
      int i;
      l=atol(s[i]);
       return l;
     }
    
    void main(){
    
       long s[9]={2, 2.345 , 2 , 92, 2.5 , -1.23};
       int;
       for (i=0; i<9; i++)   {
       
       ascii2long(s[i]);
       printf("%l",l); }
    
    
     getchar();
    }
    Thank a lot

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I don't understand you already have an array of longs??
    Code:
    long s[9]={2, 2.345 , 2 , 92, 2.5 , -1.23};

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Re: problem on atol

    Originally posted by zp523444

    Code:
      int i;
      l=atol(s[i]);
       return l;

    and whats that 'i' doing there.. whats 'i'..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: problem on atol

    Originally posted by vasanth
    and whats that 'i' doing there.. whats 'i'..
    My guess is that they think because i is in main with a current value, that it has the same value inside the function. In reality, i has some arbitrary value, and is doubtless pointing some place way out of bounds of their array.

    One other nifty problem they have:
    Code:
    long s[9]={2, 2.345 , 2 , 92, 2.5, -1.23};
    These are not long values. These are either float or double which are converted to long. As such, your array really holds something like:
    Code:
    long s[9] = { 2, 2, 2, 92, 2, -1 };
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    soory,I make mistake, the thing need input should be :
    2,
    2.345 ,
    2A ,
    92,
    2.5A ,
    -1.23,
    -204,
    1234567898,
    -1234567898,
    -A2,
    -2A

    what type of the data should I use ?

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    listen to quzah use floats or doubles, but what is that A doing in a couple of you array characters? Is it hexadecimal?

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    it is not hex

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by zp523444
    it is not hex
    Then what is it?

    Here's your code, revised by me based on what I guess you might be asking for. It uses atof() instead of atol() as your input are floats, hence I've changed the function name to suit. Change as you see fit.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float ascii2float(const char s[])
    {
      float  f;
      f = atof(s);
      return(f);
    }
    
    int main(void)
    {
      char *s[] = {"2","2.345","2","92","2.5","-1.23"};
      int i;
      
      for (i = 0; i < 6; i++)
      {
        printf("%f\n", ascii2float(s[i]));
      }
    
      getchar();
      
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21