Re: int conversion from char
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sat, 16 Jul 2005 21:49:45 GMT
"Nitin" <mcabhardwaj@xxxxxxxxx> writes:
> Alex Maceda wrote:
>> In writing a function that take as it's arguments a string and the length
>> of the string. I need to change the char input array to an integer array
>> for the operation to succed. For some reason I can't get atoi, sscanf, or
>> an extremly clumsy "while" loop to work. What is the standard method for
>> doing this?
[...]
>
> Firstly, you cannot pass a string to a function , only the base address
> is what really gets passed ( unless u wrap it in a struct object ).
Please don't use abbreviations like 'u'; they only make it more
difficult to read your article.
> Secondly, what do you mean by "convert to integer" ? chars in C are
> really ints - corresponding to the numerical value of the character-set
> on your platform. e.g. if your platform uses ASCII, a char '0' is
> really stored as 65 !
It's important to distinguish between the terms "int" and "integer".
"int" refers to a specific type; "integer" is a more generic term that
covers signed and unsigned types including char, int, long long, and
so forth.
To the OP, it's difficult to tell just what you're tring to do. As
Nitin points out, a string is already an array of integers.
Are you trying to convert a string such as "123 234 345" to an array
of int with the values 123, 234, and 345? If so, show us what you've
tried so far, and maybe we can help. You should also define precisely
what you're trying to do. For example, how are the integers delimited
in the string? Single space? One or more spaces? Commas? Do you
want to allow '-' signs? '+' signs? Hexadecimal or octal formats?
How do you want to handle overflow? The size of the result array is
going to depend on the value of the string; how are you going to
allocate it?
I don't think there's necessarily a "standard way" to do this.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.
- References:
- int conversion from char
- From: Alex Maceda
- Re: int conversion from char
- From: Nitin
- int conversion from char
- Prev by Date: Re: Is there any ANSI method to extract/tokenize argv[ ]
- Next by Date: Re: Is there any ANSI method to extract/tokenize argv[ ]
- Previous by thread: Re: int conversion from char
- Next by thread: Re: int conversion from char
- Index(es):
Relevant Pages
|