logo

Pole strun v C

Pole je nejjednodušší datová struktura v C, která ukládá homogenní data do souvislých paměťových míst. Pokud chceme vytvořit pole, deklarujeme typ Data a dáme do něj prvky:

 #include int main() { int i, arr[5] = {1, 2, 4, 2, 4}; for(i = 0; i <5; i++) { printf('%d ', arr[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 4 2 4 </pre> <p>In C, a Character and a String are separate data types, unlike other programming languages like Python. A String is a collection of Characters. Hence, to define a String, we use a Character Array:</p> <pre> #include int main() { char str[8]; printf(&apos;Enter a String: &apos;); scanf(&apos;%s&apos;, &amp;str); printf(&apos;%s&apos;, str); } </pre> <p> <strong>Output:</strong> </p> <pre> Enter a String: Hello Hello </pre> <p>Now, we want to create an Array of Strings which means we are trying to create an Array of Character Arrays. We have two ways we can do this:</p> <ol class="points"> <li>Using Two-dimensional Arrays</li> <li>Using Pointers</li> </ol> <h3>Using Two-dimensional Arrays:</h3> <p>Creating a String Array is one of the applications of two-dimensional Arrays. To get a picture of the arrangement, observe the below representation:</p> <p>For suppose we want to create an Array of 3 Strings of size 5:</p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c.webp" alt="An Array of Strings in C"> <p>Every String in a String Array must terminate with a null Character. It is the property of a String in C.</p> <p> <strong>Syntax to create a 2D Array:</strong> </p> <pre> Data_type name[rows][columns] = {{values in row 1}, {values in row 2}&#x2026;}; </pre> <p> <strong>Syntax to create a String Array:</strong> </p> <pre> char Array[rows][columns] = {&apos;String1&apos;, &apos;String2&apos;...}; </pre> <p> <strong>Now, let us create an example String Array:</strong> </p> <ul> <li>Observe that when we assign the number of rows and columns, we need to consider the Null Character to the length.</li> </ul> <pre> #include int main() { int i; char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Block&apos;}; printf(&apos;String Array: 
&apos;); for(i = 0; i <3; i++) { printf('%s
', array[i]); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> String Array: Black Blame Block </pre> <ul> <li>char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Black&apos;} -&gt; {{&apos;B&apos;, &apos;l&apos;, &apos;a&apos;, &apos;c&apos;, &apos;k&apos;, &apos;&apos;}, {&apos;B&apos;, &apos;l&apos;, &apos;a&apos;, &apos;m&apos;, &apos;e&apos;, &apos;&apos;}, {&apos;B&apos;, &apos;l&apos;, &apos;a&apos;, &apos;c&apos;, &apos;k&apos;, &apos;&apos;}}</li> <li>We cannot directly manipulate the Strings in the Array as a String is an immutable data type. The compiler raises an error:</li> </ul> <pre> char Array[0] = &apos;Hello&apos;; </pre> <p> <strong>Output:</strong> </p> <pre> [Error] assignment to expression with Array type </pre> <ul> <li>We can use the strcpy() function to copy the value by importing the String header file:</li> </ul> <pre> char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Block&apos;}; strcpy(Array[0], &apos;Hello&apos;); for(i = 0; i <3; i++) { printf('%s
', array[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> String Array: Hello Blame Block </pre> <p> <strong>The Disadvantage of using 2D Arrays:</strong> </p> <p>Suppose we want to store 4 Strings in an Array: {&apos;Java&apos;, &apos;T&apos;, &apos;point&apos;, &apos;JavaTpoint&apos;}. We will store the Strings like this:</p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c-2.webp" alt="An Array of Strings in C"> <ul> <li>The number of rows will be equal to the number of Strings, but the number of columns will equal the length of the longest String.</li> <li>The memory allocated to all the Strings will be the size of the longest String, causing &apos; <strong>Memory wastage</strong> &apos;.</li> <li>The orange part in the above representation is the memory wasted.</li> </ul> <h3>Using Pointers:</h3> <p>By using Pointers, we can avoid the Disadvantage of Memory wastage. But how do we do this?</p> <p>We need to create an Array of Pointers pointing to Strings. Hence, we need to create an Array of type &apos; <strong>char*</strong> &apos;. This way, all the Strings are stored elsewhere in the exactly needed memory, and the Pointers in the Array point to those memory locations causing no memory wastage. More specifically, the Pointers in the Array point to the first Character of the Strings.</p> <p> <strong>Syntax to create an Array of Pointers:</strong> </p> <p>Data Type* name[] = {&apos;Value 1&apos;, &apos;Value 2&apos;&#x2026;};</p> <p> <strong>Syntax to create an Array of String Pointers:</strong> </p> <p>char* Array[] = {&apos;String 1&apos;, &apos;String 2&apos;&#x2026;};</p> <p> <strong>Representation:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c-3.webp" alt="An Array of Strings in C"> <p> <strong>Now, let us create an example String Array:</strong> </p> <pre> #include #include int main() { int i; char* Array[] = {&apos;HI&apos;, &apos;UP&apos;, &apos;AT&apos;}; printf(&apos;String Array:
&apos;); for(i = 0; i <3; i++) { printf('%s
', array[i]); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> String Array: HI UP AT </pre> <h3>Summary:</h3> <p>We cannot create a String Array like a normal one, as a String is an Array of Characters. We have two ways to do this:</p> <p> <strong>1. Using a Two-Dimensional Array:</strong> </p> <p>The Disadvantage of using this way is &apos; <strong>Memory wastage</strong> ,&apos; as the memory allocated to every String in the Array will be the memory required to store the longest String of the Array.</p> <p> <strong>2. Using Pointers:</strong> </p> <p>Using Pointers, we create a single-dimensional Array of Pointers pointing to Strings. Following this method can eliminate the &apos;Memory wastage&apos; Disadvantage.</p> <hr></3;></pre></3;></pre></3;></pre></5;>

V C jsou znak a řetězec samostatné datové typy, na rozdíl od jiných programovacích jazyků, jako je Python. A String je sbírka znaků. K definování řetězce tedy používáme pole znaků:

 #include int main() { char str[8]; printf(&apos;Enter a String: &apos;); scanf(&apos;%s&apos;, &amp;str); printf(&apos;%s&apos;, str); } 

Výstup:

 Enter a String: Hello Hello 

Nyní chceme vytvořit Array of Strings, což znamená, že se snažíme vytvořit Array of Character Arrays. Máme dva způsoby, jak to udělat:

  1. Použití dvourozměrných polí
  2. Použití ukazatelů

Použití dvourozměrných polí:

Vytvoření pole String Array je jednou z aplikací dvourozměrných polí. Chcete-li si udělat obrázek o uspořádání, sledujte níže uvedené znázornění:

Předpokládejme, že chceme vytvořit pole 3 řetězců o velikosti 5:

inurl:.git/head
Pole strun v C

Každý řetězec v poli řetězců musí končit znakem null. Je to vlastnost řetězce v C.

Syntaxe pro vytvoření 2D pole:

 Data_type name[rows][columns] = {{values in row 1}, {values in row 2}&#x2026;}; 

Syntaxe pro vytvoření pole řetězců:

 char Array[rows][columns] = {&apos;String1&apos;, &apos;String2&apos;...}; 

Nyní vytvoříme příklad String Array:

  • Všimněte si, že když přiřadíme počet řádků a sloupců, musíme vzít v úvahu délku Null Character.
 #include int main() { int i; char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Block&apos;}; printf(&apos;String Array: 
&apos;); for(i = 0; i <3; i++) { printf(\'%s
\', array[i]); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> String Array: Black Blame Block </pre> <ul> <li>char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Black&apos;} -&gt; {{&apos;B&apos;, &apos;l&apos;, &apos;a&apos;, &apos;c&apos;, &apos;k&apos;, &apos;&apos;}, {&apos;B&apos;, &apos;l&apos;, &apos;a&apos;, &apos;m&apos;, &apos;e&apos;, &apos;&apos;}, {&apos;B&apos;, &apos;l&apos;, &apos;a&apos;, &apos;c&apos;, &apos;k&apos;, &apos;&apos;}}</li> <li>We cannot directly manipulate the Strings in the Array as a String is an immutable data type. The compiler raises an error:</li> </ul> <pre> char Array[0] = &apos;Hello&apos;; </pre> <p> <strong>Output:</strong> </p> <pre> [Error] assignment to expression with Array type </pre> <ul> <li>We can use the strcpy() function to copy the value by importing the String header file:</li> </ul> <pre> char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Block&apos;}; strcpy(Array[0], &apos;Hello&apos;); for(i = 0; i <3; i++) { printf(\'%s
\', array[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> String Array: Hello Blame Block </pre> <p> <strong>The Disadvantage of using 2D Arrays:</strong> </p> <p>Suppose we want to store 4 Strings in an Array: {&apos;Java&apos;, &apos;T&apos;, &apos;point&apos;, &apos;JavaTpoint&apos;}. We will store the Strings like this:</p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c-2.webp" alt="An Array of Strings in C"> <ul> <li>The number of rows will be equal to the number of Strings, but the number of columns will equal the length of the longest String.</li> <li>The memory allocated to all the Strings will be the size of the longest String, causing &apos; <strong>Memory wastage</strong> &apos;.</li> <li>The orange part in the above representation is the memory wasted.</li> </ul> <h3>Using Pointers:</h3> <p>By using Pointers, we can avoid the Disadvantage of Memory wastage. But how do we do this?</p> <p>We need to create an Array of Pointers pointing to Strings. Hence, we need to create an Array of type &apos; <strong>char*</strong> &apos;. This way, all the Strings are stored elsewhere in the exactly needed memory, and the Pointers in the Array point to those memory locations causing no memory wastage. More specifically, the Pointers in the Array point to the first Character of the Strings.</p> <p> <strong>Syntax to create an Array of Pointers:</strong> </p> <p>Data Type* name[] = {&apos;Value 1&apos;, &apos;Value 2&apos;&#x2026;};</p> <p> <strong>Syntax to create an Array of String Pointers:</strong> </p> <p>char* Array[] = {&apos;String 1&apos;, &apos;String 2&apos;&#x2026;};</p> <p> <strong>Representation:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c-3.webp" alt="An Array of Strings in C"> <p> <strong>Now, let us create an example String Array:</strong> </p> <pre> #include #include int main() { int i; char* Array[] = {&apos;HI&apos;, &apos;UP&apos;, &apos;AT&apos;}; printf(&apos;String Array:
&apos;); for(i = 0; i <3; i++) { printf(\'%s
\', array[i]); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> String Array: HI UP AT </pre> <h3>Summary:</h3> <p>We cannot create a String Array like a normal one, as a String is an Array of Characters. We have two ways to do this:</p> <p> <strong>1. Using a Two-Dimensional Array:</strong> </p> <p>The Disadvantage of using this way is &apos; <strong>Memory wastage</strong> ,&apos; as the memory allocated to every String in the Array will be the memory required to store the longest String of the Array.</p> <p> <strong>2. Using Pointers:</strong> </p> <p>Using Pointers, we create a single-dimensional Array of Pointers pointing to Strings. Following this method can eliminate the &apos;Memory wastage&apos; Disadvantage.</p> <hr></3;></pre></3;></pre></3;>
  • char Array[3][6] = {'Black', 'Blame', 'Black'} -> {{'B', 'l', 'a', 'c', 'k', '' }, {'B', 'l', 'a', 'm', 'e', ​​''}, {'B', 'l', 'a', 'c', 'k', ''}}
  • Nemůžeme přímo manipulovat s řetězci v poli, protože řetězec je neměnný datový typ. Kompilátor hlásí chybu:
 char Array[0] = &apos;Hello&apos;; 

Výstup:

jak otevřít soubor pomocí java
 [Error] assignment to expression with Array type 
  • Ke zkopírování hodnoty můžeme použít funkci strcpy() importem hlavičkového souboru String:
 char Array[3][6] = {&apos;Black&apos;, &apos;Blame&apos;, &apos;Block&apos;}; strcpy(Array[0], &apos;Hello&apos;); for(i = 0; i <3; i++) { printf(\'%s
\', array[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> String Array: Hello Blame Block </pre> <p> <strong>The Disadvantage of using 2D Arrays:</strong> </p> <p>Suppose we want to store 4 Strings in an Array: {&apos;Java&apos;, &apos;T&apos;, &apos;point&apos;, &apos;JavaTpoint&apos;}. We will store the Strings like this:</p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c-2.webp" alt="An Array of Strings in C"> <ul> <li>The number of rows will be equal to the number of Strings, but the number of columns will equal the length of the longest String.</li> <li>The memory allocated to all the Strings will be the size of the longest String, causing &apos; <strong>Memory wastage</strong> &apos;.</li> <li>The orange part in the above representation is the memory wasted.</li> </ul> <h3>Using Pointers:</h3> <p>By using Pointers, we can avoid the Disadvantage of Memory wastage. But how do we do this?</p> <p>We need to create an Array of Pointers pointing to Strings. Hence, we need to create an Array of type &apos; <strong>char*</strong> &apos;. This way, all the Strings are stored elsewhere in the exactly needed memory, and the Pointers in the Array point to those memory locations causing no memory wastage. More specifically, the Pointers in the Array point to the first Character of the Strings.</p> <p> <strong>Syntax to create an Array of Pointers:</strong> </p> <p>Data Type* name[] = {&apos;Value 1&apos;, &apos;Value 2&apos;&#x2026;};</p> <p> <strong>Syntax to create an Array of String Pointers:</strong> </p> <p>char* Array[] = {&apos;String 1&apos;, &apos;String 2&apos;&#x2026;};</p> <p> <strong>Representation:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/46/an-array-strings-c-3.webp" alt="An Array of Strings in C"> <p> <strong>Now, let us create an example String Array:</strong> </p> <pre> #include #include int main() { int i; char* Array[] = {&apos;HI&apos;, &apos;UP&apos;, &apos;AT&apos;}; printf(&apos;String Array:
&apos;); for(i = 0; i <3; i++) { printf(\'%s
\', array[i]); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> String Array: HI UP AT </pre> <h3>Summary:</h3> <p>We cannot create a String Array like a normal one, as a String is an Array of Characters. We have two ways to do this:</p> <p> <strong>1. Using a Two-Dimensional Array:</strong> </p> <p>The Disadvantage of using this way is &apos; <strong>Memory wastage</strong> ,&apos; as the memory allocated to every String in the Array will be the memory required to store the longest String of the Array.</p> <p> <strong>2. Using Pointers:</strong> </p> <p>Using Pointers, we create a single-dimensional Array of Pointers pointing to Strings. Following this method can eliminate the &apos;Memory wastage&apos; Disadvantage.</p> <hr></3;></pre></3;>

Nevýhoda použití 2D polí:

Předpokládejme, že chceme uložit 4 řetězce do pole: {'Java', 'T', 'point', 'JavaTpoint'}. Řetězce uložíme takto:

Pole strun v C
  • Počet řádků se bude rovnat počtu řetězců, ale počet sloupců se bude rovnat délce nejdelšího řetězce.
  • Paměť přidělená všem řetězcům bude mít velikost nejdelšího řetězce, což způsobí ' Plýtvání pamětí '.
  • Oranžová část ve výše uvedené reprezentaci je promarněná paměť.

Použití ukazatelů:

Použitím ukazatelů se můžeme vyhnout nevýhodě plýtvání pamětí. Ale jak to uděláme?

Musíme vytvořit pole ukazatelů ukazujících na řetězce. Proto musíme vytvořit pole typu ' char* '. Tímto způsobem jsou všechny řetězce uloženy jinde v přesně potřebné paměti a ukazatele v poli ukazují na ta paměťová místa, která nezpůsobují plýtvání pamětí. Přesněji, ukazatele v poli ukazují na první znak řetězců.

Syntaxe pro vytvoření pole ukazatelů:

Typ dat* název[] = {'Hodnota 1', 'Hodnota 2'…};

Syntaxe pro vytvoření pole řetězcových ukazatelů:

char* Pole[] = {'Řetězec 1', 'Řetězec 2'…};

Reprezentace:

Pole strun v C

Nyní vytvoříme příklad String Array:

 #include #include int main() { int i; char* Array[] = {&apos;HI&apos;, &apos;UP&apos;, &apos;AT&apos;}; printf(&apos;String Array:
&apos;); for(i = 0; i <3; i++) { printf(\'%s
\', array[i]); } return 0; < pre> <p> <strong>Output:</strong> </p> <pre> String Array: HI UP AT </pre> <h3>Summary:</h3> <p>We cannot create a String Array like a normal one, as a String is an Array of Characters. We have two ways to do this:</p> <p> <strong>1. Using a Two-Dimensional Array:</strong> </p> <p>The Disadvantage of using this way is &apos; <strong>Memory wastage</strong> ,&apos; as the memory allocated to every String in the Array will be the memory required to store the longest String of the Array.</p> <p> <strong>2. Using Pointers:</strong> </p> <p>Using Pointers, we create a single-dimensional Array of Pointers pointing to Strings. Following this method can eliminate the &apos;Memory wastage&apos; Disadvantage.</p> <hr></3;>

Souhrn:

Nemůžeme vytvořit pole řetězců jako normální pole, protože řetězec je pole znaků. Máme dva způsoby, jak to udělat:

1. Použití dvourozměrného pole:

Nevýhoda použití tohoto způsobu je „ Plýtvání pamětí ,' protože paměť přidělená každému řetězci v poli bude paměť potřebná k uložení nejdelšího řetězce pole.

vzory softwaru Java

2. Použití ukazatelů:

Pomocí ukazatelů vytvoříme jednorozměrné pole ukazatelů směřujících na řetězce. Dodržování této metody může eliminovat nevýhodu „plýtvání pamětí“.