473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with atoi()

Hi,My problem is this:
I have :
char matrix[3][3]=....
int x;
x=atoi(matrix[2][2]);

This doesn't work.Any help?

I don't have any errors.Just a warning" passing argument 1 of 'atoi'
makes pointer from integer without a cast"

Sep 9 '07 #1
23 2943
On Sun, 09 Sep 2007 12:30:47 +0000, tolkien wrote:
Hi,My problem is this:
I have :
char matrix[3][3]=....
int x;
x=atoi(matrix[2][2]);

This doesn't work.Any help?
atoi takes a pointer to char. matrix[2][2] is a char.
What are you trying to do? I'd be tempted to say "maybe you mean
atoi(&matrix[2][2])", but matrix[2][2] is the last byte in the
object you defined, so it can't be the beginning of a nonempty
string.

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 9 '07 #2
tolkien wrote:
Hi,My problem is this:
I have :
char matrix[3][3]=....
int x;
x=atoi(matrix[2][2]);

This doesn't work.Any help?

I don't have any errors.Just a warning" passing argument 1 of 'atoi'
makes pointer from integer without a cast"
Here's a clue. The prototype:

int atoi(const char *_s);

Note matrix[2][2] is type char, not char*.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 9 '07 #3
i have this matrix:
char matrix[3][3]={'3','4','5',
'6','7','8',
'1','2','3' };

and i want to use one of its elements as integer .
for example int x= matrix[1][2] (8)

Sep 9 '07 #4

"tolkien" <gv****@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
i have this matrix:
char matrix[3][3]={'3','4','5',
'6','7','8',
'1','2','3' };

and i want to use one of its elements as integer .
for example int x= matrix[1][2] (8)
In this case, "atoi" simply becomes

int v = matrix[i][j] - '0';

but if you have to deal with multi-digit integers
you'd better declare the matrix as char *matrix[3][3]
and assign to its elements the pointers to strings
allocated elsewhere. Then you can use atoi.

Sep 9 '07 #5
thank you very much!!!!!

Sep 9 '07 #6

"tolkien" <gv****@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>i have this matrix:
char matrix[3][3]={'3','4','5',
'6','7','8',
'1','2','3' };

and i want to use one of its elements as integer .
for example int x= matrix[1][2] (8)
atoi() works on strings, not single characters

Write a function

int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}

this works because you are guaranteed consecutive codes for decimal digits.
The assert() is for safety.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 9 '07 #7
"Malcolm McLean" <re*******@btinternet.comwrites:
"tolkien" <gv****@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
>>i have this matrix:
char matrix[3][3]={'3','4','5',
'6','7','8',
'1','2','3' };

and i want to use one of its elements as integer .
for example int x= matrix[1][2] (8)
atoi() works on strings, not single characters

Write a function

int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}

this works because you are guaranteed consecutive codes for decimal digits.
The assert() is for safety.
Using assert() is appropriate only if a non-digit character in the
matrix is a program bug. If it could be the result of, for example,
invalid input, assert() is not the way to check for the error.

The code snippet we've seen is clearly a toy example; we don't have
enough information about how the matrix is really initialized (unless
this is a homework problem).

I'd also check *before* converting the character to an int, probably
using isdigit().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 9 '07 #8
On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
"Malcolm McLean" <re*******@btinternet.comwrites:
>int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}
[snip]
I'd also check *before* converting the character to an int, probably
using isdigit().
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 10 '07 #9
Army1987 <ar******@NOSPAM.itwrites:
On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
>"Malcolm McLean" <re*******@btinternet.comwrites:
>>int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}
[snip]
>I'd also check *before* converting the character to an int, probably
using isdigit().
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.
No, isdigit() only returns true for '0' .. '9'. Quoting C99
7.4.1.5p2:

The isdigit function tests for any decimal-digit character (as
defined in 5.2.1).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 10 '07 #10
"Army1987" <ar******@NOSPAM.ita crit dans le message de news:
pa****************************@NOSPAM.it...
On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
>"Malcolm McLean" <re*******@btinternet.comwrites:
>>int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}
[snip]
>I'd also check *before* converting the character to an int, probably
using isdigit().
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.
Really ? I don't think so:

7.4.1.5p2: The isdigit function tests for any decimal-digit character (as
defined in 5.2.1).

5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

isxxx functions that have locale specific behaviour are noted as such.
isdigit and iscntrl do not seem to have locale specific behaviour.

I cross post to comp.std.c to get an authoritative answer on this question
relating to the C Standard.

--
Chqrlie.
Sep 10 '07 #11
Malcolm McLean wrote:
>
"tolkien" <gv****@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
i have this matrix:
char matrix[3][3]={'3','4','5',
'6','7','8',
'1','2','3' };

and i want to use one of its elements as integer .
for example int x= matrix[1][2] (8)
atoi() works on strings, not single characters

Write a function

int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}

this works because you are guaranteed consecutive
codes for decimal digits.
The assert() is for safety.
This way also protects you in the unlikely event
that ch equals INT_MIN:

int chartoval(char ch)
{
assert(ch >= '0' && ch <= '9');
return ch - '0';
}

--
pete
Sep 10 '07 #12
Charlie Gordon wrote:
>
"Army1987" <ar******@NOSPAM.ita crit dans le message de news:
pa****************************@NOSPAM.it...
On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
"Malcolm McLean" <re*******@btinternet.comwrites:
int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}
[snip]
I'd also check *before* converting the
character to an int, probably using isdigit().
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.

Really ? I don't think so:

7.4.1.5p2: The isdigit function tests for
any decimal-digit character (as defined in 5.2.1).

5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

isxxx functions that have locale specific behaviour are noted as such.
isdigit and iscntrl do not seem to have locale specific behaviour.

I cross post to comp.std.c to get
an authoritative answer on this question
relating to the C Standard.
But there's no ambiguity in the C standard
on this particular matter.

--
pete
Sep 10 '07 #13
"pete" <pf*****@mindspring.coma crit dans le message de news:
46***********@mindspring.com...
Charlie Gordon wrote:
>>
"Army1987" <ar******@NOSPAM.ita crit dans le message de news:
pa****************************@NOSPAM.it...
On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson wrote:
"Malcolm McLean" <re*******@btinternet.comwrites:
int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}
[snip]
I'd also check *before* converting the
character to an int, probably using isdigit().
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.

Really ? I don't think so:

7.4.1.5p2: The isdigit function tests for
any decimal-digit character (as defined in 5.2.1).

5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

isxxx functions that have locale specific behaviour are noted as such.
isdigit and iscntrl do not seem to have locale specific behaviour.

I cross post to comp.std.c to get
an authoritative answer on this question
relating to the C Standard.

But there's no ambiguity in the C standard
on this particular matter.
I agree, but the wording is somewhat convoluted, Army1987 got it wrong, and
it took me too long to verify that isdigit is not infected with stupid
broken Locale stuff.

Why refer to the very long section 5.2.1 instead of explicitly describe the
digits '0' to '9' . Simplicity helps.

--
Chqrlie.
Sep 10 '07 #14
Charlie Gordon wrote:
>>>int chartoval(char ch)
{
int answer = ch - '0';
assert(answer >= 0 && answer <= 9);
return answer;
}
[snip]
>>I'd also check *before* converting the character to an int, probably
using isdigit().
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.

Really ? I don't think so:

7.4.1.5p2: The isdigit function tests for any decimal-digit character (as
defined in 5.2.1).

5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
They're "contiguous" on ASCII or BCDIC based character sets, but on other
character sets, they may not be.
isxxx functions that have locale specific behaviour are noted as such.
isdigit and iscntrl do not seem to have locale specific behaviour.
That doesn't change the fact that ch - '0' is not guaranteed to portably
work.
I cross post to comp.std.c to get an authoritative answer on this
question
relating to the C Standard.
comp.lang.c is full of experts of the C standard for discussions about
coding in standard C.
comp.std.c is for discussion about the C standard, not about coding in
standard C.

[followup set to comp.lang.c]
--
You can contact me at <ta*****************@yahoDELETETHATo.fr>
Sep 10 '07 #15
Andr Gillibert wrote:
They're "contiguous" on ASCII or BCDIC based character sets, but on
other character sets, they may not be.
Excuse me. Actually, the C standard forbids such character sets.
They have to be contiguous.

--
You can contact me at <ta*****************@yahoDELETETHATo.fr>
Sep 10 '07 #16
Andr Gillibert said:

<snip>
ch - '0' is not guaranteed to portably work.
I think I'm right in saying you no longer maintain this position but,
just to be clear, it *is* guaranteed to work if the intent is to
convert 'x' to x where x is in the range 0-9.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 10 '07 #17
"Charlie Gordon" <ne**@chqrlie.orgwrote:
"pete" <pf*****@mindspring.coma crit dans le message de news:
46***********@mindspring.com...
Charlie Gordon wrote:
>
"Army1987" <ar******@NOSPAM.ita crit dans le message de news:
pa****************************@NOSPAM.it...
isdigit() could return true for locale-specific digits, which
could be distinct from the '0' - '9' Western Arabic numerals in
the basic character set, for which the ch - '0' trick doesn't
work.

Really ? I don't think so:

7.4.1.5p2: The isdigit function tests for
any decimal-digit character (as defined in 5.2.1).

5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

isxxx functions that have locale specific behaviour are noted as such.
isdigit and iscntrl do not seem to have locale specific behaviour.

I cross post to comp.std.c to get
an authoritative answer on this question
relating to the C Standard.
But there's no ambiguity in the C standard
on this particular matter.

I agree, but the wording is somewhat convoluted, Army1987 got it wrong, and
it took me too long to verify that isdigit is not infected with stupid
broken Locale stuff.

Why refer to the very long section 5.2.1 instead of explicitly describe the
digits '0' to '9' . Simplicity helps.
Possibly because if you want to change the definition of "digit" (for
example, to allow locale-specific digits, as above), you only need to
change it in one place.

Richard
Sep 10 '07 #18
Richard Heathfield wrote:
Andr Gillibert said:

<snip>
>ch - '0' is not guaranteed to portably work.

I think I'm right in saying you no longer maintain this position
Yes.
I'm sorry to have posted this wrong material.
but,
just to be clear, it *is* guaranteed to work if the intent is to
convert 'x' to x where x is in the range 0-9.
Yes.

--
You can contact me at <ta*****************@yahoDELETETHATo.fr>
Sep 10 '07 #19
"Andr Gillibert" wrote:
5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
They're "contiguous" on ASCII or BCDIC based character sets, but on other
character sets, they may not be.
...
That doesn't change the fact that ch - '0' is not guaranteed to portably
work.
Actually the C standard does require the digit codes to be contiguous
and ascending, so it is guaranteed to work on any conforming C
implementation.

I argued against this requirement, which infringes on the codeset
designer's turf, but lost the argument. (I would rather specify
macros/functions to convert between digit characters and
corresponding numeric interpretations.)
Sep 10 '07 #20
tolkien wrote:
i have this matrix:
char matrix[3][3]={'3','4','5',
'6','7','8',
'1','2','3' };

and i want to use one of its elements as integer .
for example int x= matrix[1][2] (8)
If you *have* to use atoi(), declare matrix as:

int x;

char *matrix[3][3] = {"3","4","5",
"6","7","8",
"1","2","3"};
Then the following will work:

x = atoi(matrix[2][2]);
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Sep 10 '07 #21
"Andr Gillibert" wrote:
Charlie Gordon wrote:
.... snip ...
>>
Really ? I don't think so:

7.4.1.5p2: The isdigit function tests for any decimal-digit
character (as defined in 5.2.1).

5.2.1p3: the 10 decimal digits 0 1 2 3 4 5 6 7 8 9

They're "contiguous" on ASCII or BCDIC based character sets, but
on other character sets, they may not be.
Then they are not the 'decimal digits' referred to in the
standard. Simple. If you have some unusual chars you have to
translate them first. If you have unusual bases you have to write
the appropriate code. etc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 11 '07 #22
On Mon, 10 Sep 2007 13:28:14 -0700, Keith Thompson wrote:
As a practical matter, you can't get a legible representation of each
of the required printable characters i a 7-segment LED display, though
you could encode each character.
In such an hypothetical implementation, putchar()ing any character
which can't be sensibly displayed on such a display could always
return EOF and set the ferror() flag on.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 11 '07 #23
On Mon, 10 Sep 2007 15:15:35 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
"Douglas A. Gwyn" wrote:
Actually the C standard does require the digit codes to be contiguous
and ascending, <snip>
I argued against this requirement, which infringes on the codeset
designer's turf, but lost the argument. <snip>

Baudot does not have the digits as contiguous values. But, then
again, it is also a 6 bit character set, whereas C specifies a
minimum of 8.
s/6/5/. And Baudot/IA2 is also shift-state dependent, which doesn't
fit in well with C's model, and tradition, of 'you can access any char
to which you have a (valid) pointer'.

FWIW, TeleTypeSetter is 6-bit, also shift-state dependent (actually
multiple states in many usages), and discontiguous -- because, like
Baudot/IA2, it was designed to minimize 'marks' for average text. I
know this reduced aggregate wear on (re)perferators; I never got a
straight answer on whether it saved anything significant in
power/battery use and/or cable (and perhaps cableway) heating.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Sep 23 '07 #24

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

Similar topics

19
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
by: Henry Jordon | last post by:
This is a piece of code that I have left to complete my project. I have hopefully one small error that needs to be fixed. This portion of the code evaluates the postfix notation that is passed to...
6
by: Gaijinco | last post by:
I was trying to solve: acm.uva.es/p/v101/10191.html I did this code: #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <cctype>
34
by: Zulander | last post by:
Hi, have a problem with comparing text in my function for some reason my code get hang at fgets. i am uploading this code in an IC, but i have isolated the problem to fgets(SettingInfo); could...
2
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; ...
9
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; ...
15
by: vivekian | last post by:
Hi, I have this following class class nodeInfo ; class childInfo { public: int relativeMeshId ;
2
by: James Salisbury | last post by:
Hi, Does anyone have a good page and some exercises on the use of pointers? The compiler is Source Boost and the target is an 18f picr. I am trying to find out why the code below won't compile....
50
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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
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
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...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.