Jump to content

atoi function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
felixb

felixb

    Newbie

  • Members
  • Pip
  • 7 posts
hello,

I am new here and I have a question about this code:
"
int num;
char a[4];

a[0]=9;
a[1]=4;
a[2]=NULL;
a[3]=NULL;

num=atoi(grade);
"

I have a char array (a) that have (9 4 NULL NULL) i want to extract only the 94 using atoi function.
but, when I run the program the atoi function return 0. why?

thnk you,

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
You probably meant to have
a[0] = '9';
a[1] = '4';
a[2] = '\0';
a[3] = '\0';
This is more easily written (as an initialized array) as:
char a[4] = "94";


#3
felixb

felixb

    Newbie

  • Members
  • Pip
  • 7 posts
no... but the point is that i have array and each cell have digit...
i tried now with '\0' but it still return 0

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Try looking more closely at my previous response: 9 is not the same as '9'. Use copy and paste if need be.

Edited by dcs, 24 March 2008 - 09:39 AM.


#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
<<double post -- please delete>>

#6
felixb

felixb

    Newbie

  • Members
  • Pip
  • 7 posts
thank you very much for your help

#7
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
you have used atoi(grade).
What is grade? Its not declared anywhere! Use atoi(a).A is where you stored the data!
And '\0' and NULL dont make a difference. One is a escape sequence representation and the other is a high level text, resulting both in an eof.

God is real... unless declared an integer

my blog :: http://techarraz.com/


#8
felixb

felixb

    Newbie

  • Members
  • Pip
  • 7 posts
it's ok.
the problem was that I done a[1]=9 instead of a[1]='9'

#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

Chinmoy said:

And '\0' and NULL dont make a difference. One is a escape sequence representation and the other is a high level text, resulting both in an eof.
'\0' and NULL may be different (a character vs a pointer), and neither has anything in common with EOF (a negative integer).

:confused:

Edited by dcs, 25 March 2008 - 09:53 PM.


#10
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
'\0' and NULL have the same implication. Just that NULL works good with pointers and '\0' works good with char arrays.By eof here i wanted to mean end of input buffer..

God is real... unless declared an integer

my blog :: http://techarraz.com/