473,836 Members | 1,582 Online

# strtoint

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'];
char *arr2;

how can I make arr2 = arr ?

Nov 14 '05
35 19635
"Gordon Burditt" <go***********@ burditt.org> wrote in message
news:ce******** @library2.airne ws.net...
trouble me. Why not make an array of the set of characters you want
to be in the random string and then randomly select from the set of

Well they aren't so magic 65 is equal to A and 25 is the range
of the Alphabet :-) so the generated string will be between
(A-Z)

Yes, they *ARE* magic.

C does not guarantee ANY of:

<snip>
The alphabet contains 25 or 26 letters.
There are no unaccented letters in the character set.
<snip>

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

--
Peter
Nov 14 '05 #21
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[i]=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] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";

srand ((int) time (NULL));

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

s[i] = 0;

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

return 0;
}

Output:

s = 'NYMLWOBQXZNGHQ IQ'

s = 'LWNWXNLMZIXIBM LF'

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

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.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #22
Profetas wrote on 26/07/04 :
Well that is not a bad approach. However the magic numbers 65 and 25
trouble me.
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)...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #23
Old Wolf wrote on 26/07/04 :
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.

Error MAIN.C 5: Array bounds missing ]

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #24
>> C does not guarantee ANY of:

<snip>
The alphabet contains 25 or 26 letters.
There are no unaccented letters in the character set.
<snip>

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
Nov 14 '05 #25
>> Profetas wrote on 25/07/04 :
> char arr['a','b','c'];
Emmanuel Delahaye <em***@YOURBRAn oos.fr> wrote:
This is not C.

In article <84************ **************@ posting.google. com>
Old Wolf <ol*****@inspir e.net.nz> writes: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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #26
Al Bowers <xa******@rapid sys.com> writes:
[...]
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.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #27
Chris Torek <no****@torek.n et> writes:
Profetas wrote on 25/07/04 :
> char arr['a','b','c'];
Emmanuel Delahaye <em***@YOURBRAn oos.fr> wrote:
This is not C.

In article <84************ **************@ posting.google. com>
Old Wolf <ol*****@inspir e.net.nz> writes:
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.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #28
>Chris Torek <no****@torek.n et> writes:
Even in C99, a constant-expression cannot contain a
comma-expression ...

In article <ln************ @nuthaus.mib.or g>
Keith Thompson <ks***@mib.or g> writes: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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #29

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

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

Emmanuel Delahaye <em***@YOURBRAn oos.fr> wrote:
This is not C.

In article <84************ **************@ posting.google. com>
Old Wolf <ol*****@inspir e.net.nz> writes:
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/wg...ocs/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"
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #30

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