Dynamically Allocated Arrays The declaration item array new item[array size creates a dynamic array of Item objects, indexed from o up to array size -1. Consider, for example int size, dynamic array, i; cout < Enter an array size: < flush cIn > sIze: dynamic array new int[size] for(i=0; i<size; i++)dynamic array[i]= The result is illustrated as:
The declaration item array = new Item[array size]; creates a dynamic array of Item objects, indexed from 0 up to array size - 1.Consider, for example: Dynamically Allocated Arrays int size, *dynamic_array, i; cout << "Enter an array size: " << flush; cin >> size; dynamic_array = new int[size]; for (i=0; i<size; i++) dynamic_array[i]=i; The result is illustrated as:
dynamic amay= new int (size: dynamic array for (i=0; i<size: i++) dynamic array[]=i dynamic array 012345678910 The statement delete[] dynamic array returns the storage in dynamic array to the free store. If i is an integer value and p is a pointer to an Item, then p+i is an expression of type Item
The statement delete[ ] dynamic array ; returns the storage in dynamic array to the free store. If i is an integer value and p is a pointer to an Item, then p+i is an expression of type Item *
The value of p+ i gives the memory address offset from p by i Item objects. That is, the expression p+ actually yields the address p+nx i, where n is the number of bytes of storage occupied by a simple object of type Item. Music 和p q(- Calculus Music Calculi *p p=q 和p=Aq Calculus 和q=p Calculus
The value of p + i gives the memory address offset from p by i Item objects. That is, the expression p+i actually yields the address p+n i, where n is the number of bytes of storage occupied by a simple object of type Item
o Addresses of automatic objects. If x is a variable of type Item, then &x is a value of type Item that gives the address of x In this case, a declaration and assignment such as Item *ptr=&x would establish a pointer, ptr, to the object x. ◆ Address of an array: The address of the initial element of an array is found by using the array' s name without any attached [] operators. For example, given a declaration Item x [20] the assignment Item ptr=X
Addresses of automatic objects: If x is a variable of type Item, then &x is a value of type Item* that gives the address of x. In this case, a declaration and assignment such as Item *ptr = &x would establish a pointer, ptr, to the object x. Address of an array: The address of the initial element of an array is found by using the array's name without any attached [ ] operators. For example, given a declaration Item x[20] the assignment Item *ptr = x
sets up a pointer ptr to the initial element of the array x Observe that an assignment expression ptr & x[o] could also be used to nd this address. Q Pointers to structures: If p is a pointer to a structure object that has a data member callled the data then we could access this data member with the expression Cp). the data, but C++ provides the operator->as a shorthand, so we can replace the expression Cp). the data by the equivalent, but more convenient, expression p->the data
sets up a pointer ptr to the initial element of the array x. Observe that an assignment expression ptr = &(x[0]) could also be used to nd this address. Pointers to structures: If p is a pointer to a structure object that has a data member called the data, then we could access this data member with the expression (*p).the data, but C++ provides the operator -> as a shorthand, so we can replace the expression (*p).the data by the equivalent, but more convenient, expression p->the data