logo

Een array van snaren in C

Een array is de eenvoudigste gegevensstructuur in C die homogene gegevens op aaneengesloten geheugenlocaties opslaat. Als we een array willen maken, declareren we het gegevenstype en geven we er elementen aan:

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

In C zijn een teken en een string afzonderlijke gegevenstypen, in tegenstelling tot andere programmeertalen zoals Python. Een String is een verzameling tekens. Om een ​​String te definiëren, gebruiken we daarom een ​​Character Array:

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

Uitgang:

 Enter a String: Hello Hello 

Nu willen we een array van strings maken, wat betekent dat we een array van karakterarrays proberen te maken. We hebben twee manieren waarop we dit kunnen doen:

  1. Tweedimensionale arrays gebruiken
  2. Aanwijzers gebruiken

Tweedimensionale arrays gebruiken:

Het maken van een String Array is een van de toepassingen van tweedimensionale arrays. Om een ​​beeld te krijgen van de opstelling, bekijk de onderstaande weergave:

Stel dat we een array van 3 strings van maat 5 willen maken:

c programmareeksarray
Een array van snaren in C

Elke string in een stringarray moet eindigen met een null-teken. Het is de eigenschap van een String in C.

Syntaxis voor het maken van een 2D-array:

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

Syntaxis voor het maken van een String Array:

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

Laten we nu een voorbeeld van een String Array maken:

hoe u kolommen uit verschillende tabellen in sql selecteert
  • Merk op dat wanneer we het aantal rijen en kolommen toewijzen, we rekening moeten houden met het nulteken voor de lengte.
 #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] = {'Zwart', 'Blame', 'Zwart'} -> {{'B', 'l', 'a', 'c', 'k', '' }, {'B', 'l', 'a', 'm', 'e', ​​''}, {'B', 'l', 'a', 'c', 'k', ''}}
  • We kunnen de strings in de array niet rechtstreeks manipuleren, omdat een string een onveranderlijk gegevenstype is. De compiler geeft een foutmelding:
 char Array[0] = &apos;Hello&apos;; 

Uitgang:

 [Error] assignment to expression with Array type 
  • We kunnen de functie strcpy() gebruiken om de waarde te kopiëren door het String-headerbestand te importeren:
 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;>

Het nadeel van het gebruik van 2D-arrays:

Stel dat we 4 strings in een array willen opslaan: {'Java', 'T', 'point', 'JavaTpoint'}. We slaan de strings als volgt op:

Een array van snaren in C
  • Het aantal rijen is gelijk aan het aantal Strings, maar het aantal kolommen is gelijk aan de lengte van de langste String.
  • Het geheugen dat aan alle Strings wordt toegewezen, heeft de grootte van de langste String, waardoor ' Geheugenverspilling '.
  • Het oranje gedeelte in de bovenstaande weergave is het verspilde geheugen.

Aanwijzers gebruiken:

Door Pointers te gebruiken, kunnen we het nadeel van geheugenverspilling vermijden. Maar hoe doen we dit?

We moeten een array van pointers maken die naar strings verwijzen. Daarom moeten we een array van het type ' teken* '. Op deze manier worden alle strings elders in het precies benodigde geheugen opgeslagen en wijzen de pointers in de array naar die geheugenlocaties, waardoor er geen geheugenverspilling ontstaat. Meer specifiek wijzen de pointers in de array naar het eerste teken van de strings.

Syntaxis om een ​​array van pointers te maken:

Gegevenstype* naam[] = {'Waarde 1', 'Waarde 2'…};

Syntaxis om een ​​array van stringpointers te maken:

char* Array[] = {'String 1', 'String 2'…};

Vertegenwoordiging:

Een array van snaren in C

Laten we nu een voorbeeld van een String Array maken:

gedeeltelijke afgeleide in latex
 #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;>

Samenvatting:

We kunnen geen String Array maken zoals een normale, omdat een String een Array van tekens is. We hebben twee manieren om dit te doen:

1. Een tweedimensionale array gebruiken:

Het nadeel van het gebruik van deze manier is ' Geheugenverspilling ,' aangezien het geheugen dat aan elke String in de Array wordt toegewezen, het geheugen zal zijn dat nodig is om de langste String van de Array op te slaan.

2. Aanwijzers gebruiken:

Met behulp van Pointers creëren we een eendimensionale array van pointers die naar strings wijzen. Door deze methode te volgen, kan het nadeel van 'geheugenverspilling' worden geëlimineerd.