Az Array a C legegyszerűbb adatstruktúrája, amely homogén adatokat tárol egymás melletti memóriahelyeken. Ha tömböt akarunk létrehozni, akkor deklaráljuk az adattípust, és elemeket adunk hozzá:
#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('Enter a String: '); scanf('%s', &str); printf('%s', 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}…}; </pre> <p> <strong>Syntax to create a String Array:</strong> </p> <pre> char Array[rows][columns] = {'String1', 'String2'...}; </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] = {'Black', 'Blame', 'Block'}; printf('String Array: '); 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] = {'Black', 'Blame', 'Black'} -> {{'B', 'l', 'a', 'c', 'k', ' '}, {'B', 'l', 'a', 'm', 'e', ' '}, {'B', 'l', 'a', 'c', 'k', ' '}}</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] = 'Hello'; </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] = {'Black', 'Blame', 'Block'}; strcpy(Array[0], 'Hello'); 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: {'Java', 'T', 'point', 'JavaTpoint'}. 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 ' <strong>Memory wastage</strong> '.</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 ' <strong>char*</strong> '. 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[] = {'Value 1', 'Value 2'…};</p> <p> <strong>Syntax to create an Array of String Pointers:</strong> </p> <p>char* Array[] = {'String 1', 'String 2'…};</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[] = {'HI', 'UP', 'AT'}; printf('String Array: '); 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 ' <strong>Memory wastage</strong> ,' 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 'Memory wastage' Disadvantage.</p> <hr></3;></pre></3;></pre></3;></pre></5;>
A C nyelvben a karakterek és a karakterláncok külön adattípusok, ellentétben más programozási nyelvekkel, például a Pythonnal. A karakterlánc karakterek gyűjteménye. Ezért a karakterlánc meghatározásához karaktertömböt használunk:
#include int main() { char str[8]; printf('Enter a String: '); scanf('%s', &str); printf('%s', str); }
Kimenet:
Enter a String: Hello Hello
Most szeretnénk létrehozni egy karakterlánc tömböt, ami azt jelenti, hogy megpróbálunk létrehozni egy karaktertömböt. Ezt kétféleképpen tudjuk megtenni:
- Kétdimenziós tömbök használata
- Mutatók használata
Kétdimenziós tömbök használata:
A String Array létrehozása a kétdimenziós tömbök egyik alkalmazása. Ha képet szeretne kapni az elrendezésről, kövesse az alábbi ábrázolást:
Tegyük fel, hogy egy 3 5-ös méretű karakterláncból álló tömböt akarunk létrehozni:
hogyan kell java-t nyomtatni
A String Array minden karakterláncának null karakterrel kell végződnie. Ez egy karakterlánc tulajdonsága C-ben.
Szintaxis 2D tömb létrehozásához:
Data_type name[rows][columns] = {{values in row 1}, {values in row 2}…};
Szintaxis karakterlánc-tömb létrehozásához:
char Array[rows][columns] = {'String1', 'String2'...};
Most hozzunk létre egy példa String Array-t:
"kőműves képlete"
- Figyeljük meg, hogy amikor a sorok és oszlopok számát hozzárendeljük, a nulla karaktert kell figyelembe vennünk a hosszhoz.
#include int main() { int i; char Array[3][6] = {'Black', 'Blame', 'Block'}; printf('String Array: '); 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] = {'Black', 'Blame', 'Black'} -> {{'B', 'l', 'a', 'c', 'k', ' '}, {'B', 'l', 'a', 'm', 'e', ' '}, {'B', 'l', 'a', 'c', 'k', ' '}}</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] = 'Hello'; </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] = {'Black', 'Blame', 'Block'}; strcpy(Array[0], 'Hello'); 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: {'Java', 'T', 'point', 'JavaTpoint'}. 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 ' <strong>Memory wastage</strong> '.</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 ' <strong>char*</strong> '. 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[] = {'Value 1', 'Value 2'…};</p> <p> <strong>Syntax to create an Array of String Pointers:</strong> </p> <p>char* Array[] = {'String 1', 'String 2'…};</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[] = {'HI', 'UP', 'AT'}; printf('String Array: '); 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 ' <strong>Memory wastage</strong> ,' 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 'Memory wastage' Disadvantage.</p> <hr></3;></pre></3;></pre></3;>
- char Array[3][6] = {'Fekete', 'Blame', 'Black'} -> {{'B', 'l', 'a', 'c', 'k', ' ' }, {'B', 'l', 'a', 'm', 'e', ' '}, {'B', 'l', 'a', 'c', 'k', ' '}}
- A tömbben lévő karakterláncokat nem tudjuk közvetlenül manipulálni, mivel a karakterlánc megváltoztathatatlan adattípus. A fordító hibát jelez:
char Array[0] = 'Hello';
Kimenet:
[Error] assignment to expression with Array type
- Az strcpy() függvény segítségével másolhatjuk az értéket a String fejlécfájl importálásával:
char Array[3][6] = {'Black', 'Blame', 'Block'}; strcpy(Array[0], 'Hello'); 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: {'Java', 'T', 'point', 'JavaTpoint'}. 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 ' <strong>Memory wastage</strong> '.</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 ' <strong>char*</strong> '. 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[] = {'Value 1', 'Value 2'…};</p> <p> <strong>Syntax to create an Array of String Pointers:</strong> </p> <p>char* Array[] = {'String 1', 'String 2'…};</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[] = {'HI', 'UP', 'AT'}; printf('String Array: '); 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 ' <strong>Memory wastage</strong> ,' 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 'Memory wastage' Disadvantage.</p> <hr></3;></pre></3;>
A 2D tömbök használatának hátrányai:
Tegyük fel, hogy 4 karakterláncot szeretnénk tárolni egy tömbben: {'Java', 'T', 'point', 'JavaTpoint'}. A karakterláncokat így tároljuk:
- A sorok száma megegyezik a karakterláncok számával, de az oszlopok száma megegyezik a leghosszabb karakterlánc hosszával.
- Az összes karakterlánchoz lefoglalt memória akkora lesz, mint a leghosszabb karakterlánc, ami ' Memóriapazarlás '.
- A fenti ábrázolásban a narancssárga rész az elpazarolt memória.
Mutatók használata:
A mutatók használatával elkerülhetjük a memóriapazarlás hátrányát. De hogyan tegyük ezt?
Létre kell hoznunk egy mutatótömböt, amely a karakterláncokra mutat. Ezért létre kell hoznunk egy ' típusú tömböt char* '. Ily módon az összes karakterlánc máshol tárolódik a pontosan szükséges memóriában, és a tömbben lévő mutatók azokra a memóriahelyekre mutatnak, ami nem okoz memóriapazarlást. Pontosabban, a tömb mutatói a karakterláncok első karakterére mutatnak.
Szintaxis mutatók tömbjének létrehozásához:
Adattípus* name[] = {'Érték 1', 'Érték 2'…};
Szintaxis karakterlánc-mutatók tömbjének létrehozásához:
char* Array[] = {'1. karakterlánc', '2. karakterlánc'…};
Reprezentáció:
Most hozzunk létre egy példa String Array-t:
a legszebb mosoly
#include #include int main() { int i; char* Array[] = {'HI', 'UP', 'AT'}; printf('String Array: '); 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 ' <strong>Memory wastage</strong> ,' 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 'Memory wastage' Disadvantage.</p> <hr></3;>
Összegzés:
Nem hozhatunk létre olyan karakterlánc-tömböt, mint egy normál, mivel a karakterlánc egy karaktertömb. Ennek két módja van:
1. Kétdimenziós tömb használata:
Ennek a módszernek a hátránya: Memóriapazarlás ,' mivel a tömb minden karakterláncához lefoglalt memória lesz az a memória, amely a tömb leghosszabb karakterláncának tárolásához szükséges.
2. Mutatók használata:
Mutatók segítségével létrehozunk egy egydimenziós mutatótömböt, amely a Stringekre mutat. Ennek a módszernek a követésével kiküszöbölhető a „memóriapazarlás” hátránya.
3;>3;>3;>5;>