473,322 Members | 1,526 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,322 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 20463
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.