473,509 Members | 2,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

atoi

Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ‘atoi’ makes pointer from integer
without a cast.

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

int i;
char line[100];

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0';
for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));
}
exit(0);
}

when excuting:
$ ./test
Enter line:
this is a test
Segmentation fault
Jun 27 '08 #1
11 4224
Nezhate said:
Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ?atoi? makes pointer from integer
without a cast.
That's because you're passing atoi a char (which is a kind of integer)
rather than a string (which is passed as a pointer).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
char line[100];
Make these local.
int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0';
for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));
}
I *think* you're trying to do this:

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

int main ()
{
size_t i = 0;
char line[100] = {0};
size_t len = 0;

printf("Enter line (no more than 98 characters):\n");
if(fgets(line,sizeof(line),stdin) != NULL)
{
len = strlen(line);
line[len - 1]='\0';
for (i = 0; i < len; i++)
{
printf("line[%d]=%d\n", (int)i, atoi(line + i));
}
}
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
Nezhate wrote:
Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ?atoi? makes pointer from integer
without a cast.

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

int i;
char line[100];

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0';
This is not needed. Fgets will always terminate the input with a null
character.
for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));
The function atoi expects a char * parameter. Here you are passing it
char objects which are really just integers in C. That's what your
compiler is complaining about.
}
exit(0);
}

when excuting:
$ ./test
Enter line:
this is a test
Segmentation fault
I think you may want something like this:

#include <stdio.h>
#include <stdlib.h>
#define LINE_SIZE 16

int main(void) {
char input[LINE_SIZE];
int val;

puts("Enter an integer:");
if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
val = atoi(input);
printf("You entered: %s\nAtoi returned: %d\n", input, val);
return 0;
}

But be aware that atoi provides no error detection or recovery. If you
pass it wrong or out-of-range numeric strings, it will merrily invoke
undefined behaviour. The strto* set of functions like strtol, strtoul,
etc., are much more robust. See your C library documentation for all
these functions.

Jun 27 '08 #3
santosh said:
Nezhate wrote:
>Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ?atoi? makes pointer from integer
without a cast.

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

int i;
char line[100];

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0';

This is not needed. Fgets will always terminate the input with a null
character.
He's not terminating, santosh - he's wiping the newline character. If he
were terminating, he wouldn't be using strlen!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #4
On Thu, 24 Apr 2008 02:33:00 -0700 (PDT), Nezhate
<ma************@gmail.comwrote:
>Hi all,
Can you help me? Why this warning appears in the next simple code ?
warning: passing argument 1 of ‘atoi’ makes pointer from integer
without a cast.
This is one of the few diagnostics that really is self explanatory.
atoi requires that the argument be a pointer to char. Your argument
is line[i]. Since line is an array of char, line[i] is obviously one
of the char in the array. You should recognize that char is a
completely different thing than a pointer to char. They are not
interchangeable nor is there any implicit conversion between them.
Hence the compiler is obligated to tell you that your are calling atoi
with an illegal type.
>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i;
char line[100];

int main ()
{
printf("Enter line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0';
for (i=0;i<strlen(line);i++)
{
printf("line[%d]=%d\n",i,atoi(line[i]));
You appear to be printing out the elements of line one char at a time.
Go back to your reference and convince yourself that atoi is not the
function to convert an individual char to int. You should be able to
determine how atoi knows where to start looking for characters (yes
the plural is a hint) and how atoi knows when to stop looking.

Then ask yourself what, IF ANYTHING (that is another hint), you need
to do to line[i] so that printf can print it as an int.
}
exit(0);
}

when excuting:
Until you are a lot more experienced, it is a waste of time to execute
code that did not compile cleanly.
>$ ./test
Enter line:
this is a test
Segmentation fault
Believe it or not, this is one of the preferred manifestations of
undefined behavior.
Remove del for email
Jun 27 '08 #5
Keith Thompson wrote:
or maybe:

if (c>='0' && c<='9')

Or you could add "#include <ctype.h>" and use isdigit() here.
If not using `isdigit` I'd write it as

if ('0' <= c && c <= '9') ...

so that the two occurences of `c` were next to each other
(making it obvious that it's the same `c`).

It's a shame that C lost BCPL's

'0' <= c <= '9'

(also a shame that in at least one BCPL compiler the generated
code for this was buggy if `c` had side-effects).

--
"In vain I have struggled. It will not do." /Pride and Prejudice/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 27 '08 #6
Keith Thompson wrote:
Nezhate <ma************@gmail.comwrites:
.... snip ...
>
>int char2int(char c) {
int num;
if ((c>='0')&&(c<='9'))

The extra parentheses really aren't necessary. The >= and <=
operators bind more tightly than &&. I'd write this with some
extra whitespace as:

if (c >= '0' && c <= '9')
I approve of the extra spaces, and also the extra parentheses.
They make the expression action completely understandable at first
glance.

Note that making a practice of separating all operators with blanks
avoids accidentally creating such things as a '+=' operator.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #7
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>Nezhate <ma************@gmail.comwrites:
... snip ...
>>
>>int char2int(char c) {
int num;
if ((c>='0')&&(c<='9'))

The extra parentheses really aren't necessary. The >= and <=
operators bind more tightly than &&. I'd write this with some
extra whitespace as:

if (c >= '0' && c <= '9')

I approve of the extra spaces, and also the extra parentheses.
They make the expression action completely understandable at first
glance.
So you prefer this?

if ((c >= '0') && (c <= '9'))

This is a matter of taste. I'm not going to disagree with you, but I
will say that my own taste differs. The fact that the relational
operators bind more tightly than "&&" seems to me to be sufficiently
obvious that the extra parentheses are IMHO unnecessary. Similarly, I
wouldn't use:

x = (y + z);

because it's even more obvious that "+" binds more tightly than "=".

Again, I'm not saying I disagree, merely that my own taste differs on
this point (and anyone *reading* C code needs to be able to deal with
either style).
Note that making a practice of separating all operators with blanks
avoids accidentally creating such things as a '+=' operator.
How would that happen? I don't believe there's any context in which
the tokens "+" and "=" can be adjacent in a legal program. (In early
pre-K&R C, the "-=" token was spelled "=-", which meant that "x=-1"
would decrement x rather than assigning the value -1 to it; it was
changed to "=-" for exactly this reason.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
.... snip ...
>
>Note that making a practice of separating all operators with blanks
avoids accidentally creating such things as a '+=' operator.

How would that happen? I don't believe there's any context in which
the tokens "+" and "=" can be adjacent in a legal program. (In early
pre-K&R C, the "-=" token was spelled "=-", which meant that "x=-1"
would decrement x rather than assigning the value -1 to it; it was
changed to "=-" for exactly this reason.)
It can turn faulty code into error-free (according to compiler)
code. Another nuisance area is code using ++ and --.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #9
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
... snip ...
>>
>>Note that making a practice of separating all operators with blanks
avoids accidentally creating such things as a '+=' operator.

How would that happen? I don't believe there's any context in which
the tokens "+" and "=" can be adjacent in a legal program. (In early
pre-K&R C, the "-=" token was spelled "=-", which meant that "x=-1"
would decrement x rather than assigning the value -1 to it; it was
changed to "=-" for exactly this reason.)

It can turn faulty code into error-free (according to compiler)
code.
Um, how? How can leaving out blanks accidentally create a "+="
operator? Can you provide an example?
Another nuisance area is code using ++ and --.
How so?

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #10
On Sun, 27 Apr 2008 03:47:39 -0700, Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Keith Thompson wrote:
>>CBFalconer <cb********@yahoo.comwrites:
... snip ...
>>>
Note that making a practice of separating all operators with blanks
avoids accidentally creating such things as a '+=' operator.

How would that happen? I don't believe there's any context in which
the tokens "+" and "=" can be adjacent in a legal program. (In early
pre-K&R C, the "-=" token was spelled "=-", which meant that "x=-1"
would decrement x rather than assigning the value -1 to it; it was
changed to "=-" for exactly this reason.)

It can turn faulty code into error-free (according to compiler) code.

Um, how? How can leaving out blanks accidentally create a "+="
operator? Can you provide an example?
If you want to write

*p++ = 3;

and you accidentally write

*p+ = 3;

the compiler will complain.

If you want to write

*p++=3;

and you accidentally write

*p+=3;

the compiler won't complain.
Jun 27 '08 #11
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
.... snip ...
>
>It can turn faulty code into error-free (according to compiler)
code.

Um, how? How can leaving out blanks accidentally create a "+="
operator? Can you provide an example?
a + = b; /* invalid */
becomes: a+=b; /* valid */
>
>Another nuisance area is code using ++ and --.

How so?
a = b++ + ++c; /* valid */
becomes: a=b+++++c; /* invalid */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
7421
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
6
16024
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
5
36411
by: Bansidhar | last post by:
atoi() function seems not to have any support for Hex, octal number. Usually when I read from a text file then it contain number like 0x232 etc. In this case atoi() fells. In case of itoa() there...
15
29179
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
47
46130
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
13
5745
by: ptq2238 | last post by:
Hi, I have written this code to help me learn C but I'm not sure why gcc - Wall is giving me an error when I compile Basically I want to read in a character then a number and then manipulate...
9
4008
by: Would | last post by:
Hey, hopefully one of you can help me... I keep getting an unresolved external 'atoi(char)' and I dont know why.. here is the code #include <iostream> #include <stdlib.h> using namespace std; ...
50
5169
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
10
4154
by: 66650755 | last post by:
First,thanks for all who have answered my last question. if char string="12345"; how could I convert the string(that is "3") to an int by using atoi? I only want to convert...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.