logo

C++ 포인터 배열

배열과 포인터는 서로 밀접하게 연관되어 있습니다. C++에서 배열의 이름은 포인터로 간주됩니다. 즉, 배열의 이름에는 요소의 주소가 포함됩니다. C++에서는 배열 이름을 첫 번째 요소의 주소로 간주합니다. 예를 들어, 정수 유형의 20개 값을 보유하는 마크와 같은 배열을 생성하면 마크에는 첫 번째 요소의 주소, 즉 마크[0]가 포함됩니다. 따라서 배열 이름(마크)은 배열의 첫 번째 요소의 주소를 보유하는 포인터라고 말할 수 있습니다.

예를 통해 이 시나리오를 이해해 보겠습니다.

 #include using namespace std; int main() { int *ptr; // integer pointer declaration int marks[10]; // marks array declaration std::cout &lt;&lt; &apos;Enter the elements of an array :&apos; &lt;&lt; std::endl; for(int i=0;i&gt;marks[i]; } ptr=marks; // both marks and ptr pointing to the same element.. std::cout &lt;&lt; &apos;The value of *ptr is :&apos; &lt;<*ptr<< std::endl; std::cout << 'the value of *marks is :' <<*marks<<std::endl; } < pre> <p>In the above code, we declare an integer pointer and an array of integer type. We assign the address of marks to the ptr by using the statement ptr=marks; it means that both the variables &apos;marks&apos; and &apos;ptr&apos; point to the same element, i.e., marks[0]. When we try to print the values of *ptr and *marks, then it comes out to be same. Hence, it is proved that the array name stores the address of the first element of an array.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers.webp" alt="C++ Array of Pointers"> <h3>Array of Pointers</h3> <p>An array of pointers is an array that consists of variables of pointer type, which means that the variable is a pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers; then its declaration would look like:</p> <pre> int *ptr[5]; // array of 5 integer pointer. </pre> <p>In the above declaration, we declare an array of pointer named as ptr, and it allocates 5 integer pointers in memory.</p> <p>The element of an array of a pointer can also be initialized by assigning the address of some other element. Let&apos;s observe this case through an example.</p> <pre> int a; // variable declaration. ptr[2] = &amp;a; </pre> <p>In the above code, we are assigning the address of &apos;a&apos; variable to the third element of an array &apos;ptr&apos;.</p> <p>We can also retrieve the value of &apos;a&apos; be dereferencing the pointer.</p> <pre> *ptr[2]; </pre> <p> <strong>Let&apos;s understand through an example.</strong> </p> <pre> #include using namespace std; int main() { int ptr1[5]; // integer array declaration int *ptr2[5]; // integer array of pointer declaration std::cout &lt;&lt; &apos;Enter five numbers :&apos; &lt;&lt; std::endl; for(int i=0;i&gt; ptr1[i]; } for(int i=0;i<5;i++) { ptr2[i]="&amp;ptr1[i];" } printing the values of ptr1 array std::cout << 'the are' std::endl; for(int i="0;i&lt;5;i++)" *ptr2[i] < pre> <p>In the above code, we declare an array of integer type and an array of integer pointers. We have defined the &apos;for&apos; loop, which iterates through the elements of an array &apos;ptr1&apos;, and on each iteration, the address of element of ptr1 at index &apos;i&apos; gets stored in the ptr2 at index &apos;i&apos;.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-2.webp" alt="C++ Array of Pointers"> <p>Till now, we have learnt the array of pointers to an integer. Now, we will see how to create the array of pointers to strings.</p> <h3>Array of Pointer to Strings</h3> <p>An array of pointer to strings is an array of character pointers that holds the address of the first character of a string or we can say the base address of a string.</p> <p>The following are the differences between an array of pointers to string and two-dimensional array of characters:</p> <ul> <li>An array of pointers to string is more efficient than the two-dimensional array of characters in case of memory consumption because an array of pointer to strings consumes less memory than the two-dimensional array of characters to store the strings.</li> <li>In an array of pointers, the manipulation of strings is comparatively easier than in the case of 2d array. We can also easily change the position of the strings by using the pointers.</li> </ul> <p>Let&apos;s see how to declare the array of pointers to string.</p> <p>First, we declare the array of pointer to string:</p> <pre> char *names[5] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; </pre> <p>In the above code, we declared an array of pointer names as &apos;names&apos; of size 5. In the above case, we have done the initialization at the time of declaration, so we do not need to mention the size of the array of a pointer. The above code can be re-written as:</p> <pre> char *names[ ] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; </pre> <p>In the above case, each element of the &apos;names&apos; array is a string literal, and each string literal would hold the base address of the first character of a string. For example, names[0] contains the base address of &apos;john&apos;, names[1] contains the base address of &apos;Peter&apos;, and so on. It is not guaranteed that all the string literals will be stored in the contiguous memory location, but the characters of a string literal are stored in a contiguous memory location.</p> <p> <strong>Let&apos;s create a simple example.</strong> </p> <pre> #include using namespace std; int main() { char *names[5] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; for(int i=0;i<5;i++) { std::cout << names[i] std::endl; } return 0; < pre> <p>In the above code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-3.webp" alt="C++ Array of Pointers"> <hr></5;i++)></pre></5;i++)></pre></*ptr<<>

위 선언에서는 ptr이라는 포인터 배열을 선언하고 메모리에 5개의 정수 포인터를 할당합니다.

해시셋 대 해시맵

포인터 배열의 요소는 다른 요소의 주소를 할당하여 초기화할 수도 있습니다. 이 사례를 예를 통해 살펴보겠습니다.

 int a; // variable declaration. ptr[2] = &amp;a; 

위 코드에서는 'a' 변수의 주소를 배열 'ptr'의 세 번째 요소에 할당합니다.

포인터를 역참조하여 'a' 값을 검색할 수도 있습니다.

리디마 티와리
 *ptr[2]; 

예를 통해 이해해 봅시다.

배열 목록 정렬
 #include using namespace std; int main() { int ptr1[5]; // integer array declaration int *ptr2[5]; // integer array of pointer declaration std::cout &lt;&lt; &apos;Enter five numbers :&apos; &lt;&lt; std::endl; for(int i=0;i&gt; ptr1[i]; } for(int i=0;i<5;i++) { ptr2[i]="&amp;ptr1[i];" } printing the values of ptr1 array std::cout << \'the are\' std::endl; for(int i="0;i&lt;5;i++)" *ptr2[i] < pre> <p>In the above code, we declare an array of integer type and an array of integer pointers. We have defined the &apos;for&apos; loop, which iterates through the elements of an array &apos;ptr1&apos;, and on each iteration, the address of element of ptr1 at index &apos;i&apos; gets stored in the ptr2 at index &apos;i&apos;.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-2.webp" alt="C++ Array of Pointers"> <p>Till now, we have learnt the array of pointers to an integer. Now, we will see how to create the array of pointers to strings.</p> <h3>Array of Pointer to Strings</h3> <p>An array of pointer to strings is an array of character pointers that holds the address of the first character of a string or we can say the base address of a string.</p> <p>The following are the differences between an array of pointers to string and two-dimensional array of characters:</p> <ul> <li>An array of pointers to string is more efficient than the two-dimensional array of characters in case of memory consumption because an array of pointer to strings consumes less memory than the two-dimensional array of characters to store the strings.</li> <li>In an array of pointers, the manipulation of strings is comparatively easier than in the case of 2d array. We can also easily change the position of the strings by using the pointers.</li> </ul> <p>Let&apos;s see how to declare the array of pointers to string.</p> <p>First, we declare the array of pointer to string:</p> <pre> char *names[5] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; </pre> <p>In the above code, we declared an array of pointer names as &apos;names&apos; of size 5. In the above case, we have done the initialization at the time of declaration, so we do not need to mention the size of the array of a pointer. The above code can be re-written as:</p> <pre> char *names[ ] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; </pre> <p>In the above case, each element of the &apos;names&apos; array is a string literal, and each string literal would hold the base address of the first character of a string. For example, names[0] contains the base address of &apos;john&apos;, names[1] contains the base address of &apos;Peter&apos;, and so on. It is not guaranteed that all the string literals will be stored in the contiguous memory location, but the characters of a string literal are stored in a contiguous memory location.</p> <p> <strong>Let&apos;s create a simple example.</strong> </p> <pre> #include using namespace std; int main() { char *names[5] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; for(int i=0;i<5;i++) { std::cout << names[i] std::endl; } return 0; < pre> <p>In the above code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-3.webp" alt="C++ Array of Pointers"> <hr></5;i++)></pre></5;i++)>

위 코드에서는 포인터 이름의 배열을 크기 5의 '이름'으로 선언했습니다. 위의 경우 선언 시 초기화를 수행했기 때문에 배열의 크기는 언급할 필요가 없습니다. 바늘. 위의 코드는 다음과 같이 다시 작성할 수 있습니다.

 char *names[ ] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; 

위의 경우 'names' 배열의 각 요소는 문자열 리터럴이고 각 문자열 리터럴은 문자열의 첫 번째 문자의 기본 주소를 보유합니다. 예를 들어 names[0]에는 'john'이라는 기본 주소가 포함되고, names[1]에는 'Peter'라는 기본 주소가 포함되는 식입니다. 모든 문자열 리터럴이 연속 메모리 위치에 저장된다는 보장은 없지만 문자열 리터럴의 문자는 연속 메모리 위치에 저장됩니다.

간단한 예제를 만들어 보겠습니다.

 #include using namespace std; int main() { char *names[5] = {&apos;john&apos;, &apos;Peter&apos;, &apos;Marco&apos;, &apos;Devin&apos;, &apos;Ronan&apos;}; for(int i=0;i<5;i++) { std::cout << names[i] std::endl; } return 0; < pre> <p>In the above code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.</p> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/74/c-array-pointers-3.webp" alt="C++ Array of Pointers"> <hr></5;i++)>