datatype to store both char n int in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luking4ans
    New Member
    • Mar 2008
    • 1

    datatype to store both char n int in an array

    hi, wat datatype to use if i want to store both integers & char in an array? can i use String...and how to use String?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by luking4ans
    hi, wat datatype to use if i want to store both integers & char in an array? can i use String...and how to use String?
    You should go for a structure which contains the members char and int and use the structure array to store the values

    raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You won't be able to use String with integers or array members that are struct variables.

      Comment

      • Sreeramu
        New Member
        • Apr 2007
        • 11

        #4
        I think you can use int data type to store both integer and character data type...
        Eg:int data = 'A';

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          A char and an int are both integers. The char is 8 bytes and the int is at least 16 but may be more.

          There are no character variables.

          That means an int with 65 can be displayed as A just as well as a char with 65. It's just that functions with char arguments assume you want to see the A whereas functions with int arguments assume you want to see 65.

          The syntax 'A' is an integer syntax. It tells the compiler to look up the value corresponding to A in the ASCII table and to use that value. In this case, 65.

          Comment

          • kooljay999
            New Member
            • Feb 2012
            • 1

            #6
            Thanks

            Thanks... It worked for me ... i created a structure and its array as u told...
            struct maze{int x;char y;};
            void main ()
            {maze a[10][10];

            //to store in int part
            cin>>a[0][0].x;
            //to store in char part
            cin>>a[0][0].y;

            //to access int part
            cout<<a[0][0].x;
            //to access char part
            cout<<a[0][0].y;


            }

            Comment

            Working...