473,503 Members | 4,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting number of strings literals in double dimentional char array

Hi all,

Below is the code wherein I am initializing double dimentional array
inside main with string literals.
Now I want to display the strings using a function call to which I just
want to pass the array as argument with no other info like number of
strings.

Is there a way to achieve that?

Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.

--------------------------

#include<stdio.h>
#define NUM 20

void display(char input[][NUM])
{
char (*pc)[NUM];
int i=0;

pc=input;
for(i=0;strcmp(*pc,"")!=0;i++,pc++){
printf("%s\n",*pc);
}

}
int main()
{

char input[][NUM] = {"hi","from","ranjeet",""};

display(input);
}

----------------------

Mar 7 '06 #1
14 2321

ranjmis wrote:
Hi all,

Below is the code wherein I am initializing double dimentional array
inside main with string literals.
Now I want to display the strings using a function call to which I just
want to pass the array as argument with no other info like number of
strings.

Is there a way to achieve that?

Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.

<snip>

It's impossible to know the length of your array unless you specify the
length to the function or mark it in a certain way.
I'm not sure that this is valid everywhere but you can declare main
like this:
int main(int argc, char *argv[], char **env)
where env is an array of string that lists every environment variable.
There is no counter that tells you how many elements there are (like
argc for argv) but it's dimension is one larger than the number of
variables and the last one is set to NULL. You can go through the lines
one by one until you hit NULL.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Mar 7 '06 #2
In article <11**********************@z34g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> wrote:
I'm not sure that this is valid everywhere but you can declare main
like this:
int main(int argc, char *argv[], char **env)
where env is an array of string that lists every environment variable.


That is not part of the C standard, but may be offered as an
extension by an implementation.

[It doesn't seem relevant to the user's question, though, since
the user already discussed adding a sentinal.]
--
Programming is what happens while you're busy making other plans.
Mar 7 '06 #3

Walter Roberson wrote:
In article <11**********************@z34g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> wrote:
I'm not sure that this is valid everywhere but you can declare main
like this:
int main(int argc, char *argv[], char **env)
where env is an array of string that lists every environment variable.


That is not part of the C standard, but may be offered as an
extension by an implementation.

[It doesn't seem relevant to the user's question, though, since
the user already discussed adding a sentinal.]


I just told him the options. I also think that using NULL is better
than using a zero length string.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Mar 7 '06 #4
ranjmis wrote:
Hi all,

Below is the code wherein I am initializing double dimentional array
inside main with string literals.
Now I want to display the strings using a function call to which I just
want to pass the array as argument with no other info like number of
strings.

Is there a way to achieve that?

Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.

--------------------------

#include<stdio.h>
#define NUM 20
You really don't need this in your example below with my revisions.

void display(char input[][NUM])
Change the parameter of this function to a pointer to pointer of type char.
{
char (*pc)[NUM];
int i=0;

pc=input;
for(i=0;strcmp(*pc,"")!=0;i++,pc++){
printf("%s\n",*pc);
}

}
You will want to get rid of the above and replace it with the following:

void display(char **input)
{
size_t i;

for(i = 0; input[i] != NULL; i++)
printf("%s\n", input[i]);
}

int main()
main either accepts two arguments or no arguments. Since you aren't
using argc and argv you need to add void in place of them.
{

char input[][NUM] = {"hi","from","ranjeet",""};
Using an empty (zero length) string is going to throw all kinds of
problems into the mix if you aren't careful. The better way to declare
and initialize the above would be:

char *input[] = { "hi", "from", "ranjeet", NULL };

Using the above instead, we now know where the end of our array is
located. This makes looping through the array much easier.

display(input);
You declared main as returning an int (which is correct), however you
are not returning a value. Might want to add return(0); here.
}

----------------------


Now that I've told you what changes to make, I'll tell you why. The
first change you will notice is that I declared display() as accepting
one parameter of type pointer to pointer of type char (char **). This is
an array of pointers to type char *. The reason for this change should
be obvious.

The next change will be declaring i as type size_t (you could replace
that with unsigned int if you wish, either way would work). Basically
you want an unsigned datatype here since you won't be accessing a
negative element of the array (e.g., input[-1] is invalid). Personal
preference I suppose.

Now this brings us to using NULL as the terminator for the array. Almost
every type of program which uses arrays like you are attempting to do
will use NULL as the terminator. This is easier to check for and much
faster than strcmp() and friends. NULL is also the safest bet to use as
a terminator since it is part of the C standard and thusly will always
be available.

Hope that clears things up for you,

Joe
Mar 7 '06 #5
"Nelu" <ta********@gmail.com> writes:
Walter Roberson wrote:
In article <11**********************@z34g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> wrote:
>I'm not sure that this is valid everywhere but you can declare main
>like this:
>int main(int argc, char *argv[], char **env)
>where env is an array of string that lists every environment variable.


That is not part of the C standard, but may be offered as an
extension by an implementation.

[It doesn't seem relevant to the user's question, though, since
the user already discussed adding a sentinal.]


I just told him the options. I also think that using NULL is better
than using a zero length string.


Agreed. But note that an empty string "" is not a zero length array.
Its length *as an array* is 1 (because of the trailing '\0'), even
though strlen("")==0.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 7 '06 #6
Joe Estock <je*****@NOSPAMnutextonline.com> writes:
[...]
Now that I've told you what changes to make, I'll tell you why. The
first change you will notice is that I declared display() as accepting
one parameter of type pointer to pointer of type char (char **). This
is an array of pointers to type char *. The reason for this change
should be obvious.

[...]

No, a char** is a pointer-to-pointer-to-char, *not* an array of any
kind. It can be used to point to the first element of an array of
pointers-to-char.

Arrays are not pointers. Pointers are not arrays.

See <http://www.c-faq.com/>, section 6, "Arrays and Pointers".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 7 '06 #7

Keith Thompson wrote:
"Nelu" <ta********@gmail.com> writes:
Walter Roberson wrote:
In article <11**********************@z34g2000cwc.googlegroups .com>,
Nelu <ta********@gmail.com> wrote:

>I'm not sure that this is valid everywhere but you can declare main
>like this:
>int main(int argc, char *argv[], char **env)
>where env is an array of string that lists every environment variable.

That is not part of the C standard, but may be offered as an
extension by an implementation.

[It doesn't seem relevant to the user's question, though, since
the user already discussed adding a sentinal.]


I just told him the options. I also think that using NULL is better
than using a zero length string.


Agreed. But note that an empty string "" is not a zero length array.
Its length *as an array* is 1 (because of the trailing '\0'), even
though strlen("")==0.


Yes, zero length string and zero length array are two different things.
I've seen people making mistakes similar to this and even worse, people
treating NULL like a '\0' string.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Mar 7 '06 #8
Keith Thompson wrote:
Joe Estock <je*****@NOSPAMnutextonline.com> writes:
[...]
Now that I've told you what changes to make, I'll tell you why. The
first change you will notice is that I declared display() as accepting
one parameter of type pointer to pointer of type char (char **). This
is an array of pointers to type char *. The reason for this change
should be obvious.


[...]

No, a char** is a pointer-to-pointer-to-char, *not* an array of any
kind. It can be used to point to the first element of an array of
pointers-to-char.

Arrays are not pointers. Pointers are not arrays.

See <http://www.c-faq.com/>, section 6, "Arrays and Pointers".


Whoops, thanks for catching my mistake Keith. I was more focused on the
changes to the OP's source code than I was with what I was typing :)

Joe
Mar 8 '06 #9
Nelu wrote:

Keith Thompson wrote:
Agreed. But note that an empty string ""
is not a zero length array.
Its length *as an array* is 1 (because of the trailing '\0'), even
though strlen("")==0.


Yes, zero length string and zero length array
are two different things.


A string has "length".
An array has "size".

--
pete
Mar 8 '06 #10
pete <pf*****@mindspring.com> writes:
Nelu wrote:
Keith Thompson wrote:
> Agreed. But note that an empty string ""
> is not a zero length array.
> Its length *as an array* is 1 (because of the trailing '\0'), even
> though strlen("")==0.


Yes, zero length string and zero length array
are two different things.


A string has "length".
An array has "size".


Perhaps, but the standard's terminology isn't consistent enough that
we can make much use of this. Consider variable *length* arrays.
Consider also that "size" often refers to the result of the sizeof
operator, not to the number of elements in an array.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #11
Thanks Joe.
I was really looking for something better.
The solution is really clean and seems to be faster

Mar 8 '06 #12
Nelu wrote:
ranjmis wrote:

Below is the code wherein I am initializing double dimentional
array inside main with string literals. Now I want to display
the strings using a function call to which I just want to pass
the array as argument with no other info like number of strings.

Is there a way to achieve that?

Right now I have applied a workaround where I keep my last
literal as "" which acts as my end of literals.

<snip>

It's impossible to know the length of your array unless you
specify the length to the function or mark it in a certain way.
I'm not sure that this is valid everywhere but you can declare
main like this:

int main(int argc, char *argv[], char **env)

where env is an array of string that lists every environment
variable. There is no counter that tells you how many elements
there are (like argc for argv) but it's dimension is one larger
than the number of variables and the last one is set to NULL.
You can go through the lines one by one until you hit NULL.


This is non-standard. The only approved method of interogating
environmental variables is with the getenv function, defined below
in N869:

7.20.4.5 The getenv function

Synopsis

[#1]
#include <stdlib.h>
char *getenv(const char *name);

Description

[#2] The getenv function searches an environment list,
provided by the host environment, for a string that matches
the string pointed to by name. The set of environment names
and the method for altering the environment list are
implementation-defined.

[#3] The implementation shall behave as if no library
function calls the getenv function.

Returns

[#4] The getenv function returns a pointer to a string
associated with the matched list member. The string pointed
to shall not be modified by the program, but may be
overwritten by a subsequent call to the getenv function. If
the specified name cannot be found, a null pointer is
returned.

which won't give you a count of environmental variables, whatever
they may be.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 8 '06 #13
On 7 Mar 2006 08:48:13 -0800, "ranjmis" <ra*****@gmail.com> wrote:
Hi all,

Below is the code wherein I am initializing double dimentional array
inside main with string literals.
Now I want to display the strings using a function call to which I just
want to pass the array as argument with no other info like number of
strings.
If you don't want to pass the number of strings, you could store it in
a global variable where the called function could find. The called
function cannot calculate the number of strings because it actually
receives a pointer. If the number of strings is not available
somewhere, then a sentinel value (like "") is a common solution.
Is there a way to achieve that?

Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.

--------------------------

#include<stdio.h>
#define NUM 20

void display(char input[][NUM])
{
char (*pc)[NUM];
int i=0;

pc=input;
for(i=0;strcmp(*pc,"")!=0;i++,pc++){
printf("%s\n",*pc);
}

}
int main()
{

char input[][NUM] = {"hi","from","ranjeet",""};

display(input);
}

----------------------

Remove del for email
Mar 12 '06 #14
>>Right now I have applied a workaround where I keep my last literal as
"" which acts as my end of literals.


Your "workaround" is a good idea and actually quite a common way of
doing this kind of thing. It's a similar idea to null-terminated
strings.

You can save having to bother with a 2D array if you use an array of
pointers:

const char *input[] = {"hi", "from", "ranjeet", NULL};

This way you might as well terminate the list with a null pointer rather
than with an empty string.

Then you can write your display function something like this:

void display(const char *input[])
{
const char **s;

assert(input);

for (s = input; *s; s++)
printf("%s\n", *s);
}
Mar 13 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2763
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
7
4787
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
9
1758
by: Bilgehan.Balban | last post by:
Hi, For a declaration such as: char * mystring = "ABCDabcd123"; Is it a linker issue where such strings are stored in C, or is it defined as part of the language definition? Is there any...
21
2280
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
10
1582
by: futhark77 | last post by:
Hi, In a program, I can declare strings that way: char *some_str = "some string"; char *some_str2 = "some other string"; I will be able to work on them very naturally with strlen and such....
74
3071
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
20
1952
by: Win Sock | last post by:
Hi All, somebody told me this morning that the following is leagal. char *a = "Hello wrold"; The memory is automatically allocated on the fly. Is this correct?
8
3446
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.