473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array of strings.

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.

I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};

But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:

some stringsome other string

Can anyone explain why both aren't equivalent?

Thanks!

Nov 1 '06 #1
10 1574
In article <11*********************@k70g2000cwa.googlegroups. com>,
fu*******@gmail.com <fu*******@gmail.comwrote:
>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.
>I thought I could also do the following:
>char *str_table[] = {
"some string",
"some other string"
};
>But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:
>some stringsome other string
>Can anyone explain why both aren't equivalent?
They -are- terminated. Try this program:
int main(void) {
char *str_table[] = {
"some string",
"some other string"
};
printf ("%s\n", str_table[0]);
return 0;
}
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 1 '06 #2
fu*******@gmail.com wrote:

(snipped)
I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};

But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:

some stringsome other string

Can anyone explain why both aren't equivalent?

Might you have made a typographical
error in your program? For example,
if you left off the comma after the first string token
then the results would be what you described because
string literals that are adjacent tokens are
concatenated. Otherwise your
code snippet looks fine.

See the difference between:

char *str_table[] = {
"some string",
"some other string"
};

and:

char *str_table[] = {
"some string"
"some other string"
};

?

--
Hope this helps,
Steven

Nov 1 '06 #3
In <11*********************@k70g2000cwa.googlegroups. com"fu*******@gmail.com" <fu*******@gmail.comwrites:
Can anyone explain why both aren't equivalent?
They are. You must have made some other error in your program. Please
post the complete program that exhibits this behavior, and we will help
you solve the error.

--
John Gordon "It's certainly uncontaminated by cheese."
go****@panix.com

Nov 1 '06 #4
at*************@gmail.com a écrit :
fu*******@gmail.com wrote:

(snipped)
I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};

But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:

Might you have made a typographical
error in your program? For example,
if you left off the comma after the first string token
then the results would be what you described because
string literals that are adjacent tokens are
concatenated. Otherwise your
code snippet looks fine.
This is it! Thank you very much!

Nov 1 '06 #5
"fu*******@gmail.com" <fu*******@gmail.comwrites:
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.

I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};

But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:

some stringsome other string

Can anyone explain why both aren't equivalent?
Almost certainly the code you're compiling and running doesn't match
the code you posted.

Try this:
================================
#include <stdio.h>
int main(void)
{
char *str_table[] = {
"some string",
"some other string"
};
printf ("%s\n", str_table[0]);
return 0;
}
================================

The output is:

some string

Then figure out how your misbehaving program differs from this one.
If you have trouble, post your code (copy-and-paste, don't re-type
it).

--
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.
Nov 1 '06 #6
fu*******@gmail.com wrote:
Hi,

In a program, I can declare strings that way:

char *some_str = "some string";
char *some_str2 = "some other string";
You can define a cstiring by using char pointer because the equal sign
is overloaded for this particular definition. The STRING, "some string"
and "some other string", will be implicitly concerted to CSTRING by the
equal sign.
>
I will be able to work on them very naturally with strlen and such.

I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};
However, if you define an array of char pointer and hope for an array
of string, there is no such overload function. You will get only an
array of char eventually.

Hope this helps
-O.Kittipot

Nov 1 '06 #7
In article <11**********************@m73g2000cwd.googlegroups .com>,
Kouisawang <KO********@gmail.comwrote:
>fu*******@gmail.com wrote:
>char *some_str = "some string";
char *some_str2 = "some other string";
>You can define a cstiring by using char pointer because the equal sign
is overloaded for this particular definition. The STRING, "some string"
and "some other string", will be implicitly concerted to CSTRING by the
equal sign.
Huh? What are you talking about? This is comp.lang.c and C does
not have any string or CSTRING type. There isn't even anything
in the BNF that corresponds to what you are talking about.

The line char *some_str = "some string"; does NOT have any
operator overloading. C does not *have* overloading. The '=' is
just part of the syntax for initialization of declarators,
and could have been replaced with any non-conflicting token.
There is no conversion going on: the meaning of a string literal
in the initialization of a declaration has different rules than
when string literals are used in code.

>char *str_table[] = {
"some string",
"some other string"
};
>However, if you define an array of char pointer and hope for an array
of string, there is no such overload function. You will get only an
array of char eventually.
The rules for initialized declarations define this use of
string literals in very similar ways to the above. Don't be mislead
by the declaration: it is not a pointer to an array of characters,
it is an array of pointers to characters, with the array elements
initialized to point to the strings listed.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Nov 1 '06 #8

Walter Roberson wrote:
In article <11**********************@m73g2000cwd.googlegroups .com>,
Kouisawang <KO********@gmail.comwrote:
fu*******@gmail.com wrote:
char *some_str = "some string";
char *some_str2 = "some other string";
You can define a cstiring by using char pointer because the equal sign
is overloaded for this particular definition. The STRING, "some string"
and "some other string", will be implicitly concerted to CSTRING by the
equal sign.

Huh? What are you talking about? This is comp.lang.c and C does
not have any string or CSTRING type. There isn't even anything
in the BNF that corresponds to what you are talking about.
Opps, sorry, I thought this is C++ group; I subscribe to many groups
for programming language, my bad :'(
>
The line char *some_str = "some string"; does NOT have any
operator overloading. C does not *have* overloading. The '=' is
just part of the syntax for initialization of declarators,
and could have been replaced with any non-conflicting token.
There is no conversion going on: the meaning of a string literal
in the initialization of a declaration has different rules than
when string literals are used in code.

char *str_table[] = {
"some string",
"some other string"
};
However, if you define an array of char pointer and hope for an array
of string, there is no such overload function. You will get only an
array of char eventually.

The rules for initialized declarations define this use of
string literals in very similar ways to the above. Don't be mislead
by the declaration: it is not a pointer to an array of characters,
it is an array of pointers to characters, with the array elements
initialized to point to the strings listed.
In the last sentence it should be "an array of pointer to char" not
"array of char"; thanks for the correction.
I read this thing from C++ book "Problem Solving with C++ by Walter
Savitch"
and if something's wrong it should be because I misunderstood
something.
By the way Im not English native speaker, sometimes my communication is
ambiguous. sorry about that.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Nov 1 '06 #9
Walter Roberson said:
In article <11*********************@k70g2000cwa.googlegroups. com>,
fu*******@gmail.com <fu*******@gmail.comwrote:
<snip>
>>But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:
>>some stringsome other string
>>Can anyone explain why both aren't equivalent?

They -are- terminated. Try this program:
....after inserting the following line:

#include <stdio.h>
>

int main(void) {
char *str_table[] = {
"some string",
"some other string"
};
printf ("%s\n", str_table[0]);
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 1 '06 #10
Kouisawang wrote:
Walter Roberson wrote:
In article <11**********************@m73g2000cwd.googlegroups .com>,
Kouisawang <KO********@gmail.comwrote:
>fu*******@gmail.com wrote:
>char *some_str = "some string";
>char *some_str2 = "some other string";
>You can define a cstiring by using char pointer because the equal sign
>is overloaded for this particular definition. The STRING, "some string"
>and "some other string", will be implicitly concerted to CSTRING by the
>equal sign.
Huh? What are you talking about? This is comp.lang.c and C does
not have any string or CSTRING type. There isn't even anything
in the BNF that corresponds to what you are talking about.

Opps, sorry, I thought this is C++ group; I subscribe to many groups
for programming language, my bad :'(
there's no such thaing as a CSTRING in C++ either.

<snip>
By the way Im not English native speaker, sometimes my communication is
ambiguous. sorry about that.
its not just your english you are saying things that are incorrect.
Microsoft stuff?

--
Nick Keighley

Nov 2 '06 #11

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

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
3
by: Morten71 | last post by:
I have a strange problem. I have a local string() var I populate this way: clmns() As String = {"InvoiceNo", "InvoiceDate"} When I call: Array.IndexOf(clmns,"InvoiceDate") I get 0 (zero) as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.