<?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[Zeiger Problem]]></title><description><![CDATA[<p>Hallo wie kann ich im Hauptprogramm ausgeben lassen was die Funktion Name zurück gibt?</p>
<p>Hier ist mein Ansatz:<br />
( Ich bekomme immer nur das a ausgeben und danach nur noch irgendwelche ascii Zeichen. )</p>
<pre><code class="language-cpp">#include &lt;iostream.h&gt;
#include &lt;windows.h&gt;

using namespace std;

char* name();

int main()
{

char *c=NULL;

c = name () ;

        cout&lt;&lt; c[0]&lt;&lt;endl;
        cout&lt;&lt; c[1]&lt;&lt;endl;
        cout&lt;&lt; c[2]&lt;&lt;endl;

system(&quot;PAUSE&quot;);
}
//---------------------------------------------------------------------------

char* name()
{

char data[10];

        data[0]='a';
        data[1]='b';
        data[2]='c';

return data;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/272373/zeiger-problem</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 22:24:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/272373.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 Aug 2010 14:26:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 14:26:20 GMT]]></title><description><![CDATA[<p>Hallo wie kann ich im Hauptprogramm ausgeben lassen was die Funktion Name zurück gibt?</p>
<p>Hier ist mein Ansatz:<br />
( Ich bekomme immer nur das a ausgeben und danach nur noch irgendwelche ascii Zeichen. )</p>
<pre><code class="language-cpp">#include &lt;iostream.h&gt;
#include &lt;windows.h&gt;

using namespace std;

char* name();

int main()
{

char *c=NULL;

c = name () ;

        cout&lt;&lt; c[0]&lt;&lt;endl;
        cout&lt;&lt; c[1]&lt;&lt;endl;
        cout&lt;&lt; c[2]&lt;&lt;endl;

system(&quot;PAUSE&quot;);
}
//---------------------------------------------------------------------------

char* name()
{

char data[10];

        data[0]='a';
        data[1]='b';
        data[2]='c';

return data;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1940628</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940628</guid><dc:creator><![CDATA[bob*]]></dc:creator><pubDate>Mon, 16 Aug 2010 14:26:20 GMT</pubDate></item><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 14:44:02 GMT]]></title><description><![CDATA[<p>In der Funktion angelegte Variabeln sind ausserhalb nicht gültig, du gibst also einen Pointer zurück der ins Nirwana zeigt .. und nimm strings statt array of char <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="🙂"
    /> strings sind nutzerfreundlicher.</p>
<p>machs zB so:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; // keine .h !!
#include &lt;windows.h&gt;
#include &lt;string&gt;

std::string name()
{
    std::string name = &quot;abc&quot;;
    return name;
} 

void  name1(char * preallokiertesArrayGenuegenderGroesse)
{
    preallokiertesArrayGenuegenderGroesse = name().c_str();
} 

int main()
{
    std::cout &lt;&lt; name();

    char * prealloc =  new char[512];
    name1(prealloc);

    for(int i=0;i&lt;3;++i) std::cout &lt;&lt; i &lt;&lt; &quot;:&quot; &lt;&lt; prealloc[i] &lt;&lt; &quot;  &quot;;

    delete [] prealloc;

    system(&quot;PAUSE&quot;);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1940633</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940633</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Mon, 16 Aug 2010 14:44:02 GMT</pubDate></item><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 14:38:14 GMT]]></title><description><![CDATA[<p>Verwende lieber die STL-Container (hier bieten sich std::vector&lt;char&gt; oder std::string an). Diese Verhalten sich nämlich bezüglich Kopien(bei der Übergabe) genauso wie du es von anderen Objekttypen gewohnt bist.</p>
<p>Arrays verhalten sich anders als alles andere in C++ und du machst hier auch prompt alles falsch, was man falsch machen könnte.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;  // Das heißt iostream!
#include &lt;windows.h&gt;      // Wozu?
#include &lt;cstdlib&gt;        // Für system
#include &lt;vector&gt;         // Für vector

using namespace std;

vector&lt;char&gt; name();   // Ich ändere nur den Typen...

int main()
{

vector&lt;char&gt; c = name () ;  // ...überall wo er vorkommt.

        cout&lt;&lt; c[0]&lt;&lt;endl;
        cout&lt;&lt; c[1]&lt;&lt;endl;
        cout&lt;&lt; c[2]&lt;&lt;endl;

system(&quot;PAUSE&quot;);  
}
//---------------------------------------------------------------------------

vector&lt;char&gt; name()
{

vector&lt;char&gt; data(10); // Keine eckigen Klammern hier, sonst hätte man ein Array von vectoren

        data[0]='a';
        data[1]='b';
        data[2]='c';

return data;  // Und schon klappt alles
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1940635</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940635</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 16 Aug 2010 14:38:14 GMT</pubDate></item><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 14:43:58 GMT]]></title><description><![CDATA[<p>Ty für die antworten.</p>
<p>Aber wieso soll man nicht #include &lt;iostream.h&gt; benutzen ?</p>
<p>( #include &lt;windows.h&gt; war noch von vorhin drin hab da was getestet ... )</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1940637</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940637</guid><dc:creator><![CDATA[bob*]]></dc:creator><pubDate>Mon, 16 Aug 2010 14:43:58 GMT</pubDate></item><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 14:44:38 GMT]]></title><description><![CDATA[<p>padreigh schrieb:</p>
<blockquote>
<pre><code class="language-cpp">void  name1(char * preallokiertesArrayGenuegenderGroesse)
{
    preallokiertesArrayGenuegenderGroesse = name().c_str();
}
</code></pre>
</blockquote>
<p>Und das soll jetzt was machen?</p>
<p>Lars</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1940638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940638</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 16 Aug 2010 14:44:38 GMT</pubDate></item><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 14:53:25 GMT]]></title><description><![CDATA[<p>bob* schrieb:</p>
<blockquote>
<p>Aber wieso soll man nicht #include &lt;iostream.h&gt; benutzen ?</p>
</blockquote>
<p>Weils das schon lange nicht mehr gibt, allerhöchstens noch aus Gründen der Abwärtskompatibilität. Wo auch immer du dies her hast: Deine Quelle ist seit über 12 Jahren veraltet, mindestens von der Standardkonformität her und höchstwahrscheinlich auch vom Stil. Diese Quelle sollte in Zukunft von die nicht mehr benutzt werden.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23744">@padreigh</a>: Du hattest doch vorhin noch so ein wunderhübsches Beispiel wie das mit string ein ganz sauberer Zweizeiler ist. Warum hast du das weggemacht? Weil's so schön war, schreibe ich es nochmal hin:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

string name()
{
 return &quot;abc&quot;;
}

int main()
{
  cout &lt;&lt; name() &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1940639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940639</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 16 Aug 2010 14:53:25 GMT</pubDate></item><item><title><![CDATA[Reply to Zeiger Problem on Mon, 16 Aug 2010 15:14:36 GMT]]></title><description><![CDATA[<p>manni66 schrieb:</p>
<blockquote>
<p>Und das soll jetzt was machen?</p>
</blockquote>
<p>Hubs hast recht :)) das tut so nich</p>
<pre><code class="language-cpp">std::string name()
{
    std::string name = &quot;abc&quot;;
    return name;
} 

// für den Fall das du wirklich char* aus irgend einem Grund brauchst, kannst du einfach
// einen genügend großen char * &quot;reservieren&quot; und den start davon der Methode mitgeben.
// die kann dann direkt in den Speicherbereich reinschreiben .... IMHO: nimm string, iss einfacher ;)
void  name1(char * preallokiertesArrayGenuegenderGroesse)
{
    preallokiertesArrayGenuegenderGroesse [0] = 'a';
    preallokiertesArrayGenuegenderGroesse [1] = 'b';
    preallokiertesArrayGenuegenderGroesse [2] = 'c';
}
</code></pre>
<p>SeppJ schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/23744">@padreigh</a>: Du hattest doch vorhin noch so ein wunderhübsches Beispiel wie das mit string ein ganz sauberer Zweizeiler ist. Warum hast du das weggemacht? Weil's so schön war, schreibe ich es nochmal hin:</p>
</blockquote>
<p>Ist noch da, 2 Zeilen höher <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="😉"
    /> das ist nur so kurz, das geht unter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1940644</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1940644</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Mon, 16 Aug 2010 15:14:36 GMT</pubDate></item></channel></rss>