Problem with atoi()

T

tolkien

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"
 
A

Army1987

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.
 
J

Joe Wright

tolkien said:
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*.
 
T

tolkien

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)
 
R

runner

tolkien said:
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[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.
 
M

Malcolm McLean

tolkien said:
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.
 
K

Keith Thompson

Malcolm McLean said:
tolkien said:
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().
 
A

Army1987

Malcolm McLean said:
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.
 
K

Keith Thompson

Army1987 said:
Malcolm McLean said:
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).
 
C

Charlie Gordon

Army1987 said:
Malcolm McLean said:
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.
 
P

pete

Malcolm said:
tolkien said:
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';
}
 
P

pete

Charlie said:
Army1987 said:
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.
 
C

Charlie Gordon

pete said:
Charlie said:
Army1987 said:
On Sun, 09 Sep 2007 14:56:38 -0700, Keith Thompson 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

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.
 
A

André Gillibert

Charlie said:
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]
 
A

André Gillibert

André Gillibert said:
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.
 
R

Richard Heathfield

André Gillibert said:

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>
 
R

Richard Bos

Charlie Gordon said:
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
 
A

André Gillibert

Richard said:
André Gillibert said:



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.
 
D

Douglas A. Gwyn

André Gillibert said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

atoi 11
Print with command-line arguments 0
a question abou "atoi" 6
problematic atoi conversion 2
Problem with codewars. 5
A Minor Problem With atoi() And Negative Numbers 12
atoi query 13
integer overflow in atoi 29

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top