Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. If arr represents an array, suppose that arr++ was used in an expression. Und

ID: 3570635 • Letter: 1

Question

1. If arr represents an array, suppose that arr++ was used in an expression. Under what circumstances would its use be illegal? Under what circumstances would its use be legal? When it is legal, what does it mean? (You can assume that the expression is itself legal and correctly placed in the program.)

2. Here is a structure definition:
struct studentInfo
{
string lastName, firstName;
int *ID; // pointer
double gpa;
};

a) Declare a vector of these structures where the size of the vector is to be 7.
b) Put data into the last struct of the vector for each of the members of the struct.

Explanation / Answer

1)
If arr represents an array, suppose that arr++ was used in an expression

then it will be illegal if the array is not a pointer array
int arr[4] = {1,2,3,4};
under this strategy we cannot use arr++; as it leads to compilation error

it will be legal if the array is a pointer array
int *arr = {1,2,3,4};
then arr++; will give the first element of the array i.e., 1 and for the next time when we use arr++; then it will give sum of first element of array and the size of pointer array i.e.,1+4 = 5, as the pointer array will hold 4 bytes

if the pointer array is not initialized
int *arr;
then arr++; will give you the address of the array and for the next time when we use arr++; it will increment by 4 bytes as pointer array will take 4 bytes of size.


2)
struct studentInfo
{
string lastName, firstName;
int *ID; // pointer
double gpa;
};

a)
vector<studentInfo> students(7);

b)
students[6].lastName = "asdf";
students[6].firstName = "fsdsdf";
students[6].ID = &num;
students[6].gpa = 2.5;