Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Pointer Seg Fault


  • Please log in to reply
14 replies to this topic

#13 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 24 October 2009 - 07:56 AM

Aha, so that is what you meant. I thought you were referring to

if (*string != s){
    *mstring++ = *string;
}

I assumed his code did work, in which case it would be more efficient than yours.

Our codes are basically the same, but you use one more variable and do not use strlen and strcpy. Also you do not use dynamically allocated memory directly, as you said.
  • 0
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#14 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 25 October 2009 - 06:37 AM

I think all of you are going about this in the wrong way. You don't need to use a second array at all. You can strip the string in place. Like so:
#include "stdio.h"

void strip(char * s, char c){
	char * t = s;
	while(*s != 0){
		if(*s != c)*(t++) = *s;
		s++;
	}
	*t = 0;
}

int main (){
	char s[20] = "Hello, World!\n";
	strip(s,'l');
	printf("%s",s);
}

  • 0
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#15 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 25 October 2009 - 07:32 AM

Good idea :P
  • 0
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download