PDA

View Full Version : C and String Manipulation


Codicier
10-07-2008, 02:01 PM
I'm doing some assignments for my operating systems course and the fact that C still doesn't have a standard implementation for stuff like trimming boggles the mind. The most basic string manipulation is a huge task, at least that's been my experience. If I see segmentation fault one more time I'm going to put my keyboard through the monitor. :mad:

Any good string manipulation libraries you guys know of for C? I know C isn't exactly the most popular language these days but anything would be better than the bare bones standard functions in string.h.

DangerousDaze
10-07-2008, 02:06 PM
To be honest, strings in C are really simple once you really understand them. You just have to know pointers (especially when to use an asterisk) and remember that they're null terminated.

Example:

while (*d++=*s++); <-- Never use this in production code!

This is a string copy. :)

You might want to try bstrlib.h but my advice would be to just really grok the defaults because once you do you'll stop making pointer mistakes and really start to fly.

/edit - I just re-read this and it comes across as really condescending. It's not meant to be!

Codicier
10-07-2008, 05:49 PM
If you can answer me this, it might help:

What's the difference between
char * string;
and
char string[someNum];

I was under the impression that character arrays were like pointers pointing at the first character in the array, but my program throws a bitch fit when I try to do:


char string[someNum];
char** ptr;
ptr = &string;


but is fine with:


char * string;
char** ptr;
ptr = &string;


It's been a while since I've dealt with pointers.

DangerousDaze
10-07-2008, 06:05 PM
char *string; <-- creates a character pointer called string pointing nowhere. Write to this pointer and you will stack dump

char string[someNum]; <-- creates a character pointer called string pointing at allocated memory. This is safe provided you stay within the bounds of the string.

Can you tell me how you're using those pointers you create in your examples?

Incidentally, the "&" operator isn't often used with strings because the name of string is already a pointer. For example, you might well do this:

int i=0;
int *x=&i;
*x=2;

This would set the variable i to 2 via the pointer x.

With strings you'd just do this:

char s[10]="A";
char *x = s; /* Note: No need for & because s is already an address */
*x='B';

This would change the string s to "B". Note that you don't need the "&" operator because s is already an address. If you did use "&" then you'd be making a pointer to the address of the string rather than a pointer to the string (i.e. a pointer to a pointer to a string) which wouldn't be what you wanted to do.

Hope I got that right - it's been years since I wrote in C and it's easy to create bugs when you're just scribbling in a post!

/edit - sorry this post changed a lot. I screwed it up during editing.

Codicier
10-07-2008, 07:00 PM
Well the only reason I'm trying to get a char ** of a string is because I'm using strsep (http://www.mkssoftware.com/docs/man3/strsep.3.asp) to try and tokenize a string. I've changed my code around a bit and it seems to work now but hell if it makes any sense how I got it to work.

I was going to paste the example here but now CoG seems to dislike my code, so I attached it as a text file. strsep seems to be ok with the method I use in that file.

DangerousDaze
10-08-2008, 03:45 AM
it seems to work now but hell if it makes any sense how I got it to work.

When C and pointers are concerned this is often a dangerous state of affairs. The code around your pointers may look to be working fine but you could be corrupting state elsewhere, leading to incredibly difficult bugs to find.

Example: I once had a program that core dumped on me so I put a printf statement in to see the contents of a couple of variables. Not only did the variables looks ok but the program no longer crashed! I took the printf out again and, boom, crash. What was happening was that I had a wild pointer but when I used the printf it grew the heap just big enough such that the pointer was back in valid memory space (though not where it needed to be). Bitchy.

I'm at work now but I'll take a look at your code later on.

DangerousDaze
10-08-2008, 06:04 AM
Site Admins, can you please fix the code injection error page or turn checking off because I've lost another long post over it. :( I'll post in the maintenance thread...

Anyway, the pointer code is ok in your sample. I'd recommend "hardening" it to cope with errors (you probably just left that out for brevity anyway) and I'd recommend closing the file as soon as you're done with it.

Here's an example that doesn't use strsep, but there are probably a hundred ways of doing this in C, if not an infinite number. ;)

QueQueg
10-08-2008, 07:59 AM
As a professional C/C++/C# developer of 12 years, I endorse this thread.

DD, you're quite the mentor.

Codicier
10-08-2008, 10:37 AM
As a professional C/C++/C# developer of 12 years, I endorse this thread.

DD, you're quite the mentor.

I accept your endorsement. I will now need your sponsorship to continue creating further threads complaining about C/C++.

"Sponsorship" being cash money.

DangerousDaze
10-08-2008, 10:43 AM
C will always be my favourite programming language. It doesn't try to shield you from the harsh realities of computing and once you "get it" you have limitless freedom

It's like kung fu for computers! Early on that freedom may well get you into trouble because you'll be tempted to write cryptic code like that string copy in my first reply, but later on you'll settle down and realise that with great power comes great, er, somethingorother, and you'll just get shit done. :)

QueQueg
10-08-2008, 02:08 PM
Personally, I think C# is the ultimate expression of the power of C. You retain ability to go "unsafe" if you want to, so all your pointer kung-fu isn't for naught, but you also get a rather impressive collection of goodies in the CLR to build upon.