Alternatively, you can specify that the variable holding the result be allocated ahead of time:
String getNth(String str, int n, String result) { ... }
Name:
Anonymous2006-04-02 9:49
I can't even tell what this function does, but it's bad form to return pointers in C since it forces you to malloc, and then you have to free later. Better to pass in a pointer to a string and have the function operate on that.
Name:
Anonymous2006-04-02 15:21
And the format strings accepted by sscanf() aren't regular expressions, so minus points for calling them regexps. Javur fool. Go read a book on C.
void getNth(String str, int n, String result) { ... }
Name:
Anonymous2006-04-02 22:46
>>1
I don't think malloc zeroes the allocated memory, so your'e concatenating %s onto some unknown shit (possibly past the end of the memory you allocated!)
Also, it's probably more efficient to loop through str while counting spaces than to build up this stupid format string that throws away the first n-1 strings. Actually, I can't think of how this function would be useful in the first place (if you call it more than once, you probably should just split up the string by spaces once into an argv-like array.)
/* what the hell is this? nobody knows.
* please use real variable names and
* comments in the future.
*/
for(i = 0; i < n; i++)
{
if (i == n-1) strcat(regExp, "%s");
else strcat(regExp, "%*s");
}
Stop with the sizeof(char). You're just embarrassing yourself there. I've get the ISO/IEC 9899:TC3 document in front of me right now, and it says sizeof(char) is 1. Always. Forever. On every system ever made, and every system that will ever be made. The whole point of sizeof is to determine the length of an array of unsigned char necessary to hold the representation of a value. And the size of a an integer type is the same as the size of unsigned and signed variants of that type, so the representations can be compatible for values in the intersection of their ranges. So sizeof(char) == sizeof(unsigned char), always.
I can understand writing 'char* fred = malloc(sizeof(*fred) * NUM_FRED)', but this 'sizeof(char)' thing is just ridiculous.
Oh, and to answer your problem... it seems like you are a little pointer shy. And ditch your stupid "String" typedef. You also don't want to do that stupid trick with sscanf, it's tricks like that which will end you up on the front of the daily WTF.
char *get_field(char const *restrict s, int n)
{
char const *p;
char *r;
for (; n; ++s) {
if (!*s)
return NULL;
if (*s == ' ') {
n--;
while (*s++ == ' ');
}
}
for (p = s; *p && *p != ' '; ++p);
r = xmalloc(p - s + 1);
memcpy(r, s, p - s);
r[p - s] = '\0';
return r;
}
>>17
There is actually a reason why someone might want to use a sizeof(char). If they eventually wanted to change some code using strings from ASCII to UTF18 (wchar_t), they would have less of a trouble changing the code. On the other hand, they could just use a few [code#ifdef UNICODE[/code]'s to define functions/structures/variable.
Name:
Anonymous2009-11-01 22:11
>>17
Or, they could do the sane thing and use UTF-8.