<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Call by Reference - Syntaxfehler?]]></title><description><![CDATA[<p>Hallöchen allerseits! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>ich habe noch Schwierigkeiten bzgl. callbyreference-Funktionen. In meinem Buch steht folgende Beispielaufgabe, die ich soweit auch nachvollziehen kann:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std;

void Dreieckstausch(int *Zeiger1, int *Zeiger2)
{
     int temp;
     temp=*Zeiger1;
     *Zeiger1=*Zeiger2;
     *Zeiger2=temp;
     return;
}

int main()
{
    int zahl1=3,zahl2=8;
    cout&lt;&lt;&quot;--- Vorher:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Zahl1 = &quot;&lt;&lt;zahl1&lt;&lt;endl
        &lt;&lt;&quot;Zahl2 = &quot;&lt;&lt;zahl2&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl1 = &quot;&lt;&lt;&amp;zahl1&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl2 = &quot;&lt;&lt;&amp;zahl2&lt;&lt;endl&lt;&lt;endl;
    Dreieckstausch(&amp;zahl1,&amp;zahl2);
    cout&lt;&lt;&quot;--- Nachher:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Zahl1 = &quot;&lt;&lt;zahl1&lt;&lt;endl
        &lt;&lt;&quot;Zahl2 = &quot;&lt;&lt;zahl2&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl1 = &quot;&lt;&lt;&amp;zahl1&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl2 = &quot;&lt;&lt;&amp;zahl2&lt;&lt;endl&lt;&lt;endl;

    getch();
}
</code></pre>
<p><em>Ausgabe:</em><br />
<a href="http://www.abload.de/img/cbrefbpg7.jpg" rel="nofollow">http://www.abload.de/img/cbrefbpg7.jpg</a></p>
<p>nun wollte ich versuchen, es in einem eigenen Programm zu verwirklichen: Strom (i) und Spannung (u) sind gegeben. Errechnet werden sollen Widerstand (r) und Leistung (p) mit folgenden Formeln:<br />
<strong>r=u/i</strong><br />
und<br />
<strong>p=u*i</strong></p>
<p>Die Ergebnisse r und p sollen an das Hauptprogramm zurückgegeben und dort ausgegeben werden. Das habe ich folgendermaßen versucht, zu programmieren:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std;

void ohmsches(int *u, int *i){
     int r,p;
     r = *u / *i;
     p = *u * *i;
     return;
}

int main (){
    int spannung=230, strom=2, widerstand=0, leistung=0;
    cout&lt;&lt;&quot;---Gegeben:&quot;&lt;&lt;endl
        &lt;&lt;&quot;  Spannung = &quot;&lt;&lt;spannung&lt;&lt;endl
        &lt;&lt;&quot;     Strom = &quot;&lt;&lt;strom&lt;&lt;endl&lt;&lt;endl;

    ohmsches(&amp;spannung,&amp;strom);

    cout&lt;&lt;&quot;---Gesucht:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Widerstand = &quot;&lt;&lt;widerstand&lt;&lt;endl
        &lt;&lt;&quot;  Leistung = &quot;&lt;&lt;leistung;

    getch();
}
</code></pre>
<p><em>Ausgabe:</em><br />
<a href="http://www.abload.de/img/ohm2u76.jpg" rel="nofollow">http://www.abload.de/img/ohm2u76.jpg</a></p>
<p>Allerdings bleiben r und p unverändert. Was mache ich falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/249420/call-by-reference-syntaxfehler</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 13:53:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/249420.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 07 Sep 2009 13:02:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Call by Reference - Syntaxfehler? on Mon, 07 Sep 2009 13:02:32 GMT]]></title><description><![CDATA[<p>Hallöchen allerseits! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>ich habe noch Schwierigkeiten bzgl. callbyreference-Funktionen. In meinem Buch steht folgende Beispielaufgabe, die ich soweit auch nachvollziehen kann:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std;

void Dreieckstausch(int *Zeiger1, int *Zeiger2)
{
     int temp;
     temp=*Zeiger1;
     *Zeiger1=*Zeiger2;
     *Zeiger2=temp;
     return;
}

int main()
{
    int zahl1=3,zahl2=8;
    cout&lt;&lt;&quot;--- Vorher:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Zahl1 = &quot;&lt;&lt;zahl1&lt;&lt;endl
        &lt;&lt;&quot;Zahl2 = &quot;&lt;&lt;zahl2&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl1 = &quot;&lt;&lt;&amp;zahl1&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl2 = &quot;&lt;&lt;&amp;zahl2&lt;&lt;endl&lt;&lt;endl;
    Dreieckstausch(&amp;zahl1,&amp;zahl2);
    cout&lt;&lt;&quot;--- Nachher:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Zahl1 = &quot;&lt;&lt;zahl1&lt;&lt;endl
        &lt;&lt;&quot;Zahl2 = &quot;&lt;&lt;zahl2&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl1 = &quot;&lt;&lt;&amp;zahl1&lt;&lt;endl
        &lt;&lt;&quot;- Adresse von Zahl2 = &quot;&lt;&lt;&amp;zahl2&lt;&lt;endl&lt;&lt;endl;

    getch();
}
</code></pre>
<p><em>Ausgabe:</em><br />
<a href="http://www.abload.de/img/cbrefbpg7.jpg" rel="nofollow">http://www.abload.de/img/cbrefbpg7.jpg</a></p>
<p>nun wollte ich versuchen, es in einem eigenen Programm zu verwirklichen: Strom (i) und Spannung (u) sind gegeben. Errechnet werden sollen Widerstand (r) und Leistung (p) mit folgenden Formeln:<br />
<strong>r=u/i</strong><br />
und<br />
<strong>p=u*i</strong></p>
<p>Die Ergebnisse r und p sollen an das Hauptprogramm zurückgegeben und dort ausgegeben werden. Das habe ich folgendermaßen versucht, zu programmieren:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std;

void ohmsches(int *u, int *i){
     int r,p;
     r = *u / *i;
     p = *u * *i;
     return;
}

int main (){
    int spannung=230, strom=2, widerstand=0, leistung=0;
    cout&lt;&lt;&quot;---Gegeben:&quot;&lt;&lt;endl
        &lt;&lt;&quot;  Spannung = &quot;&lt;&lt;spannung&lt;&lt;endl
        &lt;&lt;&quot;     Strom = &quot;&lt;&lt;strom&lt;&lt;endl&lt;&lt;endl;

    ohmsches(&amp;spannung,&amp;strom);

    cout&lt;&lt;&quot;---Gesucht:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Widerstand = &quot;&lt;&lt;widerstand&lt;&lt;endl
        &lt;&lt;&quot;  Leistung = &quot;&lt;&lt;leistung;

    getch();
}
</code></pre>
<p><em>Ausgabe:</em><br />
<a href="http://www.abload.de/img/ohm2u76.jpg" rel="nofollow">http://www.abload.de/img/ohm2u76.jpg</a></p>
<p>Allerdings bleiben r und p unverändert. Was mache ich falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1773827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1773827</guid><dc:creator><![CDATA[Psychedelixx]]></dc:creator><pubDate>Mon, 07 Sep 2009 13:02:32 GMT</pubDate></item><item><title><![CDATA[Reply to Call by Reference - Syntaxfehler? on Mon, 07 Sep 2009 13:05:03 GMT]]></title><description><![CDATA[<p>weil du die 2 variablen die du an die funktion übergibst nicht die sidn die beschreiben werden</p>
<p>du erzeugst ind er funktion 2 neue ints die du dann mit den werten belegst<br />
aber wenn die funktion zu ende ist werden diese beiden ints wieder gelöscht</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1773830</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1773830</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 07 Sep 2009 13:05:03 GMT</pubDate></item><item><title><![CDATA[Reply to Call by Reference - Syntaxfehler? on Mon, 07 Sep 2009 13:08:03 GMT]]></title><description><![CDATA[<p>Du übergibst ja <code>widerstand</code> und <code>leistung</code> gar nicht an die Funktion. Irgendwie müssen die schon kommunizieren. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Eine Möglichkeit:</p>
<pre><code class="language-cpp">// Übergabe per:          value        value      reference        reference
void berechne_groessen(int spannung, int strom, int* widerstand, int* leistung)
{
    *widerstand = spannung / strom;
    *leistung   = spannung * strom;
}
</code></pre>
<p>Aufruf:</p>
<pre><code class="language-cpp">berechne_groessen(spannung, strom, &amp;widerstand, &amp;leistung);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1773833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1773833</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 07 Sep 2009 13:08:03 GMT</pubDate></item><item><title><![CDATA[Reply to Call by Reference - Syntaxfehler? on Mon, 07 Sep 2009 13:14:32 GMT]]></title><description><![CDATA[<p>Nichtsdestotrotz würde ich eher so etwas machen wie:</p>
<pre><code>int widerstand(int const &amp;u, int const &amp;i){
     return u / i;
} 

int leistung(int const &amp;u, int const &amp;i){
     return u * i;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1773837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1773837</guid><dc:creator><![CDATA[jencas]]></dc:creator><pubDate>Mon, 07 Sep 2009 13:14:32 GMT</pubDate></item><item><title><![CDATA[Reply to Call by Reference - Syntaxfehler? on Mon, 07 Sep 2009 13:16:36 GMT]]></title><description><![CDATA[<p>jencas schrieb:</p>
<blockquote>
<p>Nichtsdestotrotz würde ich eher so etwas machen wie:</p>
</blockquote>
<p>Ja, ich eigentlich auch. Aber das hilft ihm nicht besonders viel, was Call by Reference angeht. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Ausserdem würde ich skalare Typen nicht als Const-Referenz übergeben, das ist den Aufwand meist nicht wert (Grösse ist etwa gleich wie die der Referenz, aber man erhält den Dereferenzierungsoverhead).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1773841</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1773841</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 07 Sep 2009 13:16:36 GMT</pubDate></item><item><title><![CDATA[Reply to Call by Reference - Syntaxfehler? on Mon, 07 Sep 2009 14:03:51 GMT]]></title><description><![CDATA[<p>Danke Nexus!<br />
jetzt versteh ich auch das Prinzip. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Danke auch an die anderen beiden. Ich wollte allerdings wissen, wie es mit mehreren Funktionswerten funktioniert, deswegen war Nexus' Lösung hilfreicher <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>hier noch mal für alle später Suchenden mein fertiges, funktionierendes Programm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
using namespace std;

//            value  value    ref    ref
//              |      |       |      |
//              V      V       V      V
void ohmsches(int u, int i, int* r, int* p){   //Funktion
     *r = u / i;
     *p = u * i;
}

int main (){
    int spannung=230, strom=2, widerstand, leistung;
    cout&lt;&lt;&quot;---Gegeben:&quot;&lt;&lt;endl
        &lt;&lt;&quot;  Spannung = &quot;&lt;&lt;spannung&lt;&lt;&quot; Volt&quot;&lt;&lt;endl
        &lt;&lt;&quot;     Strom =   &quot;&lt;&lt;strom&lt;&lt;&quot; Ampere&quot;&lt;&lt;endl&lt;&lt;endl;

//            value   value      ref      ref
//              |       |         |        |
//              V       V         V        V       
    ohmsches(spannung,strom,&amp;widerstand,&amp;leistung);  //Funktionsaufruf

    cout&lt;&lt;&quot;---Gesucht:&quot;&lt;&lt;endl
        &lt;&lt;&quot;Widerstand = &quot;&lt;&lt;widerstand&lt;&lt;&quot; Ohm&quot;&lt;&lt;endl
        &lt;&lt;&quot;  Leistung = &quot;&lt;&lt;leistung&lt;&lt;&quot; Watt&quot;;

    getch();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1773875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1773875</guid><dc:creator><![CDATA[Psychedelixx]]></dc:creator><pubDate>Mon, 07 Sep 2009 14:03:51 GMT</pubDate></item></channel></rss>