473,473 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string initialization

1 New Member
Hi, all

What is the difference of following char initializations.
char *p="Hello";
char p[]="Hello";
Oct 13 '07 #1
3 1945
Meetee
931 Recognized Expert Moderator Contributor
Hi, all

What is the difference of following char initializations.
char *p="Hello";
char p[]="Hello";
Hi,

In the first case if *p is assigned to some other value the allocate memory can change, where as in the second case 5 bytes are allocated to the variable p which is fixed.

Regards
Oct 13 '07 #2
oler1s
671 Recognized Expert Contributor
Let me clarify. The first method gets you a pointer to a constant literal. You can't modify it. Well, you could always point p to something else, but then you lose the reference to the string literal.

The second method gets you an actual character array, and then initializes it.
Oct 13 '07 #3
mattmao
121 New Member
I reckon the difference is: with the first option you allocate new memory every time you modify the content, whereas with the second option, you allocate the memory address once and then only change the content start from that address.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     char *array1="ABCDEFG";
  6.     char array2[]="ABCDEFG";
  7.     int i;
  8.  
  9.  
  10.     printf("array1 contains:%s\n", array1);
  11.     printf("array2 contains:%s\n", array2);
  12.  
  13.     array1="XXXXXXX";
  14.  
  15.     for(i=0; i<7; i++)
  16.     {
  17.         array2[i]='X';
  18.     }
  19.  
  20.     printf("array1 contains:%s\n", array1);
  21.     printf("array2 contains:%s\n", array2);
  22.  
  23.     return 0;
  24. }
Here is the result:
Expand|Select|Wrap|Line Numbers
  1. Mattmao@(dspp) 181$gcc arrays.c
  2. Mattmao@(dspp) 182$./a.exe
  3. array1 contains:ABCDEFG
  4. array2 contains:ABCDEFG
  5. array1 contains:XXXXXXX
  6. array2 contains:XXXXXXX
  7. Mattmao@(dspp) 183$
Oct 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Thomas | last post by:
I have a class that uses environment variables as part of its initialization. The following used to work using the Lucent SCL but now fails with STL. string env = getenv("ENV_VALUE"); //...
9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
9
by: Niels Dekker - no reply address | last post by:
Are all the following initializations semantically equivalent? wchar_t a = {L'\0'}; wchar_t b = {'\0'}; wchar_t c = {0}; wchar_t d = {}; If so, why don't we all use an empty initializer...
8
by: Baloff | last post by:
Hello I am not sure why my compiler will not initialize string e1("sam"); and will initialize string e1 = "sam"; here is my code and the error. thanks alot
6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
5
by: Mark | last post by:
Hello - I'm using VB .Net 2003 connected to Access 2002 via the OLE DB Jet Engine driver. When I open the connection in the Server Explorer, it works fine. But when I build the app I get this...
8
by: KRoy | last post by:
I have a password stored in the Registry encrypted using System.Security.Cryptography DES Algorithm. I supplied it a password and a Initialization Vector. I am trying to decrypt it using the...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
4
by: wesbland | last post by:
>From my understanding, when a string is stored in VB.NET and you look at it in the debugger, it has a quote on both sides to signify that it is a string as opposed to a char or int or whatever. ...
0
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,...
0
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...
0
Oralloy
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,...
1
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...
0
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...
0
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,...
1
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.