<?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[Liste, Pointer problem]]></title><description><![CDATA[<p>Hallo,</p>
<p>hab ein Pointerproblem <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Ich wollte selbst eine Liste schreiben, die jeweils das nächste Element beinhaltet und halt einen Integer Wert.</p>
<p>Im Hauptprogramm sollte es 2 Zeiger geben, die auf das erste und letzte element zeigen. Der Zeiger für das letzte Element soll halt ermöglichen, dass ich schnell elemente einfügen kann.</p>
<p>Also ich hab mein erstes Element, und füge dann 5 neue Elemente ans ende. Der Pointer letztes wird immer upgedated, dass halt das nächste Element ganz hinten dran kommt.</p>
<p>Dann will ich die Werte der Listenelemente ausgeben. Bekomm aber nicht das gewünschte ergebnis.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

typedef struct mystr mystr;  
struct mystr
{
	mystr* next;		// next element
	int a;			// value
};

int main() {

	mystr* erstes;
	mystr* letztes;

	mystr a;
	a.a = 0;

	erstes = &amp;a;
	letztes = &amp;a;

	for(int i=0; i&lt;5; i++)
	{
		mystr c;
		c.a = i;
		letztes-&gt;next = &amp;c;
		letztes = &amp;c;
	}

	for(int i=0; i&lt;5; i++)
	{
		printf(&quot;%d\n&quot;, erstes-&gt;a);
		erstes = erstes-&gt;next;
	}

    return 0;
}
</code></pre>
<p>meine ausgabe:</p>
<blockquote>
<p>0<br />
4<br />
4<br />
4<br />
4</p>
</blockquote>
<p>sieht so aus als wird da im kreis gezeigert. Hat jmd. ne idee wie ich das richtig hin bieg?</p>
<p>gruß<br />
eurohasi</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/224570/liste-pointer-problem</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 08:46:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224570.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Oct 2008 18:07:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:07:58 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>hab ein Pointerproblem <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Ich wollte selbst eine Liste schreiben, die jeweils das nächste Element beinhaltet und halt einen Integer Wert.</p>
<p>Im Hauptprogramm sollte es 2 Zeiger geben, die auf das erste und letzte element zeigen. Der Zeiger für das letzte Element soll halt ermöglichen, dass ich schnell elemente einfügen kann.</p>
<p>Also ich hab mein erstes Element, und füge dann 5 neue Elemente ans ende. Der Pointer letztes wird immer upgedated, dass halt das nächste Element ganz hinten dran kommt.</p>
<p>Dann will ich die Werte der Listenelemente ausgeben. Bekomm aber nicht das gewünschte ergebnis.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

typedef struct mystr mystr;  
struct mystr
{
	mystr* next;		// next element
	int a;			// value
};

int main() {

	mystr* erstes;
	mystr* letztes;

	mystr a;
	a.a = 0;

	erstes = &amp;a;
	letztes = &amp;a;

	for(int i=0; i&lt;5; i++)
	{
		mystr c;
		c.a = i;
		letztes-&gt;next = &amp;c;
		letztes = &amp;c;
	}

	for(int i=0; i&lt;5; i++)
	{
		printf(&quot;%d\n&quot;, erstes-&gt;a);
		erstes = erstes-&gt;next;
	}

    return 0;
}
</code></pre>
<p>meine ausgabe:</p>
<blockquote>
<p>0<br />
4<br />
4<br />
4<br />
4</p>
</blockquote>
<p>sieht so aus als wird da im kreis gezeigert. Hat jmd. ne idee wie ich das richtig hin bieg?</p>
<p>gruß<br />
eurohasi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597525</guid><dc:creator><![CDATA[eurohasi]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:07:58 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:27:00 GMT]]></title><description><![CDATA[<p>Könntest du deinen code mal ein bisschen kommentieren ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597537</guid><dc:creator><![CDATA[gary1195]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:27:00 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:28:59 GMT]]></title><description><![CDATA[<p>du kommst wohl aus C:</p>
<p>Das typedef ist absolut überflüssig. Setz dich mit Klassen auseinander!!! Das was in deinem main steht übernehmen Member-Funktionen. Tutorials gibts doch genügend</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597538</guid><dc:creator><![CDATA[Die3?]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:28:59 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:33:42 GMT]]></title><description><![CDATA[<p>hm, ist er nicht verständlich?</p>
<p>okay ich versuchs:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

typedef struct mystr mystr;  
struct mystr
{
    mystr* next;        // next element
    int a;            // value
};

int main() {

    mystr* erstes;     // points at first element
    mystr* letztes;    // points at last element

    mystr a;            // first element
    a.a = 0;            // gets some value

    erstes = &amp;a;        // erstes points at a
    letztes = &amp;a;       // letztes points at a

    for(int i=0; i&lt;5; i++) // do 5 times
    {
        mystr c;         // create a new c
        c.a = i;         // give it some value
        letztes-&gt;next = &amp;c; // set the pointer in the element onto the created element
        letztes = &amp;c; // let letztes point at just created element
    }

    for(int i=0; i&lt;5; i++) // do 5 times
    {
        printf(&quot;%d\n&quot;, erstes-&gt;a); // print value of the element, erstes points at
        erstes = erstes-&gt;next; // erstes points at the next element
    }

    return 0;  // end
}
</code></pre>
<p>Die3? schrieb:</p>
<blockquote>
<p>du kommst wohl aus C:</p>
<p>Das typedef ist absolut überflüssig. Setz dich mit Klassen auseinander!!! Das was in deinem main steht übernehmen Member-Funktionen. Tutorials gibts doch genügend</p>
</blockquote>
<p>mir ist klar dass ich einfach ne Liste aus der STL verwenden könnte, bin vor kurzem von java nach c++ gewechselt, und wollte das pointer zeugs verstehn. Darum ist es mir wichtig zu verstehen warum das beispiel nicht funktioniert.</p>
<p>gruß<br />
eurohasi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597542</guid><dc:creator><![CDATA[eurohasi]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:33:42 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:37:41 GMT]]></title><description><![CDATA[<p>Kommentare auf Denglish das is ja richtig Geil</p>
<p>Was is eigentlich dieses</p>
<pre><code class="language-cpp">erstes-&gt;a
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1597550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597550</guid><dc:creator><![CDATA[gary1195]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:37:41 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:38:57 GMT]]></title><description><![CDATA[<p>naja, erstes zeigt doch aufn element, also is erstes-&gt;a, der wert a von dem element, !?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597551</guid><dc:creator><![CDATA[eurohasi]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:38:57 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:43:06 GMT]]></title><description><![CDATA[<p>Deklaration:</p>
<pre><code class="language-cpp">int *ptr;
</code></pre>
<p>Initialisierung</p>
<pre><code class="language-cpp">ptr = &amp;var;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1597553</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597553</guid><dc:creator><![CDATA[gary1195]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:43:06 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:45:52 GMT]]></title><description><![CDATA[<p>Das in Zeile 25 erzeugte Objekt wird am Ende des Blocks (Zeile 29) automatisch zerstört und freigegeben - jeder Zeiger, der auf dieses Objekt zeigte, ist damit automatisch ungültig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597556</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597556</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:45:52 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:51:15 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Das in Zeile 25 erzeugte Objekt wird am Ende des Blocks (Zeile 29) automatisch zerstört und freigegeben - jeder Zeiger, der auf dieses Objekt zeigte, ist damit automatisch ungültig.</p>
</blockquote>
<p>okay, ich verstehe, klar^^.</p>
<p>wie kann ich das umgehn?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597558</guid><dc:creator><![CDATA[eurohasi]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:51:15 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:53:35 GMT]]></title><description><![CDATA[<p>Erzeuge das Objekt außerhalb des Blocks zB oben mit a</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597561</guid><dc:creator><![CDATA[gary1195]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:53:35 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:56:21 GMT]]></title><description><![CDATA[<p>okay, habs geschafft, danke, es war wirklich der fehler dass das object c ja nur daseinsberechtigung hat in seinem Block,</p>
<p>hab das problem mal so gelöst. Es leistet das gewünschte.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

class myclass
{
public:
	myclass* next;		// next element
	int a;			// value

	myclass(int val)
	{
		a = val;
	}

	void add(int val)
	{
		next = new myclass(val);
	}
};

int main() {

	myclass *erstes;
	myclass *letztes;

	myclass* a = new myclass(0);	

	erstes = a;
	letztes = a;

	for(int i=0; i&lt;5; i++)
	{
		letztes-&gt;add(i);
		letztes = letztes-&gt;next;
	}

	for(int i=0; i&lt;5; i++)
	{
		printf(&quot;%d\n&quot;, erstes-&gt;a);
		erstes = erstes-&gt;next;
	}

	int lo;
	cin &gt;&gt; lo;

    return 0;
}
</code></pre>
<p>danke gary für die hilfe <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>gruß<br />
eurohasi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597565</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597565</guid><dc:creator><![CDATA[eurohasi]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:56:21 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 18:59:18 GMT]]></title><description><![CDATA[<p>ich würd aber noch im ctor den &quot;next&quot; zeiger auf null setzen, um das ende der liste zu markieren.</p>
<p>da wir die länge der liste meist nicht vorher kennen (sonst würde man ein array verwenden) kann man die null als abbruchbedingung benutzen</p>
<pre><code class="language-cpp">while (erstes)
{
    printf(&quot;%d\n&quot;, erstes-&gt;a);
    erstes = erstes-&gt;next;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1597566</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597566</guid><dc:creator><![CDATA[helferlein]]></dc:creator><pubDate>Sat, 11 Oct 2008 18:59:18 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 19:13:05 GMT]]></title><description><![CDATA[<p>anstatt</p>
<pre><code class="language-cpp">int lo;
cin &gt;&gt; lo;
</code></pre>
<p>kannst du</p>
<pre><code class="language-cpp">fflush(stdin);
getchar();
</code></pre>
<p>verwenden</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597571</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597571</guid><dc:creator><![CDATA[gary1195]]></dc:creator><pubDate>Sat, 11 Oct 2008 19:13:05 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 19:10:08 GMT]]></title><description><![CDATA[<p>gary1195 schrieb:</p>
<blockquote>
<p>anstatt</p>
<pre><code class="language-cpp">int lo;
cin &gt;&gt; lo;
</code></pre>
<p>kannst du besser</p>
<pre><code class="language-cpp">fflush(stdin);
getchar();
</code></pre>
<p>verwenden</p>
</blockquote>
<p>LOL</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597574</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597574</guid><dc:creator><![CDATA[Don06]]></dc:creator><pubDate>Sat, 11 Oct 2008 19:10:08 GMT</pubDate></item><item><title><![CDATA[Reply to Liste, Pointer problem on Sat, 11 Oct 2008 19:35:44 GMT]]></title><description><![CDATA[<p><strong>@ gary1195:</strong><br />
Wir sind immer noch im <strong>C++</strong>-Forum. Von daher wäre es vielleicht vorteilhaft, den Threadersteller nicht zusätzlich zu verunsichern... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /><br />
(Im Übrigen hat auch <code>printf()</code> hier nichts zu suchen.)</p>
<p><strong>@ eurohasi:</strong><br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-124532.html" rel="nofollow">Hier</a> gibts einen Artikel über Zeiger. Es lohnt sich sicher, den durchzulesen, Zeiger werden dort sehr gut erläutert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1597585</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1597585</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 11 Oct 2008 19:35:44 GMT</pubDate></item></channel></rss>