<?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[Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht]]></title><description><![CDATA[<p>Hallo Forum</p>
<p>**<br />
Problembeschreibung:<br />
Ich versuche die Inhalte zweier 2dim-Int-Felder, die dyn. besorgt werden, zu tauschen. Dies funktioniert jedoch nicht.<br />
Der Tausch geschieht in der Funktion next_generation().<br />
Es gibt 4 Ausgaben der Felder: Vor und nach dem next_generation()-Aufruf und<br />
innerhalb der Funktion nochmal vor u. nach dem Zeigertausch.<br />
&quot;Innere&quot; Ausgaben zeigen, dass Felder getauscht sind. Verlässt er die Funktionen ist dies nicht mehr der Fall... warum... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> Bitte um Hilfe..<br />
Kompletter Code und Konsolenausgabe ist unten angehängt. Probiert ihn ruhig selbst aus falls nötig.<br />
**</p>
<p>Programmbeschreibung:<br />
+Zwei 2dim-Int-Felder werden durch new_int_matrix() dynamisch reserviert und mit 0 initialisiert.<br />
+Feld1 wird mit ein paar werten belegt.<br />
+Beide Felder werden vor next_generation()-Aufruf ausgegeben.<br />
+next_generation() wird Aufgerufen. Darin passiert folgendes:<br />
++Feld2 wird mit ein paar werten belegt.<br />
++Beide Felder werden ausgegeben.<br />
++Es erfolgt ein Zeigertausch mit Hilfe eines Hilfszeigers. (HIER FEHLER?)<br />
++Beide Felder werden ausgegeben.<br />
+Beide Felder werden nach next_generation()-Aufruf ausgegeben.<br />
+Die beiden 2dim-Int-Felder werden durch delete_int_matrix() wieder freigegeben.</p>
<p>HAUPTPROGRAMM:</p>
<pre><code>#include &lt;iostream&gt;     // Fuer Ausgaben
#include &lt;cstdlib&gt;      // Fuer rand() und system()
#include &lt;ctime&gt;        // Fuer srand
#include &lt;cstdio&gt;

using namespace std;

typedef unsigned int uint;

// HAUPTPROGRAMM
int main()
{
    int rows=10, columns=10;    // Zeilen u. Spalten der Matrix

    // Zwei Felder anlegen
    int** feld1=0;
    int** feld2=0;
    feld1 = new_int_matrix ( rows, columns );
    feld2 = new_int_matrix ( rows, columns );

    // FELD1-BELEGUNG
    feld1[5][5]=1;
    feld1[5][6]=1;
    feld1[6][5]=1;
    feld1[6][6]=1;

    system(&quot;cls&quot;);  // Konsole saubern

    // FELDERAUSGABE - Vor Funktionsaufruf next_generation()
    cout &lt;&lt; &quot;Felder vor Funktionsaufruf:&quot; &lt;&lt; endl;
    for(int i=0;i&lt;rows;i+=1) {
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    next_generation ( feld1, feld2, columns, rows );

    // FELDERAUSGABE - Nach Funktionsaufruf next_generation()
    cout &lt;&lt; &quot;Felder nach Funktionsaufruf:&quot; &lt;&lt; endl;
    for(int i=0;i&lt;rows;i+=1) {
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    // Beide Felder freigeben
    delete_int_matrix ( feld1 );
    delete_int_matrix ( feld2 );
    return 0;
}
</code></pre>
<p>FUNKTIONEN:<br />
Benutzte Funktionen:</p>
<pre><code>// === FUNCTION ======================================================================
// Name:        new_int_matrix
// Description: Reserviert dynamisch eine rows*columns-grosse intMatrix.
// Parameter:   rows ist die Zeilenanzahl der intMatrix
//              columns ist die Spaltenanzahl der intMatrix
// Rueckgabe:   Zeiger auf die intMatrix
// =====================================================================================
int** new_int_matrix ( int rows, int columns )
{
    int i;
    int **m;
    m = new int* [rows]; // Reserviert Zeigerfeld
    *m = new int [rows*columns](); // Reserviert u. Initialisiert Datenfeld
    for ( i=1; i&lt;rows; i+=1 ) // Setzt Zeiger des Zeigerfeldes
    m[i] = m[i-1] + columns;
    return m;
}
</code></pre>
<pre><code>// === FUNCTION ======================================================================
// Name:        delete_int_matrix
// Description: Gibt den Speicher fuer die int-Matrix hinter m frei.
// Parameter:   m ist der Zeiger auf intMatrix
// Rueckgabe:
// =====================================================================================
void delete_int_matrix ( int **m )
{
    delete[] *m; // delete data array
    delete[] m; // delete pointer array
}
</code></pre>
<pre><code>// === FUNCTION ======================================================================
// Name:        next_generation
// Description: Erzeugt Inhalte für zweites Feld und tauscht diese mit dem ersten Feld
// Parameter:   hoehe ist die Zeilenanzahl der intMatrix
//              breite ist die Spaltenanzahl der intMatrix
// Rueckgabe:   0
// =====================================================================================
uint next_generation ( int **feld1, int **feld2, uint breite, uint hoehe ){

    // FELD2-BELEGUNG
    feld2[1][1]=2;
    feld2[1][2]=2;
    feld2[2][1]=2;
    feld2[2][2]=2;

    // AUSGABE - Vor Zeigertausch
    cout &lt;&lt; &quot;(Innerhalb Funktion)Felder vor Zeigertausch:&quot; &lt;&lt; endl;
    for(uint i=0;i&lt;hoehe;i+=1) {
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    // Zeigertausch
    int** p_temp=0;
    p_temp=feld1;
    for(uint i=0;i&lt;hoehe+1;i+=1) p_temp[i]=feld1[i];
    feld1=feld2;
    for(uint i=0;i&lt;hoehe+1;i+=1) feld1[i]=feld2[i];
    feld2=p_temp;
    for(uint i=0;i&lt;hoehe+1;i+=1) feld2[i]=p_temp[i];

    // AUSGABE - Nach Zeigertausch
    cout &lt;&lt; &quot;(Innerhalb Funktion)Felder nach Zeigertausch:&quot; &lt;&lt; endl;
    for(uint i=0;i&lt;hoehe;i+=1) {
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>KONSOLENAUSGABE:</p>
<pre><code>Felder vor Funktionsaufruf:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

(Innerhalb Funktion)Felder vor Zeigertausch:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

(Innerhalb Funktion)Felder nach Zeigertausch:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

Felder nach Funktionsaufruf:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

Process returned 0 (0x0)   execution time : 0.106 s
Press any key to continue.
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/332018/tausch-zweier-zweidimensionaler-int-felder-durch-zeiger-klappt-nicht</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 14:42:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332018.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Apr 2015 12:12:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht on Fri, 03 Apr 2015 12:12:52 GMT]]></title><description><![CDATA[<p>Hallo Forum</p>
<p>**<br />
Problembeschreibung:<br />
Ich versuche die Inhalte zweier 2dim-Int-Felder, die dyn. besorgt werden, zu tauschen. Dies funktioniert jedoch nicht.<br />
Der Tausch geschieht in der Funktion next_generation().<br />
Es gibt 4 Ausgaben der Felder: Vor und nach dem next_generation()-Aufruf und<br />
innerhalb der Funktion nochmal vor u. nach dem Zeigertausch.<br />
&quot;Innere&quot; Ausgaben zeigen, dass Felder getauscht sind. Verlässt er die Funktionen ist dies nicht mehr der Fall... warum... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> Bitte um Hilfe..<br />
Kompletter Code und Konsolenausgabe ist unten angehängt. Probiert ihn ruhig selbst aus falls nötig.<br />
**</p>
<p>Programmbeschreibung:<br />
+Zwei 2dim-Int-Felder werden durch new_int_matrix() dynamisch reserviert und mit 0 initialisiert.<br />
+Feld1 wird mit ein paar werten belegt.<br />
+Beide Felder werden vor next_generation()-Aufruf ausgegeben.<br />
+next_generation() wird Aufgerufen. Darin passiert folgendes:<br />
++Feld2 wird mit ein paar werten belegt.<br />
++Beide Felder werden ausgegeben.<br />
++Es erfolgt ein Zeigertausch mit Hilfe eines Hilfszeigers. (HIER FEHLER?)<br />
++Beide Felder werden ausgegeben.<br />
+Beide Felder werden nach next_generation()-Aufruf ausgegeben.<br />
+Die beiden 2dim-Int-Felder werden durch delete_int_matrix() wieder freigegeben.</p>
<p>HAUPTPROGRAMM:</p>
<pre><code>#include &lt;iostream&gt;     // Fuer Ausgaben
#include &lt;cstdlib&gt;      // Fuer rand() und system()
#include &lt;ctime&gt;        // Fuer srand
#include &lt;cstdio&gt;

using namespace std;

typedef unsigned int uint;

// HAUPTPROGRAMM
int main()
{
    int rows=10, columns=10;    // Zeilen u. Spalten der Matrix

    // Zwei Felder anlegen
    int** feld1=0;
    int** feld2=0;
    feld1 = new_int_matrix ( rows, columns );
    feld2 = new_int_matrix ( rows, columns );

    // FELD1-BELEGUNG
    feld1[5][5]=1;
    feld1[5][6]=1;
    feld1[6][5]=1;
    feld1[6][6]=1;

    system(&quot;cls&quot;);  // Konsole saubern

    // FELDERAUSGABE - Vor Funktionsaufruf next_generation()
    cout &lt;&lt; &quot;Felder vor Funktionsaufruf:&quot; &lt;&lt; endl;
    for(int i=0;i&lt;rows;i+=1) {
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    next_generation ( feld1, feld2, columns, rows );

    // FELDERAUSGABE - Nach Funktionsaufruf next_generation()
    cout &lt;&lt; &quot;Felder nach Funktionsaufruf:&quot; &lt;&lt; endl;
    for(int i=0;i&lt;rows;i+=1) {
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(int j=0;j&lt;columns;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    // Beide Felder freigeben
    delete_int_matrix ( feld1 );
    delete_int_matrix ( feld2 );
    return 0;
}
</code></pre>
<p>FUNKTIONEN:<br />
Benutzte Funktionen:</p>
<pre><code>// === FUNCTION ======================================================================
// Name:        new_int_matrix
// Description: Reserviert dynamisch eine rows*columns-grosse intMatrix.
// Parameter:   rows ist die Zeilenanzahl der intMatrix
//              columns ist die Spaltenanzahl der intMatrix
// Rueckgabe:   Zeiger auf die intMatrix
// =====================================================================================
int** new_int_matrix ( int rows, int columns )
{
    int i;
    int **m;
    m = new int* [rows]; // Reserviert Zeigerfeld
    *m = new int [rows*columns](); // Reserviert u. Initialisiert Datenfeld
    for ( i=1; i&lt;rows; i+=1 ) // Setzt Zeiger des Zeigerfeldes
    m[i] = m[i-1] + columns;
    return m;
}
</code></pre>
<pre><code>// === FUNCTION ======================================================================
// Name:        delete_int_matrix
// Description: Gibt den Speicher fuer die int-Matrix hinter m frei.
// Parameter:   m ist der Zeiger auf intMatrix
// Rueckgabe:
// =====================================================================================
void delete_int_matrix ( int **m )
{
    delete[] *m; // delete data array
    delete[] m; // delete pointer array
}
</code></pre>
<pre><code>// === FUNCTION ======================================================================
// Name:        next_generation
// Description: Erzeugt Inhalte für zweites Feld und tauscht diese mit dem ersten Feld
// Parameter:   hoehe ist die Zeilenanzahl der intMatrix
//              breite ist die Spaltenanzahl der intMatrix
// Rueckgabe:   0
// =====================================================================================
uint next_generation ( int **feld1, int **feld2, uint breite, uint hoehe ){

    // FELD2-BELEGUNG
    feld2[1][1]=2;
    feld2[1][2]=2;
    feld2[2][1]=2;
    feld2[2][2]=2;

    // AUSGABE - Vor Zeigertausch
    cout &lt;&lt; &quot;(Innerhalb Funktion)Felder vor Zeigertausch:&quot; &lt;&lt; endl;
    for(uint i=0;i&lt;hoehe;i+=1) {
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    // Zeigertausch
    int** p_temp=0;
    p_temp=feld1;
    for(uint i=0;i&lt;hoehe+1;i+=1) p_temp[i]=feld1[i];
    feld1=feld2;
    for(uint i=0;i&lt;hoehe+1;i+=1) feld1[i]=feld2[i];
    feld2=p_temp;
    for(uint i=0;i&lt;hoehe+1;i+=1) feld2[i]=p_temp[i];

    // AUSGABE - Nach Zeigertausch
    cout &lt;&lt; &quot;(Innerhalb Funktion)Felder nach Zeigertausch:&quot; &lt;&lt; endl;
    for(uint i=0;i&lt;hoehe;i+=1) {
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>KONSOLENAUSGABE:</p>
<pre><code>Felder vor Funktionsaufruf:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

(Innerhalb Funktion)Felder vor Zeigertausch:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

(Innerhalb Funktion)Felder nach Zeigertausch:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

Felder nach Funktionsaufruf:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

Process returned 0 (0x0)   execution time : 0.106 s
Press any key to continue.
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2449014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2449014</guid><dc:creator><![CDATA[proxyghost]]></dc:creator><pubDate>Fri, 03 Apr 2015 12:12:52 GMT</pubDate></item><item><title><![CDATA[Reply to Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht on Fri, 03 Apr 2015 12:18:14 GMT]]></title><description><![CDATA[<p>Spricht was dagegen, C++ zu benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2449016</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2449016</guid><dc:creator><![CDATA[spechtr]]></dc:creator><pubDate>Fri, 03 Apr 2015 12:18:14 GMT</pubDate></item><item><title><![CDATA[Reply to Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht on Fri, 03 Apr 2015 14:13:10 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe nicht alles nachvollzogen aber der Tausch in next_generation funktioniert so nicht:</p>
<pre><code>uint next_generation ( int **feld1, int **feld2, uint breite, uint hoehe )
{
 //...
 p_temp=feld1;
 //...
 feld1=feld2;
 //...
 feld2=p_temp;
}
</code></pre>
<p>Du übergibst feld1 und feld2 per Value. D.h. die Doppelzeiger feld1 und feld2 in &quot;next_generation&quot; sind ein Kopie von den Zeigern feld1 und feld2 in Deiner &quot;main&quot;-Funktion (nur die Zeiger sind eine Kopie, nicht das worauf sie zeigen.) Wegen der Kopie ist dieser Tausch nur innerhalb der Funktion next_generation sichtbar. Du musst also das, worauf feld1 und feld2 zeigen vertauschen, und das versuchst Du auch:</p>
<pre><code>uint next_generation ( int **feld1, int **feld2, uint breite, uint hoehe )
{
 //...
for(uint i=0;i&lt;hoehe+1;i+=1) [i]=feld1[i];
 //..
for(uint i=0;i&lt;hoehe+1;i+=1) feld1[i]=feld2[i];
 //..
for(uint i=0;i&lt;hoehe+1;i+=1) feld2[i]=p_temp[i];
}
</code></pre>
<p>Das sieht zwar umständlich aus, könnte aber funktionieren, wenn p_temp auf freien gültigen Speicher zeigt. In der Kombination, wie du es geschrieben hast, hat der 2. Teil keine Wirkung denn:</p>
<pre><code>p_temp=feld1;
for(uint i=0;i&lt;hoehe+1;i+=1) p_temp[i]=feld1[i];
</code></pre>
<p>p_temp ist in der Schleife schon gleich feld1. Also ist temp[i] sowieso schon gleich feld[i]. Genau so ist es auch bei den nächsten beiden Tauschversuchen.</p>
<p>EDIT: &quot;Wegen der Kopie ist dieser Tausch nur innerhalb der Funktion next_generation sichtbar.&quot; hinzugefügt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2449020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2449020</guid><dc:creator><![CDATA[DJohn]]></dc:creator><pubDate>Fri, 03 Apr 2015 14:13:10 GMT</pubDate></item><item><title><![CDATA[Reply to Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht on Sat, 04 Apr 2015 13:28:34 GMT]]></title><description><![CDATA[<p>Danke für den Hinweis mit den Zeiger-Kopien DJohn.<br />
Ich habe mir die beiden Zeiger, die ich übergebe, vorher ausgeben lassen:</p>
<pre><code>&amp;feld1: 0x28fedc
&amp;feld2: 0x28fed8
feld1:  0x3f0ff0
feld2:  0x3f1020
</code></pre>
<p>Und habe mir diese nochmal innerhalb der Funktion ausgeben lassen:</p>
<pre><code>&amp;feld1: 0x28fe70
&amp;feld2: 0x28fe74
feld1:  0x3f0ff0
feld2:  0x3f1020
</code></pre>
<p>Die Adressen sind unterschiedlich. Also Kopien wie du geschrieben hast.</p>
<p>Den Code mit dem Zeigertausch:</p>
<pre><code>// Zeigertausch
    int** p_temp=0;
    p_temp=feld1;
    for(uint i=0;i&lt;hoehe+1;i+=1) p_temp[i]=feld1[i];
    feld1=feld2;
    for(uint i=0;i&lt;hoehe+1;i+=1) feld1[i]=feld2[i];
    feld2=p_temp;
    for(uint i=0;i&lt;hoehe+1;i+=1) feld2[i]=p_temp[i];
</code></pre>
<p>habe ich nun geändert in:</p>
<pre><code>// Zeigertausch
    int* p_temp=0;
    for(uint i=0;i&lt;hoehe;i+=1) {
        p_temp     =   feld1[i];
        feld1[i] =   feld2[i];
        feld2[i] =   p_temp;
    }
</code></pre>
<p>Er hatte tatsächlich, wegen den Zeigerkopien, nicht richtig getauscht und war auch sonst umständlich geschrieben. Das war der entscheidende Tipp. Danke nochmal.</p>
<p>Die überarbeitete FUNKTION next_generation():</p>
<pre><code>// === FUNCTION ======================================================================
// Name:        next_generation
// Description: Erzeugt Inhalte für zweites Feld und tauscht diese mit dem ersten Feld
// Parameter:   hoehe ist die Zeilenanzahl der intMatrix
//              breite ist die Spaltenanzahl der intMatrix
// Rueckgabe:   0
// =====================================================================================
uint next_generation ( int **feld1, int **feld2, uint breite, uint hoehe ){

    // FELD2-BELEGUNG
    feld2[1][1]=2;
    feld2[1][2]=2;
    feld2[2][1]=2;
    feld2[2][2]=2;

    // AUSGABE - Vor Zeigertausch
    cout &lt;&lt; &quot;(Innerhalb Funktion)Felder vor Zeigertausch:&quot; &lt;&lt; endl;
    for(uint i=0;i&lt;hoehe;i+=1) {
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    // Zeigertausch
    int* p_temp=0;
    for(uint i=0;i&lt;hoehe;i+=1) {
        p_temp     =   feld1[i];
        feld1[i] =   feld2[i];
        feld2[i] =   p_temp;
    }

    // AUSGABE - Nach Zeigertausch
    cout &lt;&lt; &quot;(Innerhalb Funktion)Felder nach Zeigertausch:&quot; &lt;&lt; endl;
    for(uint i=0;i&lt;hoehe;i+=1) {
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld1[i][j];
        cout &lt;&lt; &quot;  |  &quot;;
        for(uint j=0;j&lt;breite;j+=1) cout &lt;&lt; &quot; &quot; &lt;&lt; feld2[i][j];
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; endl;

    return 0;
}
</code></pre>
<p>und die nun korrekte KONSOLENAUSGABE:</p>
<pre><code>Felder vor Funktionsaufruf:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

(Innerhalb Funktion)Felder vor Zeigertausch:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 2 2 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 1 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

(Innerhalb Funktion)Felder nach Zeigertausch:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

Felder nach Funktionsaufruf:
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 2 2 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 1 1 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0  |   0 0 0 0 0 0 0 0 0 0

Process returned 0 (0x0)   execution time : 0.109 s
Press any key to continue.
</code></pre>
<p>Den ersten Kommentar von spechtr habe ich nicht verstanden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2449125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2449125</guid><dc:creator><![CDATA[proxyghost]]></dc:creator><pubDate>Sat, 04 Apr 2015 13:28:34 GMT</pubDate></item><item><title><![CDATA[Reply to Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht on Sat, 04 Apr 2015 14:55:08 GMT]]></title><description><![CDATA[<p>Schön dass es jetzt für Dich funktioniert. Die Frage ist halt, warum nutzt Du nicht die Möglichkeiten von C++? Stattdessen, sieht Dein Code so aus, als würdest Du C programmieren und nur printf durch cout und malloc durch new ersetzten. Rohe Zeiger brauchst Du in C++ nur ganz selten. Hier könntest Du stattdessen z.B. std::vector oder std::array verwenden. Für das Tauschen von 2 Variablen gibt es die fertige Funktion std::swap. Die macht auch nichts anderes als:</p>
<pre><code>temp=var1;
 var1=var2;
 var2=temp;
</code></pre>
<p>nur das im Gegensatz zu Deinem Versuch die Parameter hier per Referenz übergeben werden, so dass der Tausch auch funktioniert. Dein int** ist eigenlich ein neuer Typ für eine 2-Dim-Matrix. Dazu hast Du dann die Funktionen new_int_matrix (Konstruktor) und delete_int_matrix (Destruktor). Das könnte man dann auch gleich in eine eigene Klasse packen.</p>
<p>EDIT: Rechtschreibung</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2449132</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2449132</guid><dc:creator><![CDATA[DJohn]]></dc:creator><pubDate>Sat, 04 Apr 2015 14:55:08 GMT</pubDate></item><item><title><![CDATA[Reply to Tausch zweier zweidimensionaler Int-Felder durch Zeiger klappt nicht on Tue, 07 Apr 2015 17:34:40 GMT]]></title><description><![CDATA[<p>Hi nochmal</p>
<p>Stimmt alles was du sagst. Warum nicht die Werkezuge nutzen die C++ bietet.</p>
<p>Das ist eine Aufgabe aus dem Studium gewesen.<br />
Die beiden Funktionen new_int_matrix und delete_int_matrix waren vorgegeben. Aber gute Idee mit der Klasse!<br />
Ich vermute weil der Dozent uns den Umgang mit Zeigern näher bringen will, gibt er uns Übungen, wo wir ein wenig mit rohen Zeigern arbeiten sollen.<br />
Später in der Praxis werde ich definitiv das verwenden was mir C++ bietet.<br />
Aber erstmal versuche ich zu verstehen.</p>
<p>Danke für die guten Tipps und Infos nochmal! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2449411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2449411</guid><dc:creator><![CDATA[proxyghost]]></dc:creator><pubDate>Tue, 07 Apr 2015 17:34:40 GMT</pubDate></item></channel></rss>