Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-2008, 08:19 AM
felixb felixb is offline
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0
felixb is on a distinguished road
Default atoi function

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,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-24-2008, 10:19 AM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: atoi function

You probably meant to have
Code:
a[0] = '9';
a[1] = '4';
a[2] = '\0';
a[3] = '\0';
This is more easily written (as an initialized array) as:
Code:
char a[4] = "94";
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-24-2008, 01:10 PM
felixb felixb is offline
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0
felixb is on a distinguished road
Default Re: atoi function

no... but the point is that i have array and each cell have digit...
i tried now with '\0' but it still return 0
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-24-2008, 01:35 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: atoi function

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

Last edited by dcs; 03-24-2008 at 01:39 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-24-2008, 01:38 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: atoi function

<<double post -- please delete>>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 03-24-2008, 04:35 PM
felixb felixb is offline
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0
felixb is on a distinguished road
Default Re: atoi function

thank you very much for your help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-26-2008, 01:27 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 308
Rep Power: 7
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default Re: atoi function

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-26-2008, 01:37 AM
felixb felixb is offline
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0
felixb is on a distinguished road
Default Re: atoi function

it's ok.
the problem was that I done a[1]=9 instead of a[1]='9'
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-26-2008, 01:46 AM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: atoi function

Quote:
Originally Posted by Chinmoy View Post
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).


Last edited by dcs; 03-26-2008 at 01:53 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-26-2008, 02:03 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 308
Rep Power: 7
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default Re: atoi function

'\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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inline function Chinmoy C Tutorials 1 04-07-2008 04:13 PM
function pointer Chinmoy C Tutorials 0 03-19-2008 01:52 AM
SecurityAudit vinay Visual Basic Programming 27 01-07-2008 01:14 PM
multi-pass preprocessing kenna C and C++ 11 08-14-2007 11:45 AM


All times are GMT -5. The time now is 11:45 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads