char *str1 = "Hello";
char arr1[] = { "Hello" };
char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
Is it legal to modify str1, arr1 and arr2 ? 52 3050
1.char *str1 = "Hello";
Generally the string literals would be stored in the read only data
section of the resulting object code, post compilation. So, modifying
the contents of the memory location addressed by "str1" would result in
seg fault. This might be compiler dependent though.
Although, "str1" itself can be modified.
The following is valid:
char *str1 = "Hello";
str1 = 0; // Allowed
2.char arr1[] = { "Hello" };
Contents of the memory addressed by "arr1" can be modified. "arr1"
itself cannot be modified.
That is the following is invalid:
char arr1[] = { "Hello" };
arr1 = 0; // Not allowed.
3. char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
Same comments as above for "arr2" hold true here.
-Sriram.
junky_fel...@ya hoo.co.in wrote: char *str1 = "Hello"; char arr1[] = { "Hello" }; char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
Is it legal to modify str1, arr1 and arr2 ?
This is a FAQ. Read Question 1.32 http://www.eskimo.com/~scs/C-faq/faq.html
Krishanu ju**********@ya hoo.co.in wrote: char *str1 = "Hello"; char arr1[] = { "Hello" }; char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
Is it legal to modify str1, arr1 and arr2 ?
FAQ 6.2 should be a good place to start.
To quote the draft:
The declaration
char arr1[] = { "Hello" };
defines a 'plain' char array object (of unknown size) `arr1' whose
elements
are initialized with character string literal. Array contents
are modifiable. This declaration is identical to
char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
as well as
char arr3[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
OTOH,
char *str1 = "Hello";
defines str1 with type 'pointer to char' and initializes it
to point to an object with type 'array of char' with length
6 whose elements are initialized with a character string literal.
If an attempt is made to use p to modify the contents of the array,
the behavior is undefined. ju**********@ya hoo.co.in wrote: char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
This case has no string literal, in fact no string at all. It's an
array of five characters without a null terminator.
Brian
"Default User" <de***********@ yahoo.com> wrote in message
news:3l******** *****@individua l.net... ju**********@ya hoo.co.in wrote:
char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
This case has no string literal, in fact no string at all. It's an array of five characters without a null terminator.
No, it's an array of six characters and it DOES have a null terminator.
Mark wrote: char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
This case has no string literal, in fact no string at all. It's an array of five characters without a null terminator.
No, it's an array of six characters and it DOES have a null terminator.
% cat foo.c
#include <stdio.h>
int main(void)
{
char foo[] = { 'H', 'e', 'l', 'l', 'o' };
printf("%u\n", (unsigned)(size of foo));
return 0;
}
% gcc -Wall -ansi -pedantic foo.c
% ./a.out
5
Mark wrote: "Default User" <de***********@ yahoo.com> wrote in message news:3l******** *****@individua l.net... ju**********@ya hoo.co.in wrote:
char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
This case has no string literal, in fact no string at all. It's an array of five characters without a null terminator.
No, it's an array of six characters and it DOES have a null terminator.
Wanna bet?
Brian
On 9 Aug 2005 02:09:27 -0700, in comp.lang.c , ju**********@ya hoo.co.in wrote: char *str1 = "Hello";
you can modify str1 (change where it points) but not what it currently
points to.
char arr1[] = { "Hello" }; char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
These are identical objects. You can modify what is inside them, but
not change arr1 or arr2.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mark McIntyre wrote: On 9 Aug 2005 02:09:27 -0700, in comp.lang.c , ju**********@ya hoo.co.in wrote:
char *str1 = "Hello";
you can modify str1 (change where it points) but not what it currently points to.
char arr1[] = { "Hello" }; char arr2[] = { 'H', 'e', 'l', 'l', 'o' };
These are identical objects. [...]
To quote Default User, elsethread: "Wanna bet?"
-- Er*********@sun .com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Don Starr |
last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?
For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the result?)
char x = "abcd";
printf( "%d\n", sizeof( x ) );
-Don
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have type of "const char *".
Right? But why does the compiler I am using allow s to be modified, instead
of generating compile error?
|
by: Janice |
last post by:
char* line = "abcd";
How to convert the line to upper case and print?
Any option for printf to do this?
Thanx
|
by: junky_fellow |
last post by:
what would be the output for the following piece of code ?
if ( "hello" == "hello" )
printf("True\n");
else
printf("False\n");
What is the reason for that ?
|
by: copx |
last post by:
Can you / are you supposed to free() string literals which are no longer
needed?
In my case I've menu construction code that looks like this:
menu_items = list_new();
list_add(menu_items, "Item 1");
list_add(menu_items, "Item 2");
list_add(menu_items, "Item 3");
menu_create(menu_items, false)
| |
by: Dead Loop |
last post by:
Hi all,
I'm a beginner and my question is:
Are there any differences between
char *p = "Hello, world!";
and
const char *p = "Hello, world!";
?
|
by: arnuld |
last post by:
i tried to output these 2 to the std. output:
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
|
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire |
last post by:
Hello all,
I'm learning C and I still am struggling to understand some basic
concepts. For example, I read in the standard that with functions such
as strcpy, 'If copying takes place between objects that overlap, the
behavior is undefined.' But how can I be sure that they don't overlap?
For example, if I write this:
char string1 = "overlap";
|
by: polas |
last post by:
Good morning,
I have a quick question to clear up some confusion in my mind. I
understand that using a string literal in a declaration such as char
*p = "string literal" declares a pointer to memory holding the string
and the string might very well be held in read only memory.
However, I am sure that I read somewhere that the declaration char a
= "string literal", even though a is an array (and I understand the
differences between...
|
by: lithiumcat |
last post by:
Hi,
I'm not yet very confident in my use of standard terminology, so
please be kind if I'm mis-calling something, I will do my best no to
make it again once pointed out.
I'm wondering what is the lifetime or a compile-time string constant,
I think that is what is called the storage duration of a string
litteral.
|
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: 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: 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: 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...
| |