PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > partial initialized array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
partial initialized array

Réponse
 
LinkBack Outils de la discussion
Vieux 29/11/2007, 06h00   #1
Nishu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut partial initialized array

Hi All,

What is the C-standard expected result for referring the
'uninitialized element' of the partial initialized automatic array ?

/*************************/
#include <stdio.h>

int main(void)
{
int arr[2][3][4] = { 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,

12, 13, 14, 15,
16, 17, 18, 19,
20, 21
} ;

printf(" arr[1][2][2] = %d \n", arr[1][2][3]);

return 0;
}

/*************************/

Is the result defined as 0 by standard? If yes, then, why is that so?

Thanks,
Naresh
  Réponse avec citation
Vieux 29/11/2007, 07h15   #2
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: partial initialized array

Nishu wrote:

> Hi All,
>
> What is the C-standard expected result for referring the
> 'uninitialized element' of the partial initialized automatic array ?


It is initialised to zero.

> Is the result defined as 0 by standard? If yes, then, why is that so?


Because the Standard says so.


  Réponse avec citation
Vieux 30/11/2007, 03h09   #3
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: partial initialized array

On Wed, 28 Nov 2007 22:00:31 -0800 (PST), Nishu
<naresh.attri@gmail.com> wrote in comp.lang.c:

> Hi All,
>
> What is the C-standard expected result for referring the
> 'uninitialized element' of the partial initialized automatic array ?
>
> /*************************/
> #include <stdio.h>
>
> int main(void)
> {
> int arr[2][3][4] = { 0, 1, 2, 3,
> 4, 5, 6, 7,
> 8, 9, 10, 11,
>
> 12, 13, 14, 15,
> 16, 17, 18, 19,
> 20, 21
> } ;
>
> printf(" arr[1][2][2] = %d \n", arr[1][2][3]);
>
> return 0;
> }
>
> /*************************/
>
> Is the result defined as 0 by standard? If yes, then, why is that so?


Why not?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Vieux 30/11/2007, 03h25   #4
Thad Smith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: partial initialized array

Nishu wrote:
> Hi All,
>
> What is the C-standard expected result for referring the
> 'uninitialized element' of the partial initialized automatic array ?
>
> Is the result defined as 0 by standard? If yes, then, why is that so?


Yes, it is defined as 0, probably because it is easy to implement the
entire object as either fully initialized or not initialized. There
would be a lot of work and little payoff for leaving unspecified
elements within an aggregate unchanged.

--
Thad
  Réponse avec citation
Vieux 01/12/2007, 01h25   #5
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: partial initialized array

On Wed, 28 Nov 2007 22:00:31 -0800 (PST), Nishu
<naresh.attri@gmail.com> wrote:

>Hi All,
>
>What is the C-standard expected result for referring the
>'uninitialized element' of the partial initialized automatic array ?
>
>/*************************/
>#include <stdio.h>
>
>int main(void)
>{
> int arr[2][3][4] = { 0, 1, 2, 3,
> 4, 5, 6, 7,
> 8, 9, 10, 11,
>
> 12, 13, 14, 15,
> 16, 17, 18, 19,
> 20, 21
> } ;
>
> printf(" arr[1][2][2] = %d \n", arr[1][2][3]);


Is one of the two final subscripts a typo or am I missing some hidden
meaning?

>
> return 0;
>}
>
>/*************************/
>
>Is the result defined as 0 by standard? If yes, then, why is that so?


When an aggregate object is initialized with a list of initialization
values that does not fill up the entire object, any remaining portions
of the object are initialized as if by assignment with 0. If any
remaining portion is itself an aggregate, apply this rule recursively.
There is an exception for unions: only the first member of the union
is initialized. So the short answer to your first question is yes, it
is specified as 0 by the standard.

For the second question, you may get an answer in a newsgroup that
discusses the standard. I would guess that this simply put the
official stamp of approval on what was the common practice at the time
the first C standard was being drafted. It also provides a very
convenient shorthand
int x[100] = {1,2,3};
is much easier on the eyes and keyboard than
int x[100] = {1,2,3,0,0,0,0,0,0,...


Remove del for email
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12822 seconds with 13 queries