Thread: Refrencing Array

  1. #1
    Kyle
    Join Date
    Jan 2005
    Posts
    5

    Refrencing Array

    Hi this is my first post here and im really hoping i can get a little help on something which my be really easy anyways ....
    I have never used typedef before i need to declare a typedef for a char acter array of 3 chars.

    would this be correct:
    typedef char value_type[3];

    also i have to write a class and one of the private member variable is a array of type value_type from above. I need to write an accessor function it display the contents of the array, but you can not return arrays in c++, so would i go about return a refrence to that array??

    Any help would be appreciated Thanks!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Read this about typedefs
    http://www.cplusplus.com/doc/tutorial/tut3-6.html.

    For your second question I believe that you could just use a loop if i am reading your question right.
    eg.
    Code:
    #include <iostream>
    
    class myClass
    {
      public:    
        myClass()
        {
          int i;
          
          for(i = 0; i < 3; i++)
          {
            
              myArray[i] = i + 2;
              
          }
        }
        
        void printValues()
        {
          int i;
          
          for(i =0; i < 3; i++)
          {
            
            std::cout<<myArray[i]<<std::endl;
            
          }
          
        }
        private:
          int myArray[3];
        
    };
    
    int main(void)
    {
      
      myClass myMain;
      myMain.printValues();
      std::cin.get();
      
      return 0;
    }
    Woop?

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Addressing your second question, if you want to pass an array of value_type, then you can accept an argument of type value_type*. Consider the following code:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    typedef char value_type[3];
    
    void do_stuff(value_type *arr)
    {
        memcpy(arr[0],"ab",3);
        memcpy(arr[1],"cd",3);
        memcpy(arr[2],"ef",3);
        memcpy(arr[3],"gh",3);
        memcpy(arr[4],"ij",3);
    }
    
    int main()
    {
        value_type a[5];
        memset(a,0,15);
        do_stuff(a);
        cout << a[0] << endl;
        cout << a[1] << endl;
        cout << a[2] << endl;
        cout << a[3] << endl;
        cout << a[4] << endl;
        return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by Perverse
    Hi this is my first post here and im really hoping i can get a little help on something which my be really easy anyways ....
    I have never used typedef before i need to declare a typedef for a char acter array of 3 chars.

    would this be correct:
    typedef char value_type[3];

    also i have to write a class and one of the private member variable is a array of type value_type from above. I need to write an accessor function it display the contents of the array, but you can not return arrays in c++, so would i go about return a refrence to that array??

    Any help would be appreciated Thanks!

    Regarding the second question....having a pointer that points to the address of that array would work fine. In the getArray() (Or whatever the accessor function is), just return that pointer....or you can de-reference it and print it out...

    Anyhoo, a pointer would accomplish what you want to do by pointing to the address of the first element in the array.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Perverse

    would this be correct:
    typedef char value_type[3];

    Any help would be appreciated Thanks!
    Well, it is valid C code and it is valid C++ code. Whether it is correct or not pretty much depends on what the question was.

    (It says that variables declared as value_type are arrays of three chars.)

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
      typedef char value_type[3];
      value_type x = {'a', 'b', 'c'};
      value_type y;
      unsigned int i;
    
      cout << "sizeof(x) = " <<  sizeof(x) << endl;
      for (i = 0; i < sizeof(x); i++) {
        y[i] = x[i]+3;
      }
    
      for (i = 0; i < sizeof(x); i++) {
        cout << "x[" << i << "] = " << x[i] 
             << ", y[" << i << "] = " << y[i] << endl;
      }
    
      return 0;
    }
    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM