473,943 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1619
In article <11************ *********@k70g2 000cwa.googlegr oups.com>,
fu*******@gmail .com <fu*******@gmai l.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************ *********@k70g2 000cwa.googlegr oups.com"fu**** ***@gmail.com" <fu*******@gmai l.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.co m

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*******@gmai l.com" <fu*******@gmai l.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_Keit h) 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************ **********@m73g 2000cwd.googleg roups.com>,
Kouisawang <KO********@gma il.comwrote:
>fu*******@gmai l.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************ **********@m73g 2000cwd.googleg roups.com>,
Kouisawang <KO********@gma il.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************ *********@k70g2 000cwa.googlegr oups.com>,
fu*******@gmail .com <fu*******@gmai l.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

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

Similar topics

7
3282
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)) CType(local4, Short) = CType(src, Short)
3
6423
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 <stdio.h> void main(void) { char lines; int count = 0, i;
4
8858
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
7
5655
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 strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
12
5556
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 following program: #include<stdio.h> #define SIZE 1 int main()
24
3493
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
2
22629
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 read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
14
4121
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
2458
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 following. I noticed that if I set: int upper = strings.GetUpperBound(0); I never match the last element in the array (i.e. "iii")
3
1614
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 expected. If I fetch the values from the web.config this way: clmns() As String =
0
10143
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
11304
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10666
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9866
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8232
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4515
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.