strtoint

P

Profetas

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 ?
 
E

Emmanuel Delahaye

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.

char arr[] = {'a','b','c'};

But it's not a string.
char *arr2;

how can I make arr2 = arr ?

arr2 is just a pointer. You are allowed to make it point to an array f
char, but it doesn't make it a string.

Bare in mind that in C, '=' is the affectation operator, and that '=='
is the equal operator (works with numerical values)

If you want to build a string with the caracters of the array of char,
you can do this :

/* allocate room enough */
size_t const len = sizeof arr;
char *arr2 = malloc (len + 1); /* <stdlib.h> */

if (arr2 != NULL)
{
/* make the string */
*arr2 = 0;
strncat (arr2, arr, len);

<...>

/* when finished */
free (arr2), arr2 = NULL;
}
 
M

Martin Ambuhl

Profetas said:
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 ?
Here we have an example of someone who has not bothered to check even
the most elementary textbook.

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

int main(void)
{
char arr[3] = "abc";
char *arr1, *arr2, *arr3, *arr4;
size_t i;

arr1 = arr;
printf("I have set arr1=arr. Let's check:\n");
for (i = 0; i < sizeof arr; i++)
printf("arr[%lu] = %c, arr1[%lu] = %c\n", (unsigned long) i,
arr, (unsigned long) i, arr1);
putchar('\n');

if (!(arr2 = malloc(sizeof arr))) {
fprintf(stderr, "malloc for arr2 failed. Quitting.\n");
exit(EXIT_FAILURE);
}
memcpy(arr2, arr, sizeof arr);
printf("I have allocated space for arr2, and copied arr to it.\n"
"Let's check:\n");
for (i = 0; i < sizeof arr; i++)
printf("arr[%lu] = %c, arr2[%lu] = %c\n", (unsigned long) i,
arr, (unsigned long) i, arr2);
putchar('\n');
free(arr2);

if (!(arr3 = malloc(1 + sizeof arr))) {
fprintf(stderr, "malloc for arr3 failed. Quitting.\n");
exit(EXIT_FAILURE);
}
memcpy(arr3, arr, sizeof arr);
arr3[sizeof arr] = 0;
printf("In neither of the above do we have a string.\n"
"I have allocated space for arr3, and copied arr "
"and a terminating 0 to it.\n" "Let's check:\n");
for (i = 0; i < sizeof arr; i++)
printf("arr[%lu] = %c, arr3[%lu] = %c\n", (unsigned long) i,
arr, (unsigned long) i, arr3);
printf("arr3=\"%s\"\n", arr3);
putchar('\n');

if (!(arr4 = malloc(1 + strlen(arr3)))) {
fprintf(stderr, "malloc for arr4 failed. Quitting.\n");
exit(EXIT_FAILURE);
}
strcpy(arr4, arr3);
printf
("I have allocated space for arr4, and copied the string "
"arr3 to it.\n" "Let's check:\n");
printf("arr3=\"%s\", arr4 = \"%s\"\n", arr3, arr4);
putchar('\n');
free(arr3);
free(arr4);
return 0;
}


I have set arr1=arr. Let's check:
arr[0] = a, arr1[0] = a
arr[1] = b, arr1[1] = b
arr[2] = c, arr1[2] = c

I have allocated space for arr2, and copied arr to it.
Let's check:
arr[0] = a, arr2[0] = a
arr[1] = b, arr2[1] = b
arr[2] = c, arr2[2] = c

In neither of the above do we have a string.
I have allocated space for arr3, and copied arr and a terminating 0 to it.
Let's check:
arr[0] = a, arr3[0] = a
arr[1] = b, arr3[1] = b
arr[2] = c, arr3[2] = c
arr3="abc"

I have allocated space for arr4, and copied the string arr3 to it.
Let's check:
arr3="abc", arr4 = "abc"
 
R

RoSsIaCrIiLoIA

Profetas said:
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;

char arr[3]={ 'a', 'b', 'c'}, *arr2;

you can make it
Here we have an example of someone who has not bothered to check even
the most elementary textbook.

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

int main(void)
{
char arr[3] = "abc";
Is this right?
or it is better this?
char arr[] = "abc";
or this
char arr[4]= "abc"; /* arr[4]=={a, b, c, 0}*/
?
If I write "abc" there will be somewhere in the executable
'a''b''c'0?
 
E

Emmanuel Delahaye

RoSsIaCrIiLoIA wrote on 25/07/04 :
int main(void)
{
char arr[3] = "abc";
Is this right?

Yes. But it's not a string. It's an initialized array of 3 char.
or it is better this?
char arr[] = "abc";

It's not 'better', it's different. It's a string.
or this
char arr[4]= "abc"; /* arr[4]=={a, b, c, 0}*/

This is a redundent version of the above one.
?
If I write "abc" there will be somewhere in the executable
'a''b''c'0?

Yes, except in this case:

char arr[3] = "abc";
 
R

RoSsIaCrIiLoIA

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 ?

the string end with 0 [= \0]

char arr[3]={'a', 'b', 'c'}, *arr2;
unsigned i;

arr2 = malloc(1 + sizeof arr);
if(arr2 == 0) return EXIT_FAILURE;
for(i = 0; i< sizeof arr; ++i)
arr2=arr;
arr2 = 0;
use (arr2);
free(arr2);
return EXIT_SUCCESS
 
P

Profetas

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

Emmanuel Delahaye

Profetas wrote on 26/07/04 :
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.

Excuse me sir, but here is a quote of your original post:

how do I convert a array to a "string"

Followed by a weird way of defining an array of char that displayed
some lack of knowledge about what an array or a string are. Hence, we
tried to make this point clear.

That said, we probably missed this one:

does this function exist in c? on any equivalent? that convert string
to
integer?

that was unclear because it was referring to the subject line, which is
obviously not the best way to establish a clear communication stream
between you and the readers. Better to ask a full and complete question
in the body of the message.

To answer to your initial question, yes, there are strtol(), strtoul()
and eventually strtod() the day you are interested in floating points.
 
P

Profetas

Excuse me sir, but here is a quote of your original post
Sorry but it was meant to the people who act the way I described, when I
read the posts I don't read the names and I don mark people, I have posted
very few questions in the c programming language, as I spend most of my
time in the
Assembly x86 where the people is far more “mature”.

I am not saying I wasn't wrong, I know I am lazy while explaining what I
have in mind but people who think that they are the masters and everyone
else is newbie irritates me.

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;
}
 
J

jacob navia

Martin Ambuhl said:
Here we have an example of someone who has not bothered to check even
the most elementary textbook.


Yes. And that is why I didn't bother. Why should we
bother if he doesn't?

Besides, he could have friends...

"Hey man, do not bother to work/learn anything. Just ask in c.l.c and
they will fix it for you..."
 
A

Al Bowers

Profetas said:
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;
}


Well that is not a bad approach. However the magic numbers 65 and 25
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
characters.

Example:

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

#define SZ_STRING 16

int main( void )
{
char *source = "AaBbCcDdEeFfGgHhIiJjKkLlMmNn"
"OoPpQqRrSsTtUuVvWwXxYyZz0123456789";
char randomstr[SZ_STRING+1];
int i,slen;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
for(i = 0,slen = strlen(source);i < SZ_STRING;i++)
randomstr = *(source + rand()%slen);
randomstr[SZ_STRING] = '\0'; /* Nul terminate to make a string */
printf("The random string generated is \"%s\"\n",randomstr);
return 0;
}
 
P

Profetas

Well that is not a bad approach. However the magic numbers 65 and 25
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)
 
P

Peter Nilsson

Emmanuel Delahaye said:
Bare in mind that in C, '=' is the affectation operator,

[On first reading I thought you said affection operator, which I could
really do with. ;-]

I think 'affectation' must be a French/English faux ami.

http://www.m-w.com/cgi-bin/dictionary?affectation
and that '==' is the equal operator (works with numerical values)

It's the 'equal to' equality operator, not to be confused with the simple
assignment operator which consists of a single 'equal' character. Sorry for
the pedantism, but I've heard the assignment operator naively called the
'equal operator' by many newbies, so I think it's worth mentioning what the
true terms and differences are.

Note that equality operators also work operands with with pointer types, as
well as arithmetic types.
 
G

Gordon Burditt

trouble me. Why not make an array of the set of characters you want
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:

'A' == 65
'A' + 25 == 'Z'
The character set is ASCII.
The alphabet contains 25 or 26 letters.
There are no unaccented letters in the character set.
Alphabetic characters form a contiguous block of consecutive codes.

Gordon L. Burditt
 
A

Al Bowers

Profetas said:
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)

You know, I have just searched the standard, and I find no mention
of the value of 'A' being 65. But I did find in this paragraph

5.2.1.1
"The values of the members of the execution character set
are implementation-defined."

So you may be using a implementation-defined solution.

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

Chris Torek

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)

Except that 'A' is 193 (0xC1), on that IBM mainframe using EBCDIC.
(And yes, it has a C compiler.) Not only that, while 'A'+1 is 'B',
and 'B'+1 is 'C', there is an odd gap so that 'I'+1 is not 'J' --
'J' is coded as 0xD1. There is another gap before the end of the
alphabet, and '0' is coded as 0xF0.
 
E

Emmanuel Delahaye

Peter Nilsson wrote on 26/07/04 :
I think 'affectation' must be a French/English faux ami.

Oops, I meant assignment, of course. Sorry for the Frenchism!
 
P

Peter Nilsson

Gordon Burditt said:
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
...
 

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