Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array of pointers to char in functions

Reply
Thread Tools

array of pointers to char in functions

 
 
buda
Guest
Posts: n/a
 
      05-26-2004
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.


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-26-2004
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      05-26-2004
"buda" <(E-Mail Removed)> wrote in message
news:c92j21$j95$(E-Mail Removed)...
> 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[i])
putchar(*arr[i]++);

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



 
Reply With Quote
 
buda
Guest
Posts: n/a
 
      05-26-2004
"Eric Sosman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
> --
> (E-Mail Removed)
>


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[i]; *p; ++p )
printf( "%c", *p );
putchar( '\n' );
}
}

and the call is charbychar( a, sizeof(a)/sizeof(a[0]) );


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-26-2004
buda wrote:
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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.
>>
>>--
>>(E-Mail Removed)
>>

>
>
> 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[i]; *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.

--
(E-Mail Removed)

 
Reply With Quote
 
buda
Guest
Posts: n/a
 
      05-27-2004

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> buda wrote:
> > "Eric Sosman" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >>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.
> >>
> >>--
> >>(E-Mail Removed)
> >>

> >
> >
> > 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[i]; *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.
>
> --
> (E-Mail Removed)
>


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers to char pointers in functions newbie C Programming 9 09-24-2006 10:31 AM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments