<?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[Problem mit einem char*]]></title><description><![CDATA[<p>Hi Leute,</p>
<p>ich hab mal eine Funktion geschrieben:</p>
<pre><code class="language-cpp">char* CMySQL::MySQLQuery(int clause, char* column, char* from, char* table,
                         char* barrier, char* rows, char* values)
{
    // deklarieren der benötigten Variablen
    char	     *sql[9], *query;
    unsigned int i, size = 0;

    // Freimachen des Arrays
    for(i = 0; i &lt; 9; i++)
        sql[i] = &quot;&quot;;

    if(clause == 1)
    {
        // Speicher reservieren
	    sql[0] = static_cast&lt;char*&gt;(malloc(strlen(&quot;SELECT &quot;) + 1));
        sql[1] = static_cast&lt;char*&gt;(malloc(strlen(column) + 1));
        sql[2] = static_cast&lt;char*&gt;(malloc(strlen(&quot; FROM &quot;) + 1));
        sql[3] = static_cast&lt;char*&gt;(malloc(strlen(from) + 1));
        sql[4] = static_cast&lt;char*&gt;(malloc(strlen(&quot; WHERE &quot;) + 1));
        sql[5] = static_cast&lt;char*&gt;(malloc(strlen(table) + 1));
        sql[6] = static_cast&lt;char*&gt;(malloc(strlen(&quot; = '&quot;) + 1));
        sql[7] = static_cast&lt;char*&gt;(malloc(strlen(barrier) + 1));
        sql[8] = static_cast&lt;char*&gt;(malloc(strlen(&quot;'&quot;) + 1));

        // Strings kopieren
        strcpy(sql[0], &quot;SELECT &quot;);
        strcpy(sql[1], column);
        strcpy(sql[2], &quot; FROM &quot;);
        strcpy(sql[3], from);
        strcpy(sql[4], &quot; WHERE &quot;);
        strcpy(sql[5], table);
        strcpy(sql[6], &quot; = '&quot;);
        strcpy(sql[7], barrier);
        strcpy(sql[8], &quot;'&quot;);
    }
    else
    {
        // Speicher reservieren
		sql[0] = static_cast&lt;char*&gt;(malloc(strlen(&quot;INSERT INTO &quot;) + 1));
		sql[1] = static_cast&lt;char*&gt;(malloc(strlen(table) + 1));
        sql[2] = static_cast&lt;char*&gt;(malloc(strlen(&quot; (&quot;) + 1));
        sql[3] = static_cast&lt;char*&gt;(malloc(strlen(rows) + 1));
        sql[4] = static_cast&lt;char*&gt;(malloc(strlen(&quot;) VALUES (&quot;) + 1));
		sql[5] = static_cast&lt;char*&gt;(malloc(strlen(values) + 1));
		sql[6] = static_cast&lt;char*&gt;(malloc(strlen(&quot;)&quot;) + 1));
		sql[7] = static_cast&lt;char*&gt;(malloc(strlen(&quot;&quot;) + 1));
		sql[8] = static_cast&lt;char*&gt;(malloc(strlen(&quot;&quot;) + 1));

        // Strings kopieren
        strcpy(sql[0], &quot;INSERT INTO &quot;);
        strcpy(sql[1], table);
        strcpy(sql[2], &quot; (&quot;);
        strcpy(sql[3], rows);
        strcpy(sql[4], &quot;) VALUES (&quot;);
        strcpy(sql[5], values);
        strcpy(sql[6], &quot;)&quot;);
        strcpy(sql[7], &quot;&quot;);
        strcpy(sql[8], &quot;&quot;);
    }

    // Größe der einzelnen String berechnen
    for (i = 0; i &lt; 9; i++)
        size += strlen(sql[i]);

    // Speicherplatz für den Anfragestring reservieren
    query = static_cast&lt;char*&gt;(malloc(size + 1));

    // alles zusammensetzen
    for(i = 0; i &lt; 9; i++)
        strcat(query, sql[i]);

    return query;
}
</code></pre>
<p>Ich übergebe der Funktion die Werte 1, &quot;pass&quot;, &quot;user&quot;, &quot;nick&quot;, &quot;Apocalypse&quot;, NULL, NULL. Wenn man ich den Quellcode durchgehe, würde ich auf folgendes Ergebnis kommen: <strong>SELECT pass FROM user WHERE nick = 'Apocalypse'</strong></p>
<p>Die Funktion kommt im großen und ganzen auch darauf, aber vor SELECT stehen noch drei komische Zeichen und somit wird meine SQL-Anfrage falsch. Woran kann das liegen, dass da noch drei Zeichen vorher angehängt werden? Ich danke euch schonmal...</p>
<p>MfG Apocalypse</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/158931/problem-mit-einem-char</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 07:47:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/158931.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Sep 2006 10:48:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit einem char* on Sun, 10 Sep 2006 10:48:09 GMT]]></title><description><![CDATA[<p>Hi Leute,</p>
<p>ich hab mal eine Funktion geschrieben:</p>
<pre><code class="language-cpp">char* CMySQL::MySQLQuery(int clause, char* column, char* from, char* table,
                         char* barrier, char* rows, char* values)
{
    // deklarieren der benötigten Variablen
    char	     *sql[9], *query;
    unsigned int i, size = 0;

    // Freimachen des Arrays
    for(i = 0; i &lt; 9; i++)
        sql[i] = &quot;&quot;;

    if(clause == 1)
    {
        // Speicher reservieren
	    sql[0] = static_cast&lt;char*&gt;(malloc(strlen(&quot;SELECT &quot;) + 1));
        sql[1] = static_cast&lt;char*&gt;(malloc(strlen(column) + 1));
        sql[2] = static_cast&lt;char*&gt;(malloc(strlen(&quot; FROM &quot;) + 1));
        sql[3] = static_cast&lt;char*&gt;(malloc(strlen(from) + 1));
        sql[4] = static_cast&lt;char*&gt;(malloc(strlen(&quot; WHERE &quot;) + 1));
        sql[5] = static_cast&lt;char*&gt;(malloc(strlen(table) + 1));
        sql[6] = static_cast&lt;char*&gt;(malloc(strlen(&quot; = '&quot;) + 1));
        sql[7] = static_cast&lt;char*&gt;(malloc(strlen(barrier) + 1));
        sql[8] = static_cast&lt;char*&gt;(malloc(strlen(&quot;'&quot;) + 1));

        // Strings kopieren
        strcpy(sql[0], &quot;SELECT &quot;);
        strcpy(sql[1], column);
        strcpy(sql[2], &quot; FROM &quot;);
        strcpy(sql[3], from);
        strcpy(sql[4], &quot; WHERE &quot;);
        strcpy(sql[5], table);
        strcpy(sql[6], &quot; = '&quot;);
        strcpy(sql[7], barrier);
        strcpy(sql[8], &quot;'&quot;);
    }
    else
    {
        // Speicher reservieren
		sql[0] = static_cast&lt;char*&gt;(malloc(strlen(&quot;INSERT INTO &quot;) + 1));
		sql[1] = static_cast&lt;char*&gt;(malloc(strlen(table) + 1));
        sql[2] = static_cast&lt;char*&gt;(malloc(strlen(&quot; (&quot;) + 1));
        sql[3] = static_cast&lt;char*&gt;(malloc(strlen(rows) + 1));
        sql[4] = static_cast&lt;char*&gt;(malloc(strlen(&quot;) VALUES (&quot;) + 1));
		sql[5] = static_cast&lt;char*&gt;(malloc(strlen(values) + 1));
		sql[6] = static_cast&lt;char*&gt;(malloc(strlen(&quot;)&quot;) + 1));
		sql[7] = static_cast&lt;char*&gt;(malloc(strlen(&quot;&quot;) + 1));
		sql[8] = static_cast&lt;char*&gt;(malloc(strlen(&quot;&quot;) + 1));

        // Strings kopieren
        strcpy(sql[0], &quot;INSERT INTO &quot;);
        strcpy(sql[1], table);
        strcpy(sql[2], &quot; (&quot;);
        strcpy(sql[3], rows);
        strcpy(sql[4], &quot;) VALUES (&quot;);
        strcpy(sql[5], values);
        strcpy(sql[6], &quot;)&quot;);
        strcpy(sql[7], &quot;&quot;);
        strcpy(sql[8], &quot;&quot;);
    }

    // Größe der einzelnen String berechnen
    for (i = 0; i &lt; 9; i++)
        size += strlen(sql[i]);

    // Speicherplatz für den Anfragestring reservieren
    query = static_cast&lt;char*&gt;(malloc(size + 1));

    // alles zusammensetzen
    for(i = 0; i &lt; 9; i++)
        strcat(query, sql[i]);

    return query;
}
</code></pre>
<p>Ich übergebe der Funktion die Werte 1, &quot;pass&quot;, &quot;user&quot;, &quot;nick&quot;, &quot;Apocalypse&quot;, NULL, NULL. Wenn man ich den Quellcode durchgehe, würde ich auf folgendes Ergebnis kommen: <strong>SELECT pass FROM user WHERE nick = 'Apocalypse'</strong></p>
<p>Die Funktion kommt im großen und ganzen auch darauf, aber vor SELECT stehen noch drei komische Zeichen und somit wird meine SQL-Anfrage falsch. Woran kann das liegen, dass da noch drei Zeichen vorher angehängt werden? Ich danke euch schonmal...</p>
<p>MfG Apocalypse</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1134565</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1134565</guid><dc:creator><![CDATA[Apocalypse]]></dc:creator><pubDate>Sun, 10 Sep 2006 10:48:09 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit einem char* on Sun, 10 Sep 2006 10:53:06 GMT]]></title><description><![CDATA[<p>Da malloc nicht initialisierten Speicher zurückliefert, setzt das erste strcat in der Schleife ab dem ersten Nullbyte auf. D.h. es ist purer Zufall, was da noch für Zeichen vorstehen. Eine Lösung könnte sein per strcpy den leeren String nach query zu kopieren (oder einfach das erste Byte auf 0 zu setzen).</p>
<p>Allerdings solltest Du ernsthaft drüber nachdenken, ob Du das überhaupt so lassen willst. Ich würde auf Stringstreams ausweichen. malloc ist auch nicht sehr C++-like. Wer gibt eigentlich den ganzen Speicher im sql-Array wieder frei?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1134571</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1134571</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Sun, 10 Sep 2006 10:53:06 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit einem char* on Sun, 10 Sep 2006 10:54:04 GMT]]></title><description><![CDATA[<p>a) Warum nicht std::string?<br />
b) Wenn schon char* dann nimm doch sprintf</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1134572</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1134572</guid><dc:creator><![CDATA[Thomas ** 0]]></dc:creator><pubDate>Sun, 10 Sep 2006 10:54:04 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit einem char* on Sun, 10 Sep 2006 11:06:35 GMT]]></title><description><![CDATA[<p>lol C++ Casts und C Speicherallokierung...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1134578</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1134578</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Sun, 10 Sep 2006 11:06:35 GMT</pubDate></item></channel></rss>