# Get to know with arrays

การใช้งานอาร์เรย์นั้นค่อนข้างยุ่งยากและซับซ้อนเพราะว่า บนตัวอาร์เรย์นั้นเก็บค่าไว้มากมาย ทำให้การที่เราจะระบุหรือเรียกใช้งานอาร์เรย์ทั้งหมดนั้นจำเป็นจะต้องใช้การวนรอบเข้ามาช่วย ตัวอย่างเช่น

### การแสดงค่าที่อาเรย์เก็บไว้ทั้งหมดตามลำดับ

```cpp
int arr[] = {11,4,2,77,112,69,420,13};
int n = *(&arr + 1) - arr; //find length of array
for (int i=0;i<n;i++){
    cout<<arr[i]<<" "; // print untill the end of array
  }
```

{% hint style="warning" %}
จะเห็นได้ว่าเราจะต้องหาความยาวของอาร์เรย์เพื่อนำมาใช้ในการวนรอบโดยหลักการคือ\
"นำแอดเดรสถัดจากอาร์เรย์ - แอดเดรสของอาร์เรย์ "\
สิ่งที่ได้มาก็คือจำนวนช่องของอาร์เรย์ที่ใช้งานไปนั่นเอง
{% endhint %}

### การรับค่าใส่อาเรย์ทั้งหมดตามลำดับ

```cpp
int arr[8];
int n = *(&arr + 1) - arr; //find length of array
for (int i=0;i<n;i++){
    cout<<"Please enter integer : ";
    cin >> arr[i]; // enter untill the end of array
  }
```

{% hint style="success" %}
เราสามารถกำหนดค่าไปใส่แค่บางส่วนของอาร์เรย์ได้เช่น\
cin >> arr\[X] ;\
หรือ\
arr\[X] = 10;\
เพื่อกำค่าใหม่ในอาร์เรย์ช่องที่ \[X]
{% endhint %}

### การสร้างฟังค์ชั่นเพื่อรับอาร์เรย์

```cpp
void printarr(int arr[],int n) { //(array name, lenght of array)      
  for (int i=0;i<n;i++){
    cout<<arr[i]<<" ";
  }
```

{% hint style="warning" %}
การสร้างฟังก์ชั่นอาร์เรย์นั้นควรจะมีการส่งขนาดของอาร์เรย์มาด้วยเสมอ เนื่องจากภาษา c++\
การกำหนด agreement ว่า "int ArrayName\[ ]" จะมีค่าเท่าการประกาศว่า "int \* ArrayName"\
ทำให้สิ่งที่เข้ามาในฟังก์ชั่นนั้นไม่ใช่ตัวอาร์เรย์ แต่เป็นค่าพ้อยเตอร์ของอาร์เรย์ **ทำให้ขนาดของอาร์เรย์ที่อยู่ในฟังก์ชั่นนั้นมีขนาดใหญ่กว่าความเป็นจริง** (เนื่องจากเป็นขนาดของพ้อยเตอร์)
{% endhint %}

{% hint style="danger" %}
คำเตือน เนื่องจากค่าที่รับมาคือพ้อยเตอร์ของอาร์เรย์ ค่าใดๆก็ตามที่ทำให้อาร์เรย์ในฟังก์ชั่นเปลี่ยนค่า\
ค่าอาร์เรย์ที่ส่งมาให้ฟังก์ชั่นก็จะเปลี่ยนตามด้วยเช่นกัน
{% 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/c-programming-techniques/array-and-properties-of-an-array/get-to-know-with-arrays.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.
