473,480 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ array initilization

109 New Member
i want to initilize all values in my array to 0. what is right?

Expand|Select|Wrap|Line Numbers
  1. char a[3] = {'0'};  --->  a[0]=0, a[1]=0, a[2]=0, a[3]=0
  2.  
or

Expand|Select|Wrap|Line Numbers
  1. char a[3];
  2.  
  3. for(int i=0; i<3;i++)
  4. {
  5.  a[i]='0';
  6. }
  7.  
thanks
Sep 4 '08 #1
3 1731
Ganon11
3,652 Recognized Expert Specialist
Have you tried both in a C++ program? Did they both work?
Sep 4 '08 #2
boxfish
469 Recognized Expert Contributor
Second way.
Expand|Select|Wrap|Line Numbers
  1. char a[3] = {'0'};  --->  a[0]=0, a[1]=0, a[2]=0, a[3]=0
  2.  
That won't happen.

This will:
Expand|Select|Wrap|Line Numbers
  1. char a[3] = {'0'};  --->  a[0]='0', a[1]=?, a[2]=?, a[3]=Segmentation fault
  2.  
You have to initialize all the values in the array, not just one. And by the way, a[3] does not exist.
Sep 4 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
You have to initialize all the values in the array, not just one. And by the way, a[3] does not exist.
Strictly speaking not exactly true.

For global data in a conforming compiler you do not need to initiaise the data at all. A conforming platform will automatically zero all memory locations that have not been explicitly initialised to something else.

If you want to you can initialise part of an array or structure in this case the rest of the array or structure is initialised to 0.

You use of a for loop was not initialisation, it was assignment after the variable had already been created and initialised (to 0).

Variables that appear on the stack do not get initialised in the same way (as they don't exist when the program starts), initialisation of stack variable often acts much more like assignment e.g. initialise a stack based array to zero and you end up with an assignment to each location.


While you can partially initialise an array or structure (i.e. don't provide values for all indexes/members) I would normally consider it poor practice to do so and some compilers will issue a warning.
Sep 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2753
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5153
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3460
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55521
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
7
6616
by: baumann | last post by:
hi all, #include "stdafx.h" struct a_{ int a; int b; };
6
309
by: puzzlecracker | last post by:
Why would this work? - and it does! any thoughts? #include<stdio.h> void init(char **str){ *str="Awesome"; } main(){
5
1704
by: vsnadagouda | last post by:
Hello All, I am looking for some way to delay the global variable initilization. To make myself more clear, here is a simple example: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++...
8
5636
by: The Cool Giraffe | last post by:
I have a dynamically declared array of integers and i'd like to check the number of elements. For some reason, the expected sizeof(mtx)/sizeof(mtx) doesn't work. I've been googling and i got a...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.