-2

When I was study lvalue i see that

C expression can be lvalue if a subscript ([]) expression that does not evaluate to an array.

(from https://docs.microsoft.com/en-us/cpp/c-language/l-value-and-r-value-expressions?redirectedfrom=MSDN&view=vs-2019)

But jagged array does evaluate to an array and we can use them as lvalue (i suppose) Could anyone explain this?

  • What is a jagged array? Is that int **x;? Then x[0] is not an array, it is a pointer. – user253751 Jul 3 at 15:32
1

The following does not apply to jagged arrays:

Any of the following C expressions can be l-value expressions:

  • A subscript ([ ]) expression that does not evaluate to an array

A jagged array is an approach to a two-dimensional data structure that allows for varying the 2nd dimension between positions of the 1st dimension.  We think of it as an array of arrays, but it is really an array of pointers.  Those elements (of the first dimension), the pointers, are lvalues; they can be assigned to (assuming they're not const).

An array of (pointers to) strings is such a jagged array (of characters).  Any individual (non-const) element of the array can be changed to (point to) a different string.


An array of arrays is another approach to two (or more) dimensional data structure, but it does not involve storing pointers.  All dimensions are real arrays, and thus cannot be assigned to.

A two dimensional array of characters — an array of array of characters — has only room for characters.  The only lvalues there are the individual characters.

| improve this answer | |
  • so, what is the array of array and arrays can be used as non-lvalue? – Ulaş Sezgin Jun 4 at 19:06
  • @UlaşSezgin (Firstly) In the C language, the term "jagged arrays" implies that someone has to create an array of pointers (containing pointers to a certain type) somewhere, and initialize the pointers in this array to point to valid memory locations. The individual elements in this array of pointers can be reassigned; therefore, these individual elements are l-values. – rwong Jun 4 at 21:10
  • 1
    @UlaşSezgin (Secondly) Even though teaching books for the C language often say "an array is really just a pointer", the truth is more complicated. An array in C is handled differently in terms of syntax and rules compared to a pointer in C, even though they both support the subscript expression. For example, if a variable of type int data[100][100] is declared, the C language does not allow one to assign data to a variable of type int**, because the former isn't a jagged array; it lacks a table of pointers that refer to a memory space allocated for chunks of a hundred integers. – rwong Jun 4 at 21:15
  • And sizeof works differently for arrays (gives the size of the whole array) than pointers (gives the size of a pointer) as well. – Erik Eidt Jun 5 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.