# Array

Array คืออะไร\
&#x20;        อาร์เรย์เป็นประเภทหนึ่งในการจัดเก็บตัวแปร โดยอาร์เรย์จะสามารถเก็บค่าได้หลายๆค่าต่างกับตัวแปรธรรมดาที่สามารถเก็บค่าได้ทีละ1ค่า

![](/files/-MRyO21LG00AjE3DcLe6)

โดยการประกาศอาร์เรย์นั้นสามารถทำได้โดยการใช้คำสั่งดังนี้

```cpp
// ประเภท ชื่อ[ขนาด]; เช่น
int Hallway[9];
string cars[5];
double grade[4] = {4.00,3.79,2.75,3.11};
char Alpgrade[5] = {'A','B','C','D','F'};
string name[] = {"Jame","Jack","John"};
```

จากตัวอย่างด้านบนจะเห็นได้ว่า เราสามารถประกาศอาร์เรย์สำหรับเก็บค่าต่างเหมือนกับการประกาศตัวแปรปกติ แต่มีสิ่งที่เพิ่มเติมมาคือ \[ขนาด] ที่ต้องใส่ต่อท้าย ซึ่งขนาดของอาร์เรย์นั้นไม่จำเป็นต้องระบุก็ได้ เช่นเดียวกับในตัวอย่างบรรทัดที่6 ซึ่งในบรรทัดที่ 6 นั้นจะมีขนาดของอาเรย์คือ 3 ช่องตามข้อมูลที่กำหนดไว้ให้นั่นเอง

ตัวอย่างการใช้งานอาร์เรย์

```cpp
#include <iostream> 
using namespace std; 

// index example code 
int main() 
{ int allnumber[5] = {4,11,76,4,88};
  cout << allnumber[0] << endl;
  cout << allnumber[1] << endl;
  cout << allnumber[2] << endl;
  cout << allnumber[3] << endl;
  cout << allnumber[4] << endl;
  cout << allnumber << endl;
    return 0; 
} 
```

{% hint style="info" %}
จากตัวอย่างด้านบนจะเห็นว่า Array จะเริ่มต้นช่องแรกด้วยการนับที่ index ที่ \[0]
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
