<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FireTeam &#187; vettori</title>
	<atom:link href="http://www.fireteam.it/tag/vettori/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fireteam.it</link>
	<description>#fuoco@AzzurraNet</description>
	<lastBuildDate>Tue, 16 Mar 2010 15:55:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Array Multidimensionali in C</title>
		<link>http://www.fireteam.it/2009/07/array-multidimensionali-in-c/</link>
		<comments>http://www.fireteam.it/2009/07/array-multidimensionali-in-c/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 15:50:48 +0000</pubDate>
		<dc:creator>saverio</dc:creator>
				<category><![CDATA[Programmazione C]]></category>
		<category><![CDATA[puntatori]]></category>
		<category><![CDATA[stringhe]]></category>
		<category><![CDATA[vettori]]></category>

		<guid isPermaLink="false">http://www.fireteam.it/?p=367</guid>
		<description><![CDATA[Questo articolo tratta la gestione delle stringhe e, in generale, dei vettori multidimensionali nel linguaggio C.
Allocazione di memoria del vettore
Sappiamo che nel linguaggio c dichiariamo un vettore con l&#8217;istruzione
int vett[n];
in questo modo creiamo un vettore chiamato vett di n elementi:
vett[0] , vett[1] , &#8230; , vett[n-1]
Supponiamo, con n=5, di riempire il vettore in questo modo:



10
11
12
13
14



Risulterà [...]]]></description>
			<content:encoded><![CDATA[<p>Questo articolo tratta la gestione delle stringhe e, in generale, dei vettori multidimensionali nel linguaggio C.</p>
<h2>Allocazione di memoria del vettore</h2>
<p>Sappiamo che nel linguaggio c dichiariamo un vettore con l&#8217;istruzione</p>
<p>int vett[n];</p>
<p>in questo modo creiamo un vettore chiamato vett di n elementi:</p>
<p>vett[0] , vett[1] , &#8230; , vett[n-1]</p>
<p>Supponiamo, con n=5, di riempire il vettore in questo modo:</p>
<table border="1">
<tbody>
<tr>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
</tr>
</tbody>
</table>
<p>Risulterà che:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"> vett<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>vett
 vett<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">11</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>vett<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span>
 vett<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">12</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>vett<span style="color: #339933;">+</span><span style="color: #0000dd;">2</span>
 vett<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">13</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>vett<span style="color: #339933;">+</span><span style="color: #0000dd;">3</span>
 vett<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">14</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>vett<span style="color: #339933;">+</span><span style="color: #0000dd;">4</span></pre></td></tr></table></div>

<p><span id="more-367"></span></p>
<p>Inoltre supponiamo di aver fatto girare al computer il programma,<br />
avremo alcuni indirizzi se stampiamo:</p>
<p>804400008 = vett<br />
804400012 = vett+1<br />
804400016 = vett+2<br />
804400020 = vett+3<br />
804400024 = vett+4</p>
<h2>Passare il vettore ad una funzione</h2>
<p>Ora che sappiamo come viene creato un vettore in memoria affrontiamo<br />
il problema di passare il vettore nelle funzioni. Supponiamo la funzione:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> funzione<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>vettore<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
 vettore<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>che aggiunge 20 ad ogni elemento del vettore. Quale sarà la chiamata giusta<br />
da fare nel programma chiamante?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 .
 .
 .
 <span style="color: #202020;">funzione</span><span style="color: #009900;">&#40;</span>vett<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 .
 .
 .
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Perchè passiamo &#8220;vett&#8221; e non &#8220;&amp;vett&#8221;?<br />
Il contenuto di &#8220;vett&#8221; non è altro che un indirizzo&#8230; il primo elemento del<br />
vettore è &#8220;*vett&#8221; oppure &#8220;vett[0]&#8220;.<br />
Quindi con questa chiamata non facciamo altro che passare per valore alla<br />
funzione un indirizzo che è proprio l&#8217;indirizzo del nostro vettore <img src='http://www.fireteam.it/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Praticamente nella funzione viene dichiarato un puntatore ad intero chiamato<br />
&#8220;vettore&#8221;. La dichiarazione è</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>vettore</pre></td></tr></table></div>

<p>Quindi in vettore mettiamo l&#8217;indirizzo che nell&#8217;esempio è 804400008.<br />
All&#8217;intrno della funzione risulterà che:</p>
<p>vettore   = 804400008          &lt;&#8211; indirizzo<br />
*vettore   = 10                 &lt;&#8211; contenuto di vett[0]<br />
*vettore+1 = 11                 &lt;&#8211; contenuto di vett[1]</p>
<p>Questo è il motivo che nel linguaggio C non è obbligatorio definire la<br />
dimensione del vettore!</p>
<h2>STAMPA SU VIDEO DI UN VETTORE v DI n ELEMENTI</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> StampaVettore<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> <span style="color: #339933;">*</span>v<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> Vettore = ( &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
 <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%.2f %s&quot;</span><span style="color: #339933;">,</span>v<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%.2f %s <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>v<span style="color: #009900;">&#91;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot; );&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>STAMPA SU VIDEO DI UNA MATRICE M DI n RIGHE ED m COLONNE</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> StampaMatrice<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">float</span> <span style="color: #339933;">**</span>M<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span>j<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>m<span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%.2f %s&quot;</span><span style="color: #339933;">,</span>M<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;   &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.fireteam.it/2009/07/array-multidimensionali-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
