Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The expression </p> <pre><code>abc+1 </code></pre> <p>is a pointer. Your pointers would therefore appear to be 4 bytes wide.</p> <p>On the other hand, the expression</p> <pre><code>abc </code></pre> <p>is an array of char of length 100 and so its size is 100.</p> <p>And as for the version using int instead of char, I'm sure you can now apply the above reasoning to explain the output.</p> <hr> <p>I think your edit to the question essentially asks:</p> <blockquote> <p>Why is <code>abc+1</code> a pointer, and <code>abc</code> an array.</p> </blockquote> <p>The standard says:</p> <blockquote> <p><strong>Other operands - Lvalues, arrays, and function designators (ISO/IEC 9899:201x 6.3.2.1/3)</strong></p> <p>Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary &amp; operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.</p> </blockquote> <p>So, when you write <code>sizeof(abc)</code> then <code>abc</code> does not meet the criteria required to convert the expression <code>abc</code> into a pointer. So <code>abc</code> is treated as an array. But in the expression <code>sizeof(abc+1)</code>, <code>abc+1</code> does not meet any of the listed exceptions and so is converted into a pointer.</p> <p>Colloquially this is known as an array decaying to a pointer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload