Re: C and C++



There is an art to getting things done easily in C code. Writing concise
C code that isn't also cryptic and buggy is an even more difficult art.
But it can be done, to some extent.

A lot of it involves learning and developing idioms to accomplish tasks
in a certain standard way with a minimum of extra added steps.

For example, to see if argv contains the switches "-fast" or "-slow",
and to accumulate all the arguments that aren't switches into another
list, you might do something like this:

int main(int argc, char* argv[]) {
char** fileList = malloc(argc * sizeof(char *));
char** fileListEnd = fileList;
char* arg;
int readArg;
int lastFile;
int slow = 0, fast = 0;

I notice your char* you are passing to sizeof is a pointer to a char.
Yes that seems easier. But you've declared two pointers to pointers to
chars. Is this programs using them?

/* parse args */
*fileListEnd = NULL;
for (readArg = 1; readArg < argc; readArg++) {
arg = argv[readArg];

if (*arg == '-') {
if (! strcmp(arg, "-slow")) {
slow++;
} else if (! strcmp(arg, "-fast")) {
fast++;
} else {
printf ("illegal switch '%s'\n", arg);
exit(1);
}
} else {
/* collect a filename */
*(fileListEnd++) = arg;
*fileListEnd = NULL;
}
}

exit(0);
}

You'll notice several labor-saving measures above. Since there
are no collection classes that expand lists as needed, I simply
make a conservative (pessimistic) guess about how big an array
to allocate. I also don't copy the strings in argv; I merely copy
pointers to them. That saves me the trouble of allocating the
right number of bytes. This is OK as long as argv doesn't change,
but I prefer a style where you never change argv anyway. I also
use the "!strcmp()" idiom, which means "these strings match". And
I terminate the fileList array with a NULL pointer (not the same
thing as a null-terminated string, although conceptually identical).
This allows me to pass around a pointer to the start of the array
and that alone is enough information to iterate over all the
elements. In an OO language, you'd just use an object that stores
the data and its length, and pass around that object (or a pointer
or reference to it), but since that option is not easily available
in C, you find a way to do the same thing with a single scalar
value to save work.

Well, hopefully that gives you the idea of what I'm talking about.

I also skipped checking the return code from malloc() and I also
skipped free()ing the malloc()ed memory, but that was out of
laziness (and it is just an example). I'm not advocating really
doing that. :-)

- Logan

Using the freemalloc, memcpy, malloc functions, basically the mem
functions would probably get everything done in cpu registers wouldn't it?

Bill


.



Relevant Pages

  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Sorry, int main (int argc, char **argv) again.
    ... Never because argv is an array. ... a pointer to a char (such as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)