Home > Mobile >  Testing validity of certain phrases in c
Testing validity of certain phrases in c

Time:09-15

Given as data these 3:

float pin[5];
float mat[2][4];
float* ptrf;

I have to check whether the following statements are correct or not:

ptrf = *mat;          // I think its is correct
*(mat 2) = pin[2];    // I think its false
*(mat[0]) = pin[0];   // I am not sure
ptrf = mat[1];        // I think its correct

I am trying really hard to understand how pointers work with matrixes but this is really confusing. I'd really appreciated if someone cared to explain!

CodePudding user response:

Firstly, float ptrf* is syntactically invalid. I assume it should be float *ptrf; (ptrf is a pointer to float).

Given the declarations:

float pin[5];
float mat[2][4];
float *ptrf;

In the statement ptrf = pin;, the value (actually an lvalue) designated by pin (of type float[5]) is converted to a float* pointing to the initial element of the array. All is correct. ptrf is pointing to pin[0].

In the statement ptrf = *mat; the lvalue designated by mat (of type float[2][4]) is converted to a float(*)[4] pointing to the initial element of the outer array. (The element is itself an array of type float[4]. A type pointing to float[4] is written syntactically as float(*)[4].) The unary * operator dereferences that pointer producing an lvalue of type float[4] that is the initial row of the outer array. That lvalue of type float[4] is converted to a float* pointing to the initial element of the array. All is correct. ptrf is pointing to mat[0][0].

In the statement *(mat 2) = pin[2];, the lvalue designated by mat (of type float[2][4]) is converted to a float(*)[4] pointing to the initial element of the outer array. mat 2 points to element index 2 of the outer array. This is just past the end of the array. It is OK to point just past the final element of an array, but it is not OK to access an element there. *(mat 2) is dereferencing that non-existent element so is not OK. Also, *(mat 2) has type float[4] and arrays are not allowed on the left-hand side of an assignment because they are no longer an lvalue after conversion to a pointer to their initial element. Also, the right-hand side of the assignment has type float, which is incompatible with the left-hand side.

In the statement *(mat[0]) = pin[0];, mat[0] is the first element of the outer array and has type float[4]. That is converted to a float* pointing to its initial element. The unary * operator dereferences that pointer to an lvalue of type float. The right-hand side of the assignment pin[0] also has type float. All is correct. Note that *(mat[0]) is equivalent to mat[0][0], and it should be fairly obvious that mat[0][0] = pin[0]; is correct.

For the statement ptrf = mat[1];, remember that ptrf = *mat; was correct and note that ptrf = *mat; is equivalent to ptrf = mat[0];. The only difference for ptrf = mat[1]; is that ptrf will point to the initial element of row 1 of the matrix instead of pointing to the initial element of row 0. The matrix has 2 rows so it is OK to point to the initial element of row 1 (and it is also OK to point to the initial element of the non-existent row 2 as long as the contents of that row are not being accessed). All is correct. ptrf is pointing to mat[1][0].

CodePudding user response:

Ok so after all the answer is Correct,False,Correct,Correct And the way to check if the statements are valid is to compile them in a .c file

  • Related