strtoint

O

Old Wolf

Emmanuel Delahaye said:
Profetas wrote on 25/07/04 :
does this function exist in c? on any equivalent? that convert string to
integer?

how do I convert a array to a "string"
example
char arr['a','b','c'];

This is not C.

Actually it is; if the system uses ASCII then it declares an
array of 99 chars.
 
E

Emmanuel Delahaye

Profetas wrote on 26/07/04 :
My goal was to generate a random string, I knew how to generate random
numbers, any way here is my solution.

for(i=0; i<16; i++)
{
var_ptr=65 + rand()%25;
}


Assuming ASCII, you can replace 65 by 'A' and it will work. The trap is
that you have to add a final 0 to make the array of char, a string [1].

A more portable way should be:

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

int main (void)
{
char s[16 + 1];
size_t i;

static char a_alpha[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

srand ((int) time (NULL));

for (i = 0; i < sizeof s - 1; i++)
{
s = a_alpha[rand () % sizeof a_alpha]; /* assuming A-Z */
}

s = 0;

printf ("s = '%s'\n", s);

return 0;
}

Output:

s = 'NYMLWOBQXZNGHQIQ'

s = 'LWNWXNLMZIXIBMLF'

---------------------
[1] Maybe it was obvious to you, but in that case, you should have
shown it more clearly.

Additionally, other people, mainly newbies, are reading the newsgroup,
and if they see a code without corrections, they tend to read it as a
valid code. This is why my ethic leads me to avoid to leave bad or
incomplete code without a comment.
 
E

Emmanuel Delahaye

Profetas wrote on 26/07/04 :
Well they aren't so magic 65 is equal to A

In ASCII only.
and 25 is the range
of the Alphabet :) so the generated string will be between
(A-Z)

Used to be 26 to me, but maybe we don't have the same alphabet (I
learnt recently that some letters don't exist in Italian for
example)...
 
G

Gordon Burditt

C does not guarantee ANY of:
What do you mean by this, given 5.2.1p3...

Both the basic source and basic execution character sets
shall have the following members: the 26 uppercase letters
of the Latin alphabet
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
the 26 lowercase letters of the Latin alphabet
a b c d e f g h i j k l m
n o p q r s t u v w x y z
...

I said: C does not guarantee ANY of
.... there are no unaccented letters in the character set.

Since your quote from the standard demonstrates that there
are at least 52 unaccented letters, it proves my point.

Actually, I meant "C does not guarantee that there are no accented
letters in the character set", but with the mistake the result was
still true.

Gordon L. Burditt
 
C

Chris Torek

Profetas wrote on 25/07/04 :

Actually it is; if the system uses ASCII then it declares an
array of 99 chars.

Even in C99, a constant-expression cannot contain a comma-expression:

[#3] Constant expressions shall not contain assignment,
increment, decrement, function-call, or comma operators,
except when they are contained within the operand of a
sizeof operator.83

I have never understood why comma-expressions are forbidden,
although certainly for the code in ">>>" above, a warning is
quite desirable even in non-Standard modes in which comma-expressions
*are* allowed in constants.
 
K

Keith Thompson

Al Bowers said:
This news group is dedicated to the language as defined by the
C Standard(ISO/IEC 9899:1999). Again, I point out that in this
newsgroup, with readers that use various implementations, that
your assertion that 'A' is value 65 is idiotic.

No, it's not idiotic. It's merely incorrect.
 
K

Keith Thompson

Chris Torek said:
Profetas wrote on 25/07/04 :
char arr['a','b','c'];

Actually it is; if the system uses ASCII then it declares an
array of 99 chars.

Even in C99, a constant-expression cannot contain a comma-expression:

[#3] Constant expressions shall not contain assignment,
increment, decrement, function-call, or comma operators,
except when they are contained within the operand of a
sizeof operator.83

Right, but in C99 the expression between the square brackets doesn't
have to be a constant-expression, so
char arr['a','b','c'];
declares a variable length array (even though the length can be
determined at compilation time).
I have never understood why comma-expressions are forbidden,
although certainly for the code in ">>>" above, a warning is
quite desirable even in non-Standard modes in which comma-expressions
*are* allowed in constants.

Probably because a constant-expression with a comma operator most
likely indicates a bug. The left operand of a comma operator is
evaluated only for its side effects, and a constant-expression can't
have side effects.
 
C

Chris Torek

Right, but in C99 the expression between the square brackets doesn't
have to be a constant-expression, so
char arr['a','b','c'];
declares a variable length array (even though the length can be
determined at compilation time).

True, but only if "arr" appears in a context in which VLAs are
allowed. So it is always invalid in C89, and sometimes valid
in C99:

char wrong[1,2]; /* error in both C89 and C99 */
void fn(void) {
char ok_and_VLA[1,2]; /* error in C89, otherwise VLA of size 2 */
...
}

Interestingly, one can test for "VLA-ness" at runtime by applying
the sizeof operator, whose operand *is* evaluated if the operand
is a VLA. I am not quite sure whether this can be used to detect
whether a given C99 compiler treats (1,2) as a constant-expression
in the "ok_and_VLA" case above.
 
A

Al Bowers

Chris said:
Profetas wrote on 25/07/04 :

char arr['a','b','c'];

Emmanuel Delahaye said:
This is not C.


Old Wolf said:
Actually it is; if the system uses ASCII then it declares an
array of 99 chars.


Even in C99, a constant-expression cannot contain a comma-expression:

[#3] Constant expressions shall not contain assignment,
increment, decrement, function-call, or comma operators,
except when they are contained within the operand of a
sizeof operator.83

Because of the comma operator, the expression, ('a','b','c') cannot
be a constant expression. However, vla's do not require a constant
expression and in my opinion the non-constant expression would
be nothing other than a poor construct of the array.

In defect report 261:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_261.htm
I quote:

"The Committee also agrees that:

int fla [(0+5)]; // is a normal array, not variably modified
int vla [(0,5)]; // is a variable length array"
 
R

RoSsIaCrIiLoIA

On Sun, 25 Jul 2004 18:51:52 -0400, "Profetas" wrote:
profetas>does this function exist in c? on any equivalent? that
profetas>convert string to
profetas>integer?
profetas>
(***)
profetas>how do I convert a array to a "string"
profetas>example
profetas>char arr['a','b','c'];
profetas>char *arr2;
profetas>
profetas>how can I make arr2 = arr ?
(*)
Do I have to explain every single detail so you can understand?
I didn't ask if c had string I didn't ask if my array was an string did
I?
I didn't ask you how to allocate memory did I?, I ask if there is ano
function that convert a string to integer, ok now you are gonna write 20
pages saying that c has no string assuming that I don't know, man you
don't know what people know so don't judge people if you didn't understand
their question just ignore the question if you did understand the question
and you want to answer do so, but only answer the question asked.

I have seen only from (***) to (*) and have jump the first question
and the subjet of message. So one/two answer?
 
S

Shug Boabie

RoSsIaCrIiLoIA wrote:
I have seen only from (***) to (*) and have jump the first question
and the subjet of message. So one/two answer?

#include <stdlib.h>
int atoi(const char *nptr);
 
E

Emmanuel Delahaye

Shug Boabie wrote on 28/07/04 :
RoSsIaCrIiLoIA wrote:


#include <stdlib.h>
int atoi(const char *nptr);

The ato*() functions are obsolete since 1989. Go for the strto*() ones.
 
S

Shug Boabie

Emmanuel said:
Shug Boabie wrote on 28/07/04 :

The ato*() functions are obsolete since 1989. Go for the strto*() ones.

hmm, in that case it is a bit annoying that gcc on `-pedantic -Wall -Werror
-std=c99' doesn't even mention this...
 
D

Dave Vandervies

Emmanuel Delahaye wrote:

hmm, in that case it is a bit annoying that gcc on `-pedantic -Wall -Werror
-std=c99' doesn't even mention this...

`Obsolete' is a bit too strong; they're still part of the standard.
strtol and friends have better error handling capabilities and well-
defined behavior in all cases, though, while atoi and friends invoke
undefined behavior on overflow (so you're responsible that you only give
it input that's in range).


dave
 
A

Alex Monjushko

hmm, in that case it is a bit annoying that gcc on `-pedantic -Wall -Werror
-std=c99' doesn't even mention this...

"Deprecated by the standard" and "obsolete" can mean different things.
There is almost no excuse to use ato* functions when the much better
designed strto* functions are available.
 

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

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top