Re: int conversion from char



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



Relevant Pages

  • Re: is there an alternative to strstr
    ... >> To exploit this fact, you need a different data format, a plain string is ... > Ok I have put the email ids in a sorted array. ... However you can use an array of char pointers and use ... int cmp(const void *v1, const void *v2); ...
    (comp.lang.c)
  • Re: Pointers on string members of structure
    ... because this just points memstr to a fixed string and it is undefined to ... char memstrA; ... string array directly like this and how? ... struct or if the struct member points to the array. ...
    (microsoft.public.vc.language)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: K&R2, exercise 5.4
    ... int strend(char*, char*); ... Now ps points to the first character of a string which is one character ... * don't want to FAKE the array call ... outer for loop checks for each element of 1st array. ...
    (comp.lang.c)
  • Re: Arrays of Strings, malloc
    ... Each string is at most 50 characters long, ... How do I actually start the array? ... an "array of pointers" and a "pointer ... Allocate space for some number of pointers to char ...
    (comp.lang.c)