When you have compile-time strings like:
"The file is of unknown format."
What kind of variables do you use to store it? At the moment I'm using:
char const str[] = "The file is of unknown format.";
Do some of you use:
const char* const str = "The file is of unknown format.";
-Tomás 31 1808
* Tomás: When you have compile-time strings like:
"The file is of unknown format."
What kind of variables do you use to store it? At the moment I'm using:
char const str[] = "The file is of unknown format.";
Do some of you use:
const char* const str = "The file is of unknown format.";
Perhaps some do, but I think it would be less than smart to discard
information for such raw data, and to write more to do that.
There is also the question of whether to write
std::string const str = "The file is of unknown format";
and at least for one learning the language I think it's a good idea to
use std::string exclusively, and not delve into the realm of pointers
and raw arrays, simply not get _used_ to the low-level features.
However, there are some scenarios where the std::string constant isn't
possible or advisable.
And of course the strings may instead be embedded in some resource data
structure in the program or in a dynamic library, for
internationaliz ation, but that is platform-dependent and tool-dependent
and therefore outside the scope of discussion in this group.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Tomás wrote: When you have compile-time strings like:
"The file is of unknown format."
What kind of variables do you use to store it? At the moment I'm using:
char const str[] = "The file is of unknown format.";
Do some of you use:
const char* const str = "The file is of unknown format.";
-Tomás
As far as I know there is no diffrence between them.
I might be ignorant as hell. Is exactly the same.
char astr[]="array hello";
char* pstr="pointer hello";
now you have two zeroterminated memory areas and two pointers.
The diffrence is in literals, as below:
char* pstr2={'e','r', 'r','o','r'};//illegal
char astr2[]=pstr;;//illegal
char astr3[]={'e','r','r',' o','r','\0'};//legal
char astr3[]={'e','r','r',' o','r'};//legal, but not a string since no
zero is appended
Strings are represented by arrays, they are not arrays.
/Jesper
<je****@alphaca sh.se> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
Tomás wrote: When you have compile-time strings like:
"The file is of unknown format."
What kind of variables do you use to store it? At the moment I'm using:
char const str[] = "The file is of unknown format.";
Do some of you use:
const char* const str = "The file is of unknown format.";
-Tomás
As far as I know there is no diffrence between them.
I might be ignorant as hell. Is exactly the same.
char astr[]="array hello";
char* pstr="pointer hello";
---
The above two are not same.
pstr[1] = 'a'; // Illegal
astr[1] = 'a'; // Legal
Sharad
Sharad Kala wrote:
--- The above two are not same. pstr[1] = 'a'; // Illegal astr[1] = 'a'; // Legal
Sharad
No thats false.
pstr[1] = 'a'; is legal, its not safe, but it is legal.
standard behaviour is
pstr[1] = 'a';
compiles to
*(pstr+(sizeof( *pstr)))='a';
/Jesper
* je****@alphacas h.se: Sharad Kala wrote: --- The above two are not same. pstr[1] = 'a'; // Illegal astr[1] = 'a'; // Legal
Sharad No thats false. pstr[1] = 'a'; is legal, its not safe, but it is legal. standard behaviour is pstr[1] = 'a'; compiles to *(pstr+(sizeof( *pstr)))='a';
In the context (which you snipped) the assignment is not valid, because
the pointer points to a literal string constant.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* je****@alphacas h.se: pstr[1] = 'a'; compiles to *(pstr+(sizeof( *pstr)))='a';
Not in general (and it is the general you're trying to illustrate?).
It "compiles to", in the sense of being equivalent with,
*(pstr+1) = 'a';
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? je****@alphacas h.se wrote: As far as I know there is no diffrence between them. I might be ignorant as hell. Is exactly the same. char astr[]="array hello"; char* pstr="pointer hello";
At this moment, yes, you are ignorant as hell.
pstr resides in read-only memory and trying to modify it
is UB, whereas modifying astr is ok.
HTH,
- J.
Alf P. Steinbach wrote: * je****@alphacas h.se: pstr[1] = 'a'; compiles to *(pstr+(sizeof( *pstr)))='a';
Not in general (and it is the general you're trying to illustrate?).
It "compiles to", in the sense of being equivalent with,
*(pstr+1) = 'a';
yes its the general I'm after. And you are misstaken,
because operator+(char* ,int) defines the size of the element.
The code below works fine. (borland, ansicompliant,f orce c++)
class CClass
{
int i[2];
public:
CClass& operator=(int in){i[0]=in;i[1]=in;}
};
int u[]={1,2,3};
CClass c[3];
template <class T>
void MyPoint(T* arg)
{
arg[1]=3;
}
int main() {
MyPoint(u);
MyPoint(c);
}
I will give on the point concerning the const declarations that
I sadly snipped away. Sorry about that. My only excuse is that you used
the variable names astr and pstr which i introduced and did not declare
const.
Sorry.
But I will remain adamant that the Rvalues are the same type. :-)
BTW. this is the first time I have ever seen the syntax:
const char * const str="string";
Does that second const induce the area protection?
And if so, how would I protect an array the same way? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: agent349 |
last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing...
|
by: Ian Todd |
last post by:
Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char to read in 1000 lines to the array( i think!!).
But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array?
I have seen char **array etc. At first I tried char *array but I think
that gives 50...
|
by: Steve |
last post by:
I want an initializer for an array of pointers to arrays of strings.
So I can do something like this:
const char *t1 = { "a", "b", "c", NULL };
const char *t2 = { "p", "q", NULL };
const char *t3 = { "w", "x", "y", "z", NULL };
const char **test = { t1, t2, t3, NULL };
I was wondering whether the is a more elegant way of writing such an
|
by: swarsa |
last post by:
Hi All,
I realize this is not a Palm OS development forum, however, even though
my question is about a Palm C program I'm writing, I believe the topics
are relevant here. This is because I believe the problem centers
around my handling of strings, arrays, pointers and dynamic memory
allocation. Here is the problem I'm trying to solve:
I want to fill a list box with a list of Project Names from a database
(in Palm this is more...
|
by: raphfrk |
last post by:
I have the following code:
char buf;
printf("%lp\n", buf);
printf("%lp\n", &buf);
printf("%lp\n", buf);
printf("%lp\n", buf);
printf("%d\n", buf-buf);
| |
by: Giff |
last post by:
Hi
I have this problem that I can't solve, I know it shouldn't be hard but
I'm not a good coder and I'm going crazy tonight...
I have a txt file that goes like this:
string1 string2
string3 string4
|
by: Aris |
last post by:
A few days ago I asked from you how to join a string like "file"
A number that change values from 1 to 5 and another string like ".txt"
to have a result like "file1.txt","file2.txt" and so on
and you gave me the right answer.
But now I got another problem.
|
by: mike3 |
last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms-
windows.programmer.win32
since there's Windows material in here as well as questions related to
standard
C++. Not sure how that'd go over at just comp.lang.c++. If one of
these groups is
too inappropriate, just take it off from where you send your replies.)
Hi.
|
by: bowlderyu |
last post by:
Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c
#include <stdio.h>
#define LEN 20
struct info {
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |