plz tell me the difference

J

jeniffer

char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences
 
R

Rod Pemberton

jeniffer said:
char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences

Questions 1.32 and 6.2 and their answers should help. If not, tell us why
not.
http://www.c-faq.com


Rod Pemberton
 
A

AG

char *a declares a pointer to a string. So, to say char *a = "this is
a string", puts the address of the first letter of the string into the
pointer a. So, that first "t" is located somewhere in memory, the
pointer a contains the address.
char *a="this is a string";
If you wrote printf("char = %d",a); you would see the address
where a has been stored
If you wrote printf("char = %c",*a); you would see what value was
at that address.

flank
 
C

code break

"char *a="this is a string"
Here the string literals " this is a string " is stored in data
segment & you are making pointer variable "a" to point this location
but "a" can be changed to point to any other location in a program.

char a[]="this is a string";

but here you can't change pointer of the variable "a" to other
location in the program.

i hope this help you....
 
C

code break

"char *a="this is a string"
Here the string literals " this is a string " is stored in data
segment & you are making pointer variable "a" to point this location
but "a" can be changed to point to any other location in a program.

char a[]="this is a string";

but here you can't change pointer of the variable "a" to other
location in the program.

i hope this help you....
 
V

Vladimir Oka

AG opined:
char *a declares a pointer to a string. So, to say char *a = "this
is a string", puts the address of the first letter of the string into
the pointer a. So, that first "t" is located somewhere in memory,
the pointer a contains the address.

So far so good, albeit somewhat less than precise...
If you wrote printf("char = %d",a); you would see the address
where a has been stored

No, you wouldn't.

You'd see (if you're lucky) whatever your implementation thinks a
pointer interpreted as an `int` looks like. In fact, you get Undefined
Behaviour, as "%d" tells `printf()` to expect an `int`, when in fact
you're passing `char *`. UB essentially means: all bets are off, duck
or grouse!

Also, since you didn't terminate output with '\n', you may end up
getting absolutely no output at all.

You could output a pointer (as represented by your implementation) if
you used "%p" instead. How, and whether it relates to memory
addresses, is another matter.
If you wrote printf("char = %c",*a); you would see what value
was at that address.

This will work as expected, yes (apart from missing '\n', that is).


--
The trouble with a kitten is that
When it grows up, it's always a cat
-- Ogden Nash.

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
V

Vladimir Oka

code break opined:
"char *a="this is a string"
Here the string literals " this is a string " is stored in data
segment & you are making pointer variable "a" to point this location
but "a" can be changed to point to any other location in a program.

C knows not of "data segment", nor does it say where exactly the string
is stored. It's important to note that you're not allow to modify it.
char a[]="this is a string";

but here you can't change pointer of the variable "a" to other
location in the program.

Wrong.

In this case `a` is not a pointer, but an array. It is correct that you
can't make `a` refer to something else, but that is true of any
variable you declare. As it is not declared `const` the contents of
this array can be modified.

Also, in the line above, the string is used to initialise array `a`
(including the terminating '\0'). It is not otherwise accessible from
the program.
i hope this help you....

Actually, no.
 
J

John Bode

jeniffer said:
char *a="this is a string";
char a[]="this is a string";
first is a pointer while second is an array.Tell me more differences

I'm going to rewrite this a little:

char *ptr="this is a string";
char arr[]="this is a string";

In the above code, we're talking about 3 distinct objects: the string
literal "this is a string", the pointer ptr, and the array arr. The
following hypothetical memory map shows the state of each object after
initialization (best viewed with a fixed-size font):

Item Address Contents
---- ------- --------
literal 0x0800 "this is a string"
ptr 0x1000 0x0800
arr 0x2000 "this is a string"

The string literal has its own address in memory, and may not be
writable (i.e., you may not be allowed to write to the bytes between
0x0800 and 0x0811; assume that you can't).

The pointer ptr contains the address of the string literal. Since the
literal may not be writable, you may not write to *ptr or ptr[n];
however, you can assign a new value to ptr (i.e., ptr=NULL,
ptr=&arr[0], ptr=malloc(16), etc.), and if this new value points to
writable memory, you may then also write to *ptr or ptr[n].

The array arr contains a *copy* of the contents of the string literal.
The array contents are writable, so you can write to *arr or arr[n];
however, the array object itself is not writable, so you may not write
arr=NULL, or arr="this is a string", or arr=p, etc.
 
K

Keith Thompson

AG said:
char *a declares a pointer to a string. So, to say char *a = "this is
a string", puts the address of the first letter of the string into the
pointer a. So, that first "t" is located somewhere in memory, the
pointer a contains the address.

Strictly speaking, char *a declares a pointer to a char, not a pointer
to a string. That pointer-to-char can, and commonly does, point to
the first character of a string (a sequence of chars terminated by a
'\0'), and can be used to access the entire string. Referring to a as
a "pointer to a string" is a common verbal shorthand, and a useful
one, but it's not literally correct.

More generally, it's common to use a pointer-to-FOO to point to the
first element of an array of FOO:

FOO arr[10];
FOO *ptr = arr; /* or, equivalently, FOO *ptr = &arr[0]; */

but it's *also* possible to have a pointer to an array, which is
distinct from a pointer to the array's first element:

FOO (*arr_ptr)[10] = &arr;

Calling ptr a "pointer to an array" blurs this distinction.

Incidentally, pointers to arrays are rarely useful. It's usually more
useful to have a pointer to the first element of an array, and keep
track of the length separately rather than hard-wiring a fixed length
into an pointer-to-array type.
 
P

pete

Keith said:
Strictly speaking, char *a declares a pointer to a char, not a pointer
to a string. That pointer-to-char can, and commonly does, point to
the first character of a string (a sequence of chars terminated by a
'\0'), and can be used to access the entire string. Referring to a as
a "pointer to a string" is a common verbal shorthand, and a useful
one, but it's not literally correct.

(a) is a "pointer to a string" according to the definition
in the standard for "pointer to a string".

I don't think it would be wrong to say that
char *a = "this is a string";
declares a pointer with a value unequal to NULL.
The point being, that there are other aspects
to an initialised declaration, besides the type.
 
K

Keith Thompson

pete said:
(a) is a "pointer to a string" according to the definition
in the standard for "pointer to a string".

You're right. C99 7.1.1 says:

A _pointer to a string_ is a pointer to its initial (lowest
addressed) character.

If you interpret the phrase literally, it's a useful shorthand but not
strictly correct. Since the standard provides a definition for it, I
concede the point.
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top