473,411 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

string declaration(s)

i was thinking of declaring a string . .

two ideas came in my mind

i) char a[10] = "ilovemyc";

ii) char *p = "ilovemyc";

how can we differentiate the above two declarations?
Nov 14 '05 #1
5 20466
Gautam <ga********@yahoo.com> scribbled the following:
i was thinking of declaring a string . . two ideas came in my mind i) char a[10] = "ilovemyc"; ii) char *p = "ilovemyc"; how can we differentiate the above two declarations?


Did you read the comp.lang.c FAQ? The main difference between the two
declarations is that the first one holds the bytes in the string in
the variable itself, while the second one merely holds a pointer
pointing to the place where the bytes are. When used as a value, the
variable "a" in the first declaration "decays" into a pointer to the
first byte.
In practice, there are two differences that you should care about:
the first guarantees the string to be modifiable, the second doesn't.
You can freely assign the second to point anywhere you want, but
with the first, you're stuck with your statically reserved space and
can't reassign it.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
Nov 14 '05 #2
Gautam schrieb:
i was thinking of declaring a string . .

two ideas came in my mind

i) char a[10] = "ilovemyc";

ii) char *p = "ilovemyc";

how can we differentiate the above two declarations?


sizeof(a) = 10
sizeof(p) = 4 (probably)

however there's another way to declare and define a string:

char b[] = "ilovemyc";

sizeof(b) = 9
Nov 14 '05 #3
In <44*************************@posting.google.com> ga********@yahoo.com (Gautam) writes:
i was thinking of declaring a string . .

two ideas came in my mind

i) char a[10] = "ilovemyc";

ii) char *p = "ilovemyc";

how can we differentiate the above two declarations?


By reading our C book and understanding the difference between an array
and a pointer. If the difference is still unclear after this exercise,
there is always the comp.lang.c FAQ.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

"Gautam" <ga********@yahoo.com> wrote in message
news:44*************************@posting.google.co m...
i was thinking of declaring a string . .

two ideas came in my mind

i) char a[10] = "ilovemyc";

ii) char *p = "ilovemyc";

how can we differentiate the above two declarations?


In the first case, you are declaring a 10-element array of char and
initializing it's contents with the string "ilovemyc"; it is equivalent to
typing

char a[10] = {'i','l','o','v','e','m','y','c',0,0};

You may modify the contents of this array.

In the second case, you are declaring a pointer to char and initializing it
to point to the base of a 9-element array of char that is stored in
non-writable memory; it is equivalent to typing

char *p = 0x........

where 0x........ is the address of the first element of the string.

You may not modify the contents of the string being pointed to.
Nov 14 '05 #5
Gautam wrote:

i was thinking of declaring a string . .

two ideas came in my mind

i) char a[10] = "ilovemyc";
ii) char *p = "ilovemyc";

how can we differentiate the above two declarations?


By the source characters that separate "char" and "=".

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6

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

Similar topics

4
by: Jeff Williams | last post by:
This doesn't compile: const char** ids = { "aaa", "bbb", "ccc" }; I hope the above illustrates what I am trying to do, if it is possible, what
12
by: Riley DeWiley | last post by:
I am looking for a graceful way to declare a string const that is to be visible across many files. If I do this: //----hdr.h const char * sFoo = "foo"; //file.cpp
5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
7
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...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
12
by: Terry | last post by:
If the string object contain like below: string Mystring = " "; What function should be used for checking the "Mystring" is no char inside? Thanks Terry
24
by: v4vijayakumar | last post by:
why the following string, 'str' is read-only? char *str = "test string"; anyhow 'str' needs to be in memory. do you think, making 'str' red-only would gain performance? or, it is right?
31
by: Peter Michaux | last post by:
Hi, I want to know the name of an object's constructor function as a string. Something like this <script type="text/javascript"> function Foo(){}; var a = new Foo(); alert('"' +...
5
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...
4
by: Chris Forone | last post by:
hello group, why have i to bracket the second ctor param in the following example? thx & hand, chris #include <fstream> #include <iterator> int main()
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
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...
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
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...

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.