array of pointers to char in functions

B

buda

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.
 
E

Eric Sosman

buda said:
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.
 
M

Mike Wahler

buda said:
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
 
B

buda

Eric Sosman said:
buda said:
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]) );
 
E

Eric Sosman

buda said:
buda said:
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.
 
B

buda

Eric Sosman said:
buda said:
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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top