Re: Yet another pointer related problem

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 03/03/04


Date: 3 Mar 2004 13:54:30 GMT

In <c24e34$53j$1@news-rocq.inria.fr> Grumble <invalid@kma.eu.org> writes:

>sugaray wrote:
>
>> The purpose of the following program is to store all the arguments
>> passed to main() into an array of char, and print'em out onto the
>> screen.
>
>#include <stdio.h>
>
>int main(int argc, char **argv)
>{
> char **p;
>
> for (p = argv; *p != NULL; ++p) puts(*p);
>
> return 0;
>}
>
>or
>
>#include <stdio.h>
>
>int main(int argc, char **argv)
>{
> int i;
>
> for (i=0; i < argc; ++i) puts(argv[i]);
>
> return 0;
>}

Two examples of wasteful programs that can be combined into an
environmentally friendly one:

    #include <stdio.h>

    int main(int argc, char **argv)
    {
        while (*argv) puts(*argv++);
        return 0;
    }

Dan :-)

P.S. This example and the canonical strcpy loop are at the limit of
      what I consider readable C code. People who don't understand them
      cannot consider themselves C programmers and people who abhor them
      are likely to never like C.

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages

  • Re: Newbie help on string splitting
    ... a logical first step would be to find out how to store the ... You can use an unsigned int to store the first and second ... int StrPos(const char *s, char c) ... To your problem, to store an individual IP, load it into an array of four chars ...
    (comp.lang.c)
  • Re: Reading a Text file
    ... int main ... In any case, you said earlier you want to store the data in an array, ... don't store its result in a char. ...
    (comp.lang.c)
  • Re: Natural size: int
    ... "char" is commonly used to store text characters. ... "short" is commonly used to store large arrays of numbers, or perhaps wide text characters. ... "int" is commonly used to store an integer. ... "int unsigned" guarantees us at least 65535 array elements -- what percentage of the time do we have an array any bigger than that? ...
    (comp.lang.c)
  • Re: storing delimited string in an array
    ... What would be the best way of storing the above string into an array ... I typically build a array of pointers to some char arrays of sufficient size. ... For example, I will often declare a char **, allocated space for the number of char * items I want to store in it and then allocate room for each string as needed, associating them with the container pointer "array". ...
    (comp.lang.c)
  • Re: Assign continusly variable to pointer.
    ... what is the purpose of the array? ... actually trying to store, we might be able to suggest ways of doing it that ... Prev by Date: ...
    (microsoft.public.vc.language)