Computing.Net > Forums > Programming > Change value in char array in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Change value in char array in C

Reply to Message Icon

Name: seeJ
Date: April 15, 2004 at 17:47:22 Pacific
OS: linux
CPU/Ram: 1.3
Comment:

Im fairly new to C. I have a mysql query that has variables in it that are type char and they have to be.
Im wondering if i have a while loop
ex.
i=32;
while (i <= 500){
str_concat(sqlblah blah,
i,
null);
}
i++;

in the sql stuff that value of i needs to be of type char.So basically how can i make a char variable equal to my integer but still stay a char and then increment with the integer in the while loop.
Is this possible?
Any insight would be great.
Feel free to email if you need more explaination.




Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: April 15, 2004 at 23:42:52 Pacific
Reply:

If I understand you correctly...
Yes, this is possible. You can
throw a character type cast on your integer.

int y = 3;
char x = (char) y;

This will put the character representation of the number 3 into the character variable x based on the value of y.

IR


0

Response Number 2
Name: Hoang Tran
Date: April 16, 2004 at 03:41:46 Pacific
Reply:

It looks like you'll need to declare an array of char then use sprintf to convert the number i into a string before you can use string concatenation. I assume that is in C and not C++.


0

Response Number 3
Name: Don Arnett
Date: April 16, 2004 at 07:27:46 Pacific
Reply:

IR

It must have been past your bedtime when you answered this question. (I'm sure that you know the stuff below and just mispoke, so my descriptions are geared more to others who don't realize these things.)

Casting doesn't change the data that is cast, it just tells the compiler that it is OK to view the data as a different data type.

The value of the character 3 is 0x33 (meaning 33 hex), while obviously the value of the integer 3 is 0x03.

Your answer says that the 0x03 stored in y will be converted to 0x33 when it is cast into x.

Not true.

x will contain 0x03 also.


But your basic answer is correct (that you can use a char variable like an integer), I believe. I really didn't understand the question asked, so...

But a char variable can be used like an integer. A char can be treated just like a one byte integer when desired. How you use the data determines if it seems to be an integer or a character.

Try this:

char c;

c=65;
printf("c = %d\n",c); /* c = 65 */
printf("c = %c\n",c); /* c = A */

c = c + 5;

printf("c = %d\n",c); /* c = 70 */
printf("c = %c\n",c); /* c = F */

c = 'B' + 1;
printf("c = %d\n",c); /* c = 67 */
printf("c = %c\n",c); /* c = C */


The value inside the char variable is simply an integer, it depends upon how you use it whether it appears to be an integer or a character.


0

Response Number 4
Name: seeJ
Date: April 16, 2004 at 07:35:24 Pacific
Reply:

Thank you for your help. I used sprintf() and that worked perfect. It placed the decimal value of i = 32 into str = "32" like i needed. I could have also used itoa() to do the same thing. Recasting would have given me a character representation of 32 rather than "32". Thank you for your answers.

C


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


emailing hyperlinks with ... c++ open internet explore...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Change value in char array in C

v.c++changing cstring to char array www.computing.net/answers/programming/vcchanging-cstring-to-char-array/14282.html

Explode Array In C www.computing.net/answers/programming/explode-array-in-c/3474.html

How to use string array in C++? www.computing.net/answers/programming/how-to-use-string-array-in-c/9478.html