Assigning Array Values w/ Pointer

array pointer c++
array of pointers
array using pointer program in c
pointers in c
2d array pointer c
java array pointer
pointer to multidimensional array
pointer arithmetic in c

I am practicing basic OOP w/ C++ and while using a pointer I found that I did not need to dereference the pointer to assign a value. Why is this? Menu.h:

class Menu { public:
int MenuCount;
int NumItems;
Drinks *Items;

Menu.cpp

Menu::Menu(Drinks a, Drinks b, Drinks c)  {
std::cout << "How many items would you like on this menu? "; std::cin >> NumItems;
Items = new Drinks[NumItems];
Items[0] = a; Items[1] = b; Items[2] = c; // why is it I can assign values by pointer without dereferencing? 
MenuCount = 3;

To my understanding I would've had to dereference Items with * prior to assigning a new object to the array, nonetheless this code works.

The [] operator implicitly dereferences the pointer. It hides the pointer arithmetic with arrays, and because arrays decay to pointers to the first element, it works just as well on a pointer as an array.

Pointers and Arrays in C, i.e arr[0] whose value in above case is 2. It is most likely that you would not understand this section until you are through with the chapter 'Pointers'. Assuming you have some understanding of pointers in C, let us start: An array name is a constant pointer to the first element of the array. Therefore, in the declaration − It is legal to

In your case, Items[0] is actually the same as *(Items + 0) so it is already doing the dereference for you.

Pointer to an Array, The [] operator implicitly dereferences the pointer. It hides the pointer arithmetic with arrays, and because arrays decay to pointers to the first  It is most likely that you would not understand this chapter until you go through the chapter related C++ Pointers. So assuming you have bit understanding on pointers in C++, let us start: An array name is a constant pointer to the first element of the array. Therefore, in the declaration − double balance[50];

Menu::Menu(Drinks a, Drinks b, Drinks c)  {
    std::cout << "How many items would you like on this menu? "; std::cin >> NumItems;
    1#  Items = new Drinks[NumItems];
    2#  Items[0] = a; Items[1] = b; Items[2] = c; // why is it I can assign values by pointer without dereferencing? 
    MenuCount = 3;
}
  1. We are assigning a pointer to a pointer, new Drinks[] will create an array on the heap and return a pointer to it.

  2. Items[0] is the same as (*Items + 0), it'll increment the pointer dereference it at that point.

How to access each element of an array by using only pointers in C , Pointer to an Array in C - It is most likely that you would not understand this section Thus, the following program fragment assigns p as the address of the first output each array element's value */ printf( "Array values using pointer\n"); for ( i  We declare an int array with 5 ints and assign the array numbers variable to our int pointer, ptr1. The numbers variable holds the address of the first element in the array. Assigning it to ptr1 numbers is treated as an pointer. We then get the value of the first element in the array using array notation.

Assigning Array Values w/ Pointer, Arrays and pointers are intimately linked in C. To use arrays effectively, you have Arrays in C are unusual in that variables a and b are not, technically, arrays permanent pointers to arrays. a and b permanently point to the first elements of  Assigning values to an array When we assign primitive values of one type to a variable of other (datatype) implicitly they are converted. But, when you try to assign a higher datatype to lower, at the time of compilation you will get an error saying “incompatible types: possible lossy conversion”

Pointer to an Array in C, Once we have a pointer pointing into an array, we can start doing pointer arithmetic But it's not necessary to assign a new pointer value to a pointer variable in  Assigning values to a pointer. Let us declare an integer type variable n, and a pointer to it *p. p=&n;, if I am not wrong, assigns the address of variable n (say, 3000) to p. So cout<<p<<" "<<*p; would output 3000 and 23 respectively.

Using Pointers with Arrays, There are a number of similarities between arrays and pointers in C. If you have an array As we've seen, though, you can assign two pointer variables: whenever you mention the name of an array in a context where the ``value'' of the array  In this article, we will see how to access two dimensional array using pointers in C programming. In C-language, an array can be split in the form of the pointers and compiler calculates offset to access the element of the array. The calculation of the offset depends on the array dimensions.

Comments
  • Thank you for the clarification
  • Thank you this was quite helpful as well