<?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[Removing spaces in char arrays]]></title><description><![CDATA[<p>Hi,</p>
<p>ich versuche gerade aus mehreren char arrays leerzeichen zu entfernen.<br />
Ich verzweifle hier gerade an der wolhbekannten Pointerarithmetik.</p>
<p>Ich habe zwei funktionen überladen. Die eine löscht erfolgreich leerzeichen.<br />
Die zweite soll ein zweidimensionales char array annehmen können, welches natürlich aus allen arrays die leerzeichen löscht.</p>
<p>Die letztere Funktion will nich so ganz. Also es wird erfolgreich kompiliert und es wird auch an die erste Funktion ein array übergeben. Allerdings<br />
tritt bei dieser konstellation dann ein memory violation fehler auf.</p>
<p>Vielen Dank</p>
<pre><code class="language-cpp">int main(void){
char *input2[]={&quot;hal lo&quot;,&quot;bert a&quot;,&quot;  hugo&quot;};
removeSpaces(input2);
cout &lt;&lt; &quot;\n\n whitespaces removed:&quot; &lt;&lt; input;
}

void removeSpaces(char* cArray[]){

	int i=0;
	while(*(cArray+(i))){
		cout &lt;&lt; (cArray+i) &lt;&lt; &quot;: &quot; &lt;&lt; *(cArray+i) &lt;&lt; endl;

		removeSpaces(cArray[i]);
		i++;
	}
}

void removeSpaces(char* c){
	cout &lt;&lt; &quot;&amp;c: &quot; &lt;&lt; &amp;c &lt;&lt; endl;
	cout &lt;&lt; &quot;c: &quot; &lt;&lt; c;

	char *s=c;
    while(*(c++))
    {
		while(*(s++)){
			if(*s!=' ')
				break;
		}
		*c=*s;
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/259429/removing-spaces-in-char-arrays</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 21:14:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/259429.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 22 Jan 2010 21:16:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Removing spaces in char arrays on Fri, 22 Jan 2010 21:16:39 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich versuche gerade aus mehreren char arrays leerzeichen zu entfernen.<br />
Ich verzweifle hier gerade an der wolhbekannten Pointerarithmetik.</p>
<p>Ich habe zwei funktionen überladen. Die eine löscht erfolgreich leerzeichen.<br />
Die zweite soll ein zweidimensionales char array annehmen können, welches natürlich aus allen arrays die leerzeichen löscht.</p>
<p>Die letztere Funktion will nich so ganz. Also es wird erfolgreich kompiliert und es wird auch an die erste Funktion ein array übergeben. Allerdings<br />
tritt bei dieser konstellation dann ein memory violation fehler auf.</p>
<p>Vielen Dank</p>
<pre><code class="language-cpp">int main(void){
char *input2[]={&quot;hal lo&quot;,&quot;bert a&quot;,&quot;  hugo&quot;};
removeSpaces(input2);
cout &lt;&lt; &quot;\n\n whitespaces removed:&quot; &lt;&lt; input;
}

void removeSpaces(char* cArray[]){

	int i=0;
	while(*(cArray+(i))){
		cout &lt;&lt; (cArray+i) &lt;&lt; &quot;: &quot; &lt;&lt; *(cArray+i) &lt;&lt; endl;

		removeSpaces(cArray[i]);
		i++;
	}
}

void removeSpaces(char* c){
	cout &lt;&lt; &quot;&amp;c: &quot; &lt;&lt; &amp;c &lt;&lt; endl;
	cout &lt;&lt; &quot;c: &quot; &lt;&lt; c;

	char *s=c;
    while(*(c++))
    {
		while(*(s++)){
			if(*s!=' ')
				break;
		}
		*c=*s;
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1843492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1843492</guid><dc:creator><![CDATA[irgendwer]]></dc:creator><pubDate>Fri, 22 Jan 2010 21:16:39 GMT</pubDate></item><item><title><![CDATA[Reply to Removing spaces in char arrays on Fri, 22 Jan 2010 21:49:04 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

void removeSpaces (char* c)
{
   char *p;
   for (p=c; *c; c++)
      if (*c != ' ')
         *p++ = *c;
   *p = 0;
}

void removeSpaces (char *cArray[])
{
   while (*cArray)
      removeSpaces (*(cArray++));
}

int main(void)
{
   char a[] = &quot;hal lo&quot;;
   char b[] = &quot;bert a&quot;;
   char c[] = &quot;  hugo&quot;;
   char *input2[] = {a, b, c, 0}; 

   removeSpaces (input2);

   for (int i=0; input2[i] ;i++)
      cout &lt;&lt; input2[i] &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1843498</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1843498</guid><dc:creator><![CDATA[C++Fan 2010]]></dc:creator><pubDate>Fri, 22 Jan 2010 21:49:04 GMT</pubDate></item><item><title><![CDATA[Reply to Removing spaces in char arrays on Fri, 22 Jan 2010 21:50:28 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::vector&lt;char&gt; vec(cArray, std::strlen(cArray));

std::vector&lt;char&gt;::iterator it = std::remove(vec.begin(), vec.end(), ' ');

vec.resize( it - vec.begin() );
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1843499</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1843499</guid><dc:creator><![CDATA[ldfhgld]]></dc:creator><pubDate>Fri, 22 Jan 2010 21:50:28 GMT</pubDate></item><item><title><![CDATA[Reply to Removing spaces in char arrays on Fri, 22 Jan 2010 21:54:09 GMT]]></title><description><![CDATA[<p>Das geht auch ohne Container</p>
<pre><code class="language-cpp">void removeSpaces (char* c)
{
    std::remove( c, c + strlen( c ) + 1, ' ' );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1843502</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1843502</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 22 Jan 2010 21:54:09 GMT</pubDate></item><item><title><![CDATA[Reply to Removing spaces in char arrays on Fri, 22 Jan 2010 22:30:41 GMT]]></title><description><![CDATA[<p>danke fan!</p>
<p>ich hätte vielleicht dazuschreiben sollen, dass ich keinen workaround haben möchte.</p>
<p>Kann mir jemand erklären warum mein code nicht funktioniert ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1843513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1843513</guid><dc:creator><![CDATA[irgendwer]]></dc:creator><pubDate>Fri, 22 Jan 2010 22:30:41 GMT</pubDate></item><item><title><![CDATA[Reply to Removing spaces in char arrays on Fri, 22 Jan 2010 22:38:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">while(*(c++)) 
//...
    *c=*s;
</code></pre>
<p>Du testest erst, ob c gültig ist, dann inkrementierst du das und arbeitest dann mit dem inkrementieren wert weiter, der ungültig sein kann</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1843516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1843516</guid><dc:creator><![CDATA[sfdgchj]]></dc:creator><pubDate>Fri, 22 Jan 2010 22:38:36 GMT</pubDate></item></channel></rss>