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: 3556713 • 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.

if you could give informative answers that would help a lot. thank you

Explanation / Answer

1)

The array names is not modifiable lvalue so operation ++ is not applied hence ++a that try to modify ais compilation time error (where a is array name).

Note *(a + 1) and *a++ are not same, a + 1 is a valid instruction as it just add 1 but doesn't modify a itself, Whereas ++a (that is equvilent to a = a + 1) try to modify a hence error.

Note 'array names' are not pointer. Pointers are variable but array names are not. Of-course when you assign array name to a pointer then in most expressions array names decays into address of first element. e.g.

Note p points to first element of array (a[0]).

Read some exceptions where array name not decaying into a pointer to first element?

An expression a[i] is equivalent to *(a + i), where a can be either a pointer or an array name. Hence in your second example p[i] is valid expression.

Additionally, *++p is valid because because p is a pointer (a variable).

probably arr++ assigns arr a new value and (arr+1) is used only for

dereferencing and the expression itself doesn't change arr's value.

2)

a)

vector<studentInfo> p;

b)

p.resize(7); // allocate space for 7 structs in vector

c)

studentInfo[size-1].lastName ="Pitt";

studentinfo[size-1].ID=&k;

studentinfo[size-1].gpa= 94.5634