• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

164
Vistas
array value at an index for which value hasn't been defined

in this code:

#include <stdio.h>

int main(void) 
{
    int a[2][3] = {{1,2},{4,5,6}};

    printf("%d\n",a[0][2]);
    return 0;
}

The output is 0 - since the array wasn't initialized, is this output a result of some undefined behaviour?

about 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The array was initialised. You’re defining a two-dimensional array that consists of two arrays of three integers each. For the first of these two you only give two values, and by default the missing one is initialised as zero, so your full array is {{1, 2, 0}, {4, 5, 6}. a[0][2] will give you that zero.

about 3 years ago · Santiago Trujillo Denunciar

0

From the C Standard (6.7.9 Initialization)

19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

and

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

So in fact this declaration

int a[2][3] = {{1,2},{4,5,6}};

is equivalent to

int a[2][3] = {{1,2, 0},{4,5,6}};
about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda