array of pointers to char in functions

Discussion in 'C Programming' started by buda, May 26, 2004.

  1. buda

    buda Guest

    Hi,
    given the following code,

    Code:
    ....
    int main( void ) {
    char *a[] = { "abc", "def", "ghijkl", "o", "prs" }; // for example
    ....
    }
    
    is it possible to write a small routine (a function), that will print the
    string literals in array a, character by character. (I assume it is, and
    would be grateful if someone wrote it, along with the function call from
    main). I didn't notice anything about this type of problem in the FAQ
    (although it does mention multi-dimensional arrays of various sizes, but i
    don't quite get how I should "simulate all the arrays dinamically")

    Thanks for the help.
     
    buda, May 26, 2004
    #1
    1. Advertising

  2. buda

    Eric Sosman Guest

    buda wrote:
    > Hi,
    > given the following code,
    >
    >
    Code:
    > ...
    > int main( void ) {
    > char *a[] = { "abc", "def", "ghijkl", "o", "prs" }; // for example
    > ...
    > }
    > 
    >
    > is it possible to write a small routine (a function), that will print the
    > string literals in array a, character by character. (I assume it is, and
    > would be grateful if someone wrote it, along with the function call from
    > main). I didn't notice anything about this type of problem in the FAQ
    > (although it does mention multi-dimensional arrays of various sizes, but i
    > don't quite get how I should "simulate all the arrays dinamically")


    Yes, it is possible to do what you ask in the first
    sentence. Since there's a scent of homework in the air,
    I'll just offer the declaration of the function (this is
    just one of many possibilities):

    void print_them(char *strings[], size_t how_many);

    .... and the way you would call it from main():

    print_them(a, sizeof a / sizeof a[0]);

    As for the remainder, I think you're going astray.
    There are no multi-dimensional arrays in what you've
    shown (strictly speaking, there are no multi-dimensional
    arrays in C, but what you've shown isn't even the "array
    of arrays" often referred to as an m.d.a.), and there is
    no dynamic memory allocation going on. Re-visiting the
    FAQ's section on pointers and arrays might be a good idea.

    --
     
    Eric Sosman, May 26, 2004
    #2
    1. Advertising

  3. buda

    Mike Wahler Guest

    "buda" <> wrote in message
    news:c92j21$j95$...
    > Hi,
    > given the following code,
    >
    >
    Code:
    > ...
    > int main( void ) {
    > char *a[] = { "abc", "def", "ghijkl", "o", "prs" }; // for example
    > ...
    > }
    > 
    >
    > is it possible to write a small routine (a function), that will print the
    > string literals in array a, character by character. (I assume it is, and
    > would be grateful if someone wrote it, along with the function call from
    > main).


    #include <stdio.h>

    void out(const char *arr[], size_t sz)
    {
    size_t i = 0;

    for(i = 0; i < sz; ++i)
    {
    while(*arr)
    putchar(*arr++);

    putchar('\n');
    }
    }

    int main(void)
    {
    char *a[] = {"abc", "def", "ghijkl", "o", "prs"};
    out(a, sizeof a / sizeof *a);
    return 0;
    }

    I hope I didn't just do your homework for you. :)

    > I didn't notice anything about this type of problem in the FAQ
    > (although it does mention multi-dimensional arrays of various sizes, but i
    > don't quite get how I should "simulate all the arrays dinamically")


    No 'simulation' required.

    -Mike
     
    Mike Wahler, May 26, 2004
    #3
  4. buda

    buda Guest

    "Eric Sosman" <> wrote in message
    news:...
    > buda wrote:
    > > Hi,
    > > given the following code,
    > >
    > >
    Code:
    > > ...
    > > int main( void ) {
    > > char *a[] = { "abc", "def", "ghijkl", "o", "prs" }; // for example
    > > ...
    > > }
    > > 
    > >
    > > is it possible to write a small routine (a function), that will print

    the
    > > string literals in array a, character by character. (I assume it is, and
    > > would be grateful if someone wrote it, along with the function call from
    > > main). I didn't notice anything about this type of problem in the FAQ
    > > (although it does mention multi-dimensional arrays of various sizes, but

    i
    > > don't quite get how I should "simulate all the arrays dinamically")

    >
    > Yes, it is possible to do what you ask in the first
    > sentence. Since there's a scent of homework in the air,
    > I'll just offer the declaration of the function (this is
    > just one of many possibilities):
    >
    > void print_them(char *strings[], size_t how_many);
    >
    > ... and the way you would call it from main():
    >
    > print_them(a, sizeof a / sizeof a[0]);
    >
    > As for the remainder, I think you're going astray.
    > There are no multi-dimensional arrays in what you've
    > shown (strictly speaking, there are no multi-dimensional
    > arrays in C, but what you've shown isn't even the "array
    > of arrays" often referred to as an m.d.a.), and there is
    > no dynamic memory allocation going on. Re-visiting the
    > FAQ's section on pointers and arrays might be a good idea.
    >
    > --
    >
    >


    Not homework in any way... just trying to get a better understanding of
    things. I was not implying that this is dynamic memory allocation in any
    way, but just qouting something from the FAQ that seemed relevant to me.
    Anyway, thanks for the answer. I didn't try the most obvious thing before
    posting. So, if someone want's to know, here's the solution

    void charbychar( char **a, int size ) {
    char *p;
    int i;

    for ( i = 0; i < size; ++i ) {
    for ( p = a; *p; ++p )
    printf( "%c", *p );
    putchar( '\n' );
    }
    }

    and the call is charbychar( a, sizeof(a)/sizeof(a[0]) );
     
    buda, May 26, 2004
    #4
  5. buda

    Eric Sosman Guest

    buda wrote:
    > "Eric Sosman" <> wrote in message
    > news:...
    >
    >>buda wrote:
    >>
    >>>Hi,
    >>>given the following code,
    >>>
    >>>
    Code:
    >>>...
    >>>int main( void ) {
    >>>char *a[] = { "abc", "def", "ghijkl", "o", "prs" }; // for example
    >>>...
    >>>}
    >>>
    >>>
    >>>is it possible to write a small routine (a function), that will print

    >
    > the
    >
    >>>string literals in array a, character by character. (I assume it is, and
    >>>would be grateful if someone wrote it, along with the function call from
    >>>main). I didn't notice anything about this type of problem in the FAQ
    >>>(although it does mention multi-dimensional arrays of various sizes, but

    >
    > i
    >
    >>>don't quite get how I should "simulate all the arrays dinamically")

    >>
    >> Yes, it is possible to do what you ask in the first
    >>sentence. Since there's a scent of homework in the air,
    >>I'll just offer the declaration of the function (this is
    >>just one of many possibilities):
    >>
    >>void print_them(char *strings[], size_t how_many);
    >>
    >>... and the way you would call it from main():
    >>
    >>print_them(a, sizeof a / sizeof a[0]);
    >>
    >> As for the remainder, I think you're going astray.
    >>There are no multi-dimensional arrays in what you've
    >>shown (strictly speaking, there are no multi-dimensional
    >>arrays in C, but what you've shown isn't even the "array
    >>of arrays" often referred to as an m.d.a.), and there is
    >>no dynamic memory allocation going on. Re-visiting the
    >>FAQ's section on pointers and arrays might be a good idea.
    >>
    >>--
    >>
    >>

    >
    >
    > Not homework in any way... just trying to get a better understanding of
    > things. I was not implying that this is dynamic memory allocation in any
    > way, but just qouting something from the FAQ that seemed relevant to me.
    > Anyway, thanks for the answer. I didn't try the most obvious thing before
    > posting. So, if someone want's to know, here's the solution
    >
    > void charbychar( char **a, int size ) {
    > char *p;
    > int i;
    >
    > for ( i = 0; i < size; ++i ) {
    > for ( p = a; *p; ++p )
    > printf( "%c", *p );
    > putchar( '\n' );
    > }
    > }
    >
    > and the call is charbychar( a, sizeof(a)/sizeof(a[0]) );


    Glad to see you've come up with a solution. A few
    observations that might help your understanding grow:

    First, you obviously know about the putchar() function.
    Why, then, did you use the printf() cannon to kill the single-
    character canary in the inner loop?

    Second, you might want to consider the puts() function:
    it does the entire job of the inner loop plus the line-ending
    putchar(), all in one function call. Yes, you said that you
    wanted to produce the output "character by character," but
    (1) it's not at all clear what that means, and (2) puts()
    produces its output "as if" by repeated calls to fputc(),
    and each of those outputs a single character.

    Third, you've used an `int' for the number of elements
    in the array, and again for the variable that indexes them.
    It'd be good to develop the habit of using `size_t' for
    such things, because different C implementations have different
    definitions of `int' and some of them may "top out" at a
    mere 32767. That's far more than your toy program needs, of
    course, but one assumes you won't always be writing toys ...
    When you use a `size_t' you still don't know exactly how high
    the value can go, but you know it can go at least high enough
    to count all the bytes -- and hence all the elements -- of the
    largest array the implementation can support.

    Finally, the parentheses are unnecessary when `sizeof' is
    applied to a variable: `sizeof a' and `sizeof(a)' are the same
    thing. However, parentheses *are* required when `sizeof' is
    applied to a type; you must write `sizeof(int)' instead of
    `sizeof int'. If you'd prefer to make all your `sizeof's
    look the same by using parentheses even when they're not
    required, that's fine.

    --
     
    Eric Sosman, May 26, 2004
    #5
  6. buda

    buda Guest

    "Eric Sosman" <> wrote in message
    news:...
    > buda wrote:
    > > "Eric Sosman" <> wrote in message
    > > news:...
    > >
    > >>buda wrote:
    > >>
    > >>>Hi,
    > >>>given the following code,
    > >>>
    > >>>
    Code:
    > >>>...
    > >>>int main( void ) {
    > >>>char *a[] = { "abc", "def", "ghijkl", "o", "prs" }; // for example
    > >>>...
    > >>>}
    > >>>
    > >>>
    > >>>is it possible to write a small routine (a function), that will print

    > >
    > > the
    > >
    > >>>string literals in array a, character by character. (I assume it is,

    and
    > >>>would be grateful if someone wrote it, along with the function call

    from
    > >>>main). I didn't notice anything about this type of problem in the FAQ
    > >>>(although it does mention multi-dimensional arrays of various sizes,

    but
    > >
    > > i
    > >
    > >>>don't quite get how I should "simulate all the arrays dinamically")
    > >>
    > >> Yes, it is possible to do what you ask in the first
    > >>sentence. Since there's a scent of homework in the air,
    > >>I'll just offer the declaration of the function (this is
    > >>just one of many possibilities):
    > >>
    > >>void print_them(char *strings[], size_t how_many);
    > >>
    > >>... and the way you would call it from main():
    > >>
    > >>print_them(a, sizeof a / sizeof a[0]);
    > >>
    > >> As for the remainder, I think you're going astray.
    > >>There are no multi-dimensional arrays in what you've
    > >>shown (strictly speaking, there are no multi-dimensional
    > >>arrays in C, but what you've shown isn't even the "array
    > >>of arrays" often referred to as an m.d.a.), and there is
    > >>no dynamic memory allocation going on. Re-visiting the
    > >>FAQ's section on pointers and arrays might be a good idea.
    > >>
    > >>--
    > >>
    > >>

    > >
    > >
    > > Not homework in any way... just trying to get a better understanding of
    > > things. I was not implying that this is dynamic memory allocation in any
    > > way, but just qouting something from the FAQ that seemed relevant to me.
    > > Anyway, thanks for the answer. I didn't try the most obvious thing

    before
    > > posting. So, if someone want's to know, here's the solution
    > >
    > > void charbychar( char **a, int size ) {
    > > char *p;
    > > int i;
    > >
    > > for ( i = 0; i < size; ++i ) {
    > > for ( p = a; *p; ++p )
    > > printf( "%c", *p );
    > > putchar( '\n' );
    > > }
    > > }
    > >
    > > and the call is charbychar( a, sizeof(a)/sizeof(a[0]) );

    >
    > Glad to see you've come up with a solution. A few
    > observations that might help your understanding grow:
    >
    > First, you obviously know about the putchar() function.
    > Why, then, did you use the printf() cannon to kill the single-
    > character canary in the inner loop?
    >
    > Second, you might want to consider the puts() function:
    > it does the entire job of the inner loop plus the line-ending
    > putchar(), all in one function call. Yes, you said that you
    > wanted to produce the output "character by character," but
    > (1) it's not at all clear what that means, and (2) puts()
    > produces its output "as if" by repeated calls to fputc(),
    > and each of those outputs a single character.
    >
    > Third, you've used an `int' for the number of elements
    > in the array, and again for the variable that indexes them.
    > It'd be good to develop the habit of using `size_t' for
    > such things, because different C implementations have different
    > definitions of `int' and some of them may "top out" at a
    > mere 32767. That's far more than your toy program needs, of
    > course, but one assumes you won't always be writing toys ...
    > When you use a `size_t' you still don't know exactly how high
    > the value can go, but you know it can go at least high enough
    > to count all the bytes -- and hence all the elements -- of the
    > largest array the implementation can support.
    >
    > Finally, the parentheses are unnecessary when `sizeof' is
    > applied to a variable: `sizeof a' and `sizeof(a)' are the same
    > thing. However, parentheses *are* required when `sizeof' is
    > applied to a type; you must write `sizeof(int)' instead of
    > `sizeof int'. If you'd prefer to make all your `sizeof's
    > look the same by using parentheses even when they're not
    > required, that's fine.
    >
    > --
    >
    >


    Thanks for the comments... I know about sizeof, but really do prefer to use
    parentheses :) I wanted to explicilty print character by character since i
    got mixed up with indexing the first time I tryed to write it (before I
    posted the question). Ofcourse, it's obvious to me now that a much cleaner
    solution is quite trivial :)
     
    buda, May 27, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
  2. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    325
    August Karlstrom
    Sep 24, 2006
  3. Replies:
    3
    Views:
    757
  4. davidb
    Replies:
    0
    Views:
    809
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,596
    Default User
    Sep 1, 2006
Loading...

Share This Page