C + + arrays

Source: Internet
Author: User

An array is a data format that can store multiple values of the same type.

Paste the following code:

#include <iostream>

int main ()
{
using namespace Std;
int yams[3];
Yams[0] = 7;
YAMS[1] = 8;
YAMS[2] = 6;

int yamcosts[3] = {20,30,50};
Yamcosts[3] = {20,30,50}; Not allowed;
Yams = yamcosts; Not allowed;
Float Fa[5] = {0.5f,2.3f}; Initializes only a subset of the elements, the compiler resets the other elements to 0
Float Fb[5] = {0.0}; Set all elements to 0
cout<<yams[0]<< "" <<yams[1]<< "" <<yams[2]<<endl;
cout<<yamcosts[0]<< "" <<yamcosts[1]<< "" <<yamcosts[2]<<endl;

Cout<<sizeof yams<<endl;
Cout<<sizeof yams[0]<<endl;
cout<<fa[3]<<endl;

Short test[] = {2,5,97,4};
int num_elements = sizeof test/sizeof (short);//This is a good solution when you don't care about the number of array elements.
cout<<num_elements<<endl;

return 0;
}

Extended:

1. Arrays are called compound types because they are created using other types. You can't just declare something as an array, it must be a specific type. Class such as: Flaot loans[20], where the type of loans is a float array instead of an array.

2, the importance of effective subscript. The compiler does not check if the subscript is valid. For example, if you assign a value to an element that does not exist months[101], the compiler does not give an error. However, this assignment can corrupt the data and may cause the program to terminate unexpectedly. Therefore, you must ensure that the program uses a valid subscript value.

C + + arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.