Convert array element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samitha1010
    New Member
    • Mar 2007
    • 1

    #1

    Convert array element

    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>
    Last edited by sicarie; Mar 8 '07, 06:10 PM. Reason: Removed email address per FAQ
  • andersod
    New Member
    • Mar 2007
    • 9

    #2
    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

    Comment

    • Extremist
      New Member
      • Jan 2007
      • 94

      #3
      Remember code tags:
      Code:
      #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);
      }
      Could you perhaps explain what happens at
      Code:
       void *x = NULL;
      Why are you using that?
      I'm a C++ girl so I'm just wondering ;-)

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by samitha1010
        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
        Code:
            char ch[]={'A','B','C'};
            int i;
            for(i=0; i<3;i++)
              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

        Comment

        • andersod
          New Member
          • Mar 2007
          • 9

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

          Comment

          • andersod
            New Member
            • Mar 2007
            • 9

            #6
            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("charact er %c integer value %d\n", ch[i], val);
            }
            desmond

            Comment

            Working...