Array of strings.

F

futhark77

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!
 
W

Walter Roberson

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;
}
 
A

attn.steven.kuo

(e-mail address removed) 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"
};

?
 
J

John Gordon

In said:
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.
 
F

futhark77

(e-mail address removed) a écrit :
(e-mail address removed) 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!
 
K

Keith Thompson

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).
 
K

Kouisawang

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
 
W

Walter Roberson

(e-mail address removed) wrote:
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.
 
K

Kouisawang

Walter said:
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.
 
R

Richard Heathfield

Walter Roberson said:
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 said:
int main(void) {
char *str_table[] = {
"some string",
"some other string"
};
printf ("%s\n", str_table[0]);
return 0;
}
 
N

Nick Keighley

Kouisawang said:
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.

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?
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top