By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

Convert array element

P: 1
I need to convert an array element (char type) to a int variable.Need to know how i cast it.language is c. If any one have an idea about this please send me a reply. <email address removed per Posting Guidlines>
Mar 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: 9
Hi,
Try defining the array variable as type void, then typecast it to the different types.

ex.
#include <stdio.h>
#include <stdlib.h>
int main(){
void *x = NULL;
x = (int *)1;
printf("the value is %d\n",(int *)x);
x = (char *)'A';
printf("the character is %c\n",(char*)x);
}

result:
the value is 1
the character is A


Hope this helped.

Desmond
Mar 8 '07 #2

P: 94
Remember code tags:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(){
  5.    void *x = NULL;
  6.    x = (int *)1;
  7.    printf("the value is %d\n",(int *)x);
  8.    x = (char *)'A';
  9.    printf("the character is %c\n",(char*)x);
  10. }
  11.  
Could you perhaps explain what happens at
Expand|Select|Wrap|Line Numbers
  1.  void *x = NULL;
  2.  
Why are you using that?
I'm a C++ girl so I'm just wondering ;-)
Mar 8 '07 #3

Expert 100+
P: 1,510
I need to convert an array element (char type) to a int variable.Need to know how i cast it.language is c. If any one have an idea about this please send me a reply. <email address removed per Posting Guidlines>
character variables are stored in C as byte sized integers so there is no need to cast between the types
e.g. consider this fragment of code when contains an array of char, the elements of which are printed as character using %c and decimal integer using %d
Expand|Select|Wrap|Line Numbers
  1.     char ch[]={'A','B','C'};
  2.     int i;
  3.     for(i=0; i<3;i++)
  4.       printf("character %c integer value %d\n", ch[i], ch[i]);
when run gives
character A integer value 65
character B integer value 66
character C integer value 67

the ASCII character code for A is 65 decimal
Mar 8 '07 #4

P: 9
hi,
You said in you initial statement that the language is c. I assumed you wanted c version.

Anyway, it does not matter. "void *" takes anything. This allows you to store and retrieve any data type that you want by type casting it when storing, retrieving, allocating,and distroying the memory area. "void *" gives you more control of you datatypes.
It also works perfectly in c++. I am also a c++ developer.

There are also other ways.

desmond.
Mar 8 '07 #5

P: 9
hi,,
you could also do the following:

char ch[]={'A','B','C'};
int i;
for(i=0; i<3;i++) {
int val = (int)ch[i];
printf("character %c integer value %d\n", ch[i], val);
}
desmond
Mar 8 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.