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! 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 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
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 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!
"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. 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
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
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
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)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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))
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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: 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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |