This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays-c.html
69 lines (57 loc) · 3.89 KB
/
arrays-c.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="description" content="Arrays in C, like all things in C, require a lot more care than higher level languages."><!-- Bing --><meta name="msvalidate.01" content="45CBBE1BD8265A2217DFDA630EB8F84A" /><title>Tiny Brain Fans - Arrays (C)</title><link rel="stylesheet" href="tinystyle.css"></head><body>
<main id="main"><article id="content"><h1 id="title">Arrays (C)</h1><p>Arrays in <a href="c.html">C</a>, like all things in C, require a lot more care than higher level languages.</p>
<h2>Initialization</h2>
<p>Arrays are intialized as normal, but followed by <code>n</code> items that should reside in the array: <code>int numbers[n]</code>. </p>
<p>You can set the values of each of these items with curly braces. </p>
<pre><code class="language-c">int array[5] = { 1, 2, 3, 4, 5 };
int zeroArray[10] = { 0 };
</code></pre>
<p>In an array of integers, if the list of values within the curly braces is less than the amount needed to fill it completely, all remaining values will resort to zero. Meaning that you can instantiate an array of integers with 0 via <code>int array[n] = { 0 }</code>.</p>
<h2>Access/Modification</h2>
<p>Arrays can be accessed or modified as normal via square brackets.</p>
<pre><code class="language-c">int array[8] = { 0 };
array[0] = 100;
array[4] = 500;
</code></pre>
<h2>Information</h2>
<h3>Size/Length</h3>
<p>The size or length of an array needs some calculation. The built in <code>sizeof</code> function will return the size of an array <em>in bytes</em>, which is not particularly helpful unless you know the size of each cell. So you can use the <code>sizeof</code> function to get the size of the first cell and divide the total size by that value.</p>
<pre><code class="language-c">#include <stdio.h>
int main()
{
int total, single, len;
int array[10];
total = sizeof(array);
single = sizeof(array[0]);
len = total / single;
printf("total: %d, single: %d,len: %d", total, single, len);
// total: 40, single: 4, len: 10
return 0;
}
</code></pre>
<p>Integers that are passed in as arguments will not be able to utilize this technique. The length will need to be calculated ahead of time and passed in as another argument.</p>
<pre><code class="language-c">#include <stdio.h>
void reverse(char str[], int len);
int main()
{
int total, single, len;
char array[] = "hello";
total = sizeof(array);
single = sizeof(array[0]);
len = total / single;
printf("%d, %d, %d\n", total, single, len);
reverse(array, len);
return 0;
}
void reverse(char str[], int len)
{
int i;
for (i = len - 1; i >= 0; i--)
printf("%c", str[i]);
}
</code></pre>
<h2>References</h2>
<ol>
<li><a href="https://stackoverflow.com/questions/201101/how-to-initialize-all-members-of-an-array-to-the-same-value" target="_blank">https://stackoverflow.com/questions/201101/how-to-initialize-all-members-of-an-array-to-the-same-value</a></li>
</ol>
<section id="backlinks"><details><summary>Backlinks</summary><ul><li><a href="c-data-types.html">C Data Types</a></li><li><a href="strings-c.html">Strings (C)</a></li></ul></details></section><p class="last-modified">Last modified: 202206101419</p></article></main><footer><nav><a href="index.html">Sitemap</a></nav><div class="social"><p>Built using <a href="http://codeberg.org/milofultz/swiki" target="_blank" rel="noopener noreferrer">{{SWIKI}}</a></p><p><a href="http://codeberg.org/milofultz/" target="_blank" rel="noopener noreferrer">Codeberg</a></p><p><a href="http://milofultz.com/" target="_blank" rel="noopener noreferrer">milofultz.com</a></p><p><a href="https://merveilles.town/@milofultz" target="_blank" rel="me noopener noreferrer">Mastodon</a></p></div></footer></body></html>