string declaration(s)

G

Gautam

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?
 
J

Joona I Palaste

Gautam said:
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.
 
S

Sascha Springer

Gautam said:
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
 
D

Dan Pop

In said:
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
 
J

John Bode

Gautam said:
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.
 
C

CBFalconer

Gautam said:
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 "=".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top