Hi,
given the following 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. 5 1494
buda wrote: Hi, given the following 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.
-- Er*********@sun.com
"buda" <ku*****@hotmail.com> wrote in message
news:c9**********@ls219.htnet.hr... Hi, given the following 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
"Eric Sosman" <Er*********@sun.com> wrote in message
news:40**************@sun.com... buda wrote: Hi, given the following 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.
-- Er*********@sun.com
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]) );
buda wrote: "Eric Sosman" <Er*********@sun.com> wrote in message news:40**************@sun.com...
buda wrote:
Hi, given the following 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.
-- Er*********@sun.com
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.
-- Er*********@sun.com
"Eric Sosman" <Er*********@sun.com> wrote in message
news:40**************@sun.com... buda wrote: "Eric Sosman" <Er*********@sun.com> wrote in message news:40**************@sun.com...
buda wrote:
Hi, given the following 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,
andwould be grateful if someone wrote it, along with the function call
frommain). 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.
-- Er*********@sun.com
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.
-- Er*********@sun.com
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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Carson |
last post by:
Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Carson
|
by: Christopher Benson-Manica |
last post by:
Recently, the following definition was posted to comp.lang.c++:
> char **Arr;
1) Is the type of this object "array of 3 (array of two of pointer to
pointer to char)"? If not, what is it?
...
|
by: Frank M. |
last post by:
I'm trying to declare an array of pointers to structures so that I can make
the last element a NULL pointer. I figure that it would more easily allow
my library routines to know when to stop...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: Stijn van Dongen |
last post by:
A question about void*. I have a hash library where the hash create
function accepts functions
unsigned (*hash)(const void *a)
int (*cmp) (const void *a, const void *b)
The insert function...
|
by: natkw1 |
last post by:
Hi,
I'm attempting to understand the use of pointers(at least grasp how
pointers work). I've read the FAQ on
http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I'm
still a bit...
|
by: Chad |
last post by:
I'm want
static char *output;
to hold the modified string "tel chad"
However, when I debug it,
static char *output
holds the ascii value of the strng, and not the string itself.
Here is...
|
by: M Turo |
last post by:
Hi,
I was wondering if anyone can help. I'm want to pre-load a 'table' of
function pointers that I can call using a its arrayed index, eg (non c
code example)
pFunc = func_A;
pFunc = func_B;
|
by: Ben Bacarisse |
last post by:
candide <toto@free.frwrites:
These two statements are very different. The first one is just wrong
and I am pretty sure you did not mean to suggest that. There is no
object in C that is the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |