logo

Stygų masyvas C

Masyvas yra paprasčiausia duomenų struktūra C, kuri saugo vienarūšius duomenis gretimose atminties vietose. Jei norime sukurti masyvą, deklaruojame duomenų tipą ir suteikiame jam elementus:

 #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;>

C kalboje simbolis ir eilutė yra atskiri duomenų tipai, skirtingai nuo kitų programavimo kalbų, tokių kaip Python. Styga yra simbolių rinkinys. Taigi, norėdami apibrėžti eilutę, naudojame simbolių masyvą:

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

Išvestis:

 Enter a String: Hello Hello 

Dabar norime sukurti eilučių masyvą, o tai reiškia, kad bandome sukurti simbolių masyvą. Tai galime padaryti dviem būdais:

  1. Naudojant dvimačius masyvus
  2. Rodyklės naudojimas

Naudojant dvimačius masyvus:

Styginių masyvo kūrimas yra viena iš dvimačių masyvų programų. Norėdami susidaryti vaizdą apie išdėstymą, atkreipkite dėmesį į toliau pateiktą vaizdą:

Tarkime, kad norime sukurti 3 5 dydžio eilučių masyvą:

teksto įvynioklis css
Stygų masyvas C

Kiekviena eilutė eilučių masyve turi baigtis nuliniu simboliu. Tai yra eilutės C savybė.

Sintaksė norint sukurti 2D masyvą:

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

Sintaksė norint sukurti eilučių masyvą:

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

Dabar sukurkime eilučių masyvo pavyzdį:

  • Pastebėkite, kad priskirdami eilučių ir stulpelių skaičių, ilgiui turime atsižvelgti į Null simbolį.
 #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] = {'Juoda', 'Kalta', 'Juoda'} -> {{'B', 'l', 'a', 'c', 'k', '' }, {'B', 'l', 'a', 'm', 'e', ​​''}, {'B', 'l', 'a', 'c', 'k', ''}}
  • Negalime tiesiogiai manipuliuoti masyvo eilutėmis, nes eilutė yra nekintantis duomenų tipas. Kompiliatorius iškelia klaidą:
 char Array[0] = &apos;Hello&apos;; 

Išvestis:

 [Error] assignment to expression with Array type 
  • Mes galime naudoti funkciją strcpy () norėdami nukopijuoti vertę importuodami eilutės antraštės failą:
 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;>

2D masyvų naudojimo trūkumas:

Tarkime, kad masyve norime išsaugoti 4 eilutes: {'Java', 'T', 'point', 'JavaTpoint'}. Stygas saugosime taip:

Stygų masyvas C
  • Eilučių skaičius bus lygus eilučių skaičiui, bet stulpelių skaičius bus lygus ilgiausios eilutės ilgiui.
  • Visoms eilutėms skirta atmintis bus ilgiausios eilutės dydžio, todėl „ Atminties švaistymas “.
  • Oranžinė dalis aukščiau pateiktame paveikslėlyje yra išeikvota atmintis.

Rodyklės naudojimas:

Naudodami rodykles galime išvengti atminties švaistymo trūkumo. Bet kaip tai padaryti?

Turime sukurti rodyklių masyvą, nukreipiantį į eilutes. Taigi turime sukurti masyvą, kurio tipas ' char* “. Tokiu būdu visos eilutės yra saugomos kitur tiksliai reikiamoje atmintyje, o rodyklės masyve nurodo tas atminties vietas, todėl atmintis nėra švaistoma. Tiksliau, rodyklės masyve nurodo pirmąjį stygų simbolį.

Sintaksė, norint sukurti rodyklių masyvą:

grazi kompiuterine kalba

Duomenų tipas* name[] = {'1 reikšmė', '2 reikšmė'...};

Sintaksė, norint sukurti eilutės rodyklių masyvą:

char* Array[] = {'Eilutė 1', 'Eilutė 2'...};

Atstovavimas:

Stygų masyvas C

Dabar sukurkime eilučių masyvo pavyzdį:

 #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;>

Santrauka:

Negalime sukurti eilučių masyvo kaip įprasto, nes eilutė yra simbolių masyvas. Turime du būdus tai padaryti:

1. Dvimačio masyvo naudojimas:

cast int į eilutę java

Šio būdo naudojimo trūkumas yra ' Atminties švaistymas “, nes kiekvienai masyvo eilutei skirta atmintis bus atmintis, reikalinga ilgiausiai masyvo eilutei išsaugoti.

2. Rodyklės naudojimas:

Naudodami rodykles sukuriame vienmatį rodyklių masyvą, nukreipiantį į eilutes. Taikant šį metodą galima pašalinti „atminties eikvojimo“ trūkumą.