What are the differences between the following methods of declaring
strings?
char string1[20] = "C++ forum";
char* string2 = "C++ forum";
I know that the first uses the array notation, whereas the second uses
pointer notation. But apart from that what are the implications /
dangers, etc. if any. 8 3467
On Sun, 05 Oct 2008 12:13:13 -0700, bintom wrote:
What are the differences between the following methods of declaring
strings?
char string1[20] = "C++ forum";
char* string2 = "C++ forum";
I know that the first uses the array notation, whereas the second uses
pointer notation. But apart from that what are the implications /
dangers, etc. if any.
As an initial pointer for you, see the C FAQ http://c-faq.com/decl/strlitinit.html
there are also other pointers there as well.
bintom wrote:
What are the differences between the following methods of declaring
strings?
char string1[20] = "C++ forum";
char* string2 = "C++ forum";
I know that the first uses the array notation, whereas the second uses
pointer notation. But apart from that what are the implications /
dangers, etc. if any.
It's not just notation: string1 is an array of chars, while string2 is a
pointer to char. A string-literal, such as "C++ forum", denotes an
object of static duration. For this reason there's no danger in writing
e.g.:
char const * f()
{
return "something" ;
}
but the following is a recipe for disaster:
char const * f()
{
char const arr[] = "something" ;
return arr ; // DON'T DO THIS!
}
In your first example you use the static duration object just as a
source to initialize string1 (in fact, it is likely that the compiler
will optimize away that source), whereas in the second one you directly
point to it (its first character, actually; and it may be the same
string literal object of the first line, or not; the implementation must
document this). Since attempting to modify the object corresponding to a
string literal yields undefined behavior you should add const:
char const * string2 = "C++ forum" ;
Omitting the const is allowed for backward-compatibility but deprecated.
Of course, you can modify string1, instead.
--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
On Oct 5, 10:13*pm, bintom <binoythomas1.. .@gmail.comwrot e:
What are the differences between the following methods of declaring
strings?
char string1[20] = "C++ forum";
char* string2 = "C++ forum";
I know that the first uses the array notation, whereas the second uses
pointer notation. But apart from that what are the implications /
dangers, etc. if any.
Hi bintom
Besides what Gennaro wrote, I can add two other differences:
1. size of string1 is 20, so as Gennaro mentioned the 10 last array
elements fill
with null character, but the size of *string2 is 10.
2. string2 is a pointer and it can points to elsewhere, but string1
can't.
Regards,
Saeed Amrollahi
On Oct 5, 8:13*pm, bintom <binoythomas1.. .@gmail.comwrot e:
What are the differences between the following methods of declaring
strings?
char string1[20] = "C++ forum";
char* string2 = "C++ forum";
I know that the first uses the array notation, whereas the second uses
pointer notation. But apart from that what are the implications /
dangers, etc. if any.
In the first case there is one object: an (initialised) array.
In the second case there are two objects: a string literal (which is a
char const[] array) and a pointer to it.
--
Max eb********@gmai l.com wrote:
On Oct 5, 10:13 pm, bintom <binoythomas1.. .@gmail.comwrot e:
>What are the differences between the following methods of declaring strings?
char string1[20] = "C++ forum"; char* string2 = "C++ forum";
<nitpick1>
This /defines/ strings.
</nitpick1>
[...]
1. size of string1 is 20, so as Gennaro mentioned the 10 last array
elements fill with null character, but the size of *string2 is 10.
<nitpick2>
The size of '*string2' is 1 (as it points to a 'char'). The size
of 'string2', OTOH, is the size of a 'char*' (4 on my system).
</nitpick2>
[...]
Saeed Amrollahi
Schobi
On Oct 6, 11:59*am, Hendrik Schober <spamt...@gmx.d ewrote:
ebony.s...@gmai l.com wrote:
On Oct 5, 10:13 pm, bintom <binoythomas1.. .@gmail.comwrot e:
What are the differences between the following methods of declaring
strings?
char string1[20] = "C++ forum";
char* string2 = "C++ forum";
* <nitpick1>
* This /defines/ strings.
* </nitpick1>
[...]
1. size of string1 is 20, so as Gennaro mentioned the 10 last array
elements fill with null character, but the size of *string2 is 10.
* <nitpick2>
* The size of '*string2' is 1 (as it points to a 'char'). The size
* of 'string2', OTOH, is the size of a 'char*' (4 on my system).
* </nitpick2>
[...]
* Saeed Amrollahi
* Schobi
Sorry for some confusion. I mean in array case the size of string1 is
20, but in pointer case the size of
string literal which string2 points is 10 (9 bytes for "C++ forum" and
an additional byte
for '\0')
- Saeed Amrollahi
James Kanze wrote:
On Oct 5, 9:13 pm, bintom <binoythomas1.. .@gmail.comwrot e:
>What are the differences between the following methods of declaring strings?
>char string1[20] = "C++ forum";
This declares an array of char, initialized with
{
'C', '+', '+', ' ', 'f',
'o', 'r', 'u', 'm', '\0',
'\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0'
} ;
Adding some historical information (usually a special quality of
yours:-), the fact that the last elements are initialized to zero was
clarified with C90's TC2.
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/tc2.htm>
(the fix to subclause 6.5.7). C++03 isn't in sync (i.e., it leaves the
case as uncertain as the original ISO C did) and, last time I checked,
the working draft isn't, either.
--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
In article
<82************ *************** *******@r15g200 0prd.googlegrou ps.com>,
Saeed Amrollahi <s_*********@ya hoo.comwrote:
On Oct 6, 11:59*am, Hendrik Schober <spamt...@gmx.d ewrote:
ebony.s...@gmai l.com wrote:
On Oct 5, 10:13 pm, bintom <binoythomas1.. .@gmail.comwrot e:
>What are the differences between the following methods of declaring
>strings?
>char string1[20] = "C++ forum";
>char* string2 = "C++ forum";
* <nitpick1>
* This /defines/ strings.
* </nitpick1>
[...]
1. size of string1 is 20, so as Gennaro mentioned the 10 last array
elements fill with null character, but the size of *string2 is 10.
* <nitpick2>
* The size of '*string2' is 1 (as it points to a 'char'). The size
* of 'string2', OTOH, is the size of a 'char*' (4 on my system).
* </nitpick2>
[...]
* Saeed Amrollahi
* Schobi
Sorry for some confusion. I mean in array case the size of string1 is
20, but in pointer case the size of
string literal which string2 points is 10 (9 bytes for "C++ forum" and
an additional byte
for '\0')
I disagree. For example,
const char* s1 = "012";
const char* s2 = "12";
s2 might equal s1+1 if the compiler shares the underlying character
array between the literals. In that case, s2 at the very least points to
the second element a 4-char array. I don't think you can really even
know the ultimate size of the underlying array (and thus what elements
can be accessed without invoking undefined behavior). There could even
be characters past the apparent end:
const char* s3 = "12\0Z"; //s3=s1+1 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Philippe C. Martin |
last post by:
I apologize in advance for launching this post but I might get enlightment
somehow (PS: I am _very_ agnostic ;-).
- 1) I do not consider my intelligence/education above average
- 2) I am very pragmatic
- 3) I usually move forward when I get the gut feeling I am correct
- 4) Most likely because of 1), I usually do not manage to fully explain 3)
when it comes true.
- 5) I have developed for many years (>18) in many different environments,...
|
by: Sergey Ilinsky |
last post by:
Well, I've been working with JS for three years and have a great
experience here.
But! I still have no really acceptable answer to the following
question:
What is the principle difference between declaring methods/properties
in the constructor function body and via prototypes.
Are there any real GURUs? Let's discuss the issue.
P.S. Currently I am working on XUL implementation for IE, so there is
|
by: arekkusu |
last post by:
Hello, I have the following problem:
when declaring hex data in C, you typically do something like:
const char = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };
This is quite verbose and makes the source hard to read.
An alternative is something like
const char = "deadbeef01020304";
|
by: rtilley |
last post by:
s = ' qazwsx '
# How are these different?
print s.strip()
print str.strip(s)
Do string objects all have the attribute strip()? If so, why is
str.strip() needed? Really, I'm just curious... there's a lot don't
fully understand :)
|
by: Zytan |
last post by:
What is the difference between these two lines?
Dim args As Object() = New Object() {strText}
Dim args As Object() = {strText}
args seems usuable from either, say, like so:
Me.Invoke(delegate, args)
Zytan
| |
by: edson |
last post by:
Greetings.
My microcontroller program uses arrays of strings. I want to store the
strings AND their pointers in ROM. I am able to initialize the arrays so
that the strings are put in ROM, but the compiler puts the pointers in
RAM. I would appreciate advice on how to initialize it so that both
pointers and strings are in ROM.
Here is the declaratin I use.
const char *myvariables = {"var1", "var2", "vr3", "variable4", "v5"};
|
by: Stefan Scholl |
last post by:
After an hour searching for a potential bug in XML parsing
(PyXML), after updating from 2.4 to 2.5, I found this one:
$ python2.5
Python 2.5 (release25-maint, Dec 9 2006, 14:35:53)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
|
by: Jim Johnson |
last post by:
what's the difference when declare a struct with typedef or NO
typedef?
================================
typedef struct {
.... etc
} SetupRecord;
|
by: JLORA3659 |
last post by:
dsPlcTagDataSet = new DataSet();
dsPlcTagDataSet.ReadXml(Application.StartupPath + "\\" + "PlcTag.xml");
in the xml file there is a row "iWord" that I need to sort later on but this row is comming into the dataset as a string and is not sorting properly. I need to sort it as integer. how do I get to read the xml file into the dataset declaring the datatype of the rows?, so strings are passed into the dataset as strings, integers as integers,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |