Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer array access outside a class!

Pointer array access outside a class!

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CreepingFeature
    wrote on last edited by
    #1

    Hi everyone! First, I appologise for my last post :-O! Anyway, got this here code in main(): int number1 = 5; int number2 = 10; int *p_number1 = &number1; int *p_number2 = &number2; int *arrayAddress; int *pointerArray[2]; pointerArray[0] = p_number1; pointerArray[1] = p_number2; cout << " Pointer array at [0]: " << pointerArray[0]; cout << endl; cout << " Pointer array at [1]: " << pointerArray[1]; cout << endl; cout << endl; cout << " Pointer array deref at [0]: " << *pointerArray[0]; cout << endl; cout << " Pointer array deref at [1]: " << *pointerArray[1]; cout << endl; cout << endl; arrayAddress = pointerArray[0]; cout << endl; cout << " Array address: " << arrayAddress; cout << endl; cout << " Array access through address: " << arrayAddress[0]; It compiles and the last statment prints 5 the actual value at the address that is stored in pointerArray[0]. But if I was to put arrayAddress[1] I get garbage. Question: I have a class that contains the above code, or an array that points to a bunch of variables. Is there a way to pass the name of the array outside the class private: to main() and then access it as if i was inside the class. I am asking because I have a class private: filled with 24 variables, and got 24 members in public: that can return the current value of those variables. To shrink the class I decided to make an array of pointers to those variables and pass it outside the class so that instead of using the member functions I would lookup/change values directly thru the array of pointers. If you have another solution to this problem PLEASE HELP! Thanks for your time! :)

    M 1 Reply Last reply
    0
    • C CreepingFeature

      Hi everyone! First, I appologise for my last post :-O! Anyway, got this here code in main(): int number1 = 5; int number2 = 10; int *p_number1 = &number1; int *p_number2 = &number2; int *arrayAddress; int *pointerArray[2]; pointerArray[0] = p_number1; pointerArray[1] = p_number2; cout << " Pointer array at [0]: " << pointerArray[0]; cout << endl; cout << " Pointer array at [1]: " << pointerArray[1]; cout << endl; cout << endl; cout << " Pointer array deref at [0]: " << *pointerArray[0]; cout << endl; cout << " Pointer array deref at [1]: " << *pointerArray[1]; cout << endl; cout << endl; arrayAddress = pointerArray[0]; cout << endl; cout << " Array address: " << arrayAddress; cout << endl; cout << " Array access through address: " << arrayAddress[0]; It compiles and the last statment prints 5 the actual value at the address that is stored in pointerArray[0]. But if I was to put arrayAddress[1] I get garbage. Question: I have a class that contains the above code, or an array that points to a bunch of variables. Is there a way to pass the name of the array outside the class private: to main() and then access it as if i was inside the class. I am asking because I have a class private: filled with 24 variables, and got 24 members in public: that can return the current value of those variables. To shrink the class I decided to make an array of pointers to those variables and pass it outside the class so that instead of using the member functions I would lookup/change values directly thru the array of pointers. If you have another solution to this problem PLEASE HELP! Thanks for your time! :)

      M Offline
      M Offline
      Mike Beckerleg
      wrote on last edited by
      #2

      This is wrong in so many ways. To answer the last question first - try writing an [] operator for the class to give access to the variables in the class. Then you can do things like boundary checks for the index etc, and you won't be directly exposing private data. As for the actual code you wrote. arrayAddress = pointerArray[0]; makes arrayAddress point to number1. Writing arrayAddress[0] dereferences the pointer to get the value of number1. Writing arrayAddress[1] dereferences a pointer one integer past the memory location of number1, number2 might be stored there but probably not. You need to check your usage of pointers.

      A 1 Reply Last reply
      0
      • M Mike Beckerleg

        This is wrong in so many ways. To answer the last question first - try writing an [] operator for the class to give access to the variables in the class. Then you can do things like boundary checks for the index etc, and you won't be directly exposing private data. As for the actual code you wrote. arrayAddress = pointerArray[0]; makes arrayAddress point to number1. Writing arrayAddress[0] dereferences the pointer to get the value of number1. Writing arrayAddress[1] dereferences a pointer one integer past the memory location of number1, number2 might be stored there but probably not. You need to check your usage of pointers.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Mike Beckerleg wrote: This is wrong in so many ways. You are right! But I found the code bellow to work just right: void arrayAddressFunct(int *array[], int index) { cout << endl; cout << " Array address: " << array; cout << endl; cout << " Array access through address at index: " << *array[index]; }; If the pointerArray address is passed to first argument and the desired index to the second then the function has access to the array at any index. It can actualy be used to return/change the values pointed to by the array. This is what I wanted to do in the first place. A question though: Is it a good idea to do the following? Say a got a class/etc. containing a bunch of variables and I declare an array pointing to those variables. Then I write a two members which when accessed outside of the class :-D in the form described by the code above can change and return the variables pointed to thru the array. Or is there a better way to keep the variables safe but still have access to them without having to write a pile of members for access/change/return of the values.

        M 1 Reply Last reply
        0
        • A Anonymous

          Mike Beckerleg wrote: This is wrong in so many ways. You are right! But I found the code bellow to work just right: void arrayAddressFunct(int *array[], int index) { cout << endl; cout << " Array address: " << array; cout << endl; cout << " Array access through address at index: " << *array[index]; }; If the pointerArray address is passed to first argument and the desired index to the second then the function has access to the array at any index. It can actualy be used to return/change the values pointed to by the array. This is what I wanted to do in the first place. A question though: Is it a good idea to do the following? Say a got a class/etc. containing a bunch of variables and I declare an array pointing to those variables. Then I write a two members which when accessed outside of the class :-D in the form described by the code above can change and return the variables pointed to thru the array. Or is there a better way to keep the variables safe but still have access to them without having to write a pile of members for access/change/return of the values.

          M Offline
          M Offline
          Mike Beckerleg
          wrote on last edited by
          #4

          I'm a bit confused about what you are trying to achieve but here goes. If your class contains an array of values that you want to be able to access then write an operator[] method such as: class SomeClass { public: // construction and stuff here const double& operator[](const int index) const { return mArray[index]; } double& operator[](const int index) { return mArray[index]; } private: double mArray[30]; }; Obviously you would put in some sort of bounds checking on the index value. However from what you are saying the values you are trying to access are not currently in an array, why not? What are these data items? It sounds like they are unrelated items and you just want the array to save having to write accessor methods. If that is the case you may as well just make them public X| which is what your method would do!

          C 1 Reply Last reply
          0
          • M Mike Beckerleg

            I'm a bit confused about what you are trying to achieve but here goes. If your class contains an array of values that you want to be able to access then write an operator[] method such as: class SomeClass { public: // construction and stuff here const double& operator[](const int index) const { return mArray[index]; } double& operator[](const int index) { return mArray[index]; } private: double mArray[30]; }; Obviously you would put in some sort of bounds checking on the index value. However from what you are saying the values you are trying to access are not currently in an array, why not? What are these data items? It sounds like they are unrelated items and you just want the array to save having to write accessor methods. If that is the case you may as well just make them public X| which is what your method would do!

            C Offline
            C Offline
            CreepingFeature
            wrote on last edited by
            #5

            Thanks for your time! I know the questions are a bit $#@^ed up, but I'm just a newb! I sorted the whole mess out, I got the class to do what I want and it is now like 20 times smaller :laugh:. Once again thanks for your help! :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups