Re: chars
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Tue, 08 Jan 2008 21:20:04 -0500
Keith Thompson wrote:
user923005 <dcorbit@xxxxxxxxx> writes:Yes. I would add "in memory" for the string.On Jan 7, 12:20 pm, "Bill Cunningham" <nos...@xxxxxxxxx> wrote:[...]Do I sound like I'm getting it right now?Not sure. A string (in C) is a pointer to an array {or an array} of
zero or more characters with a terminating zero.
No, a pointer is not a string, and an array is not a string.
A string is, by definition, "a contiguous sequence of characters
terminated by and including the first null character", and a "pointer
to a string" is "a pointer to its initial (lowest addressed)
character".
So a pointer may *point* to a string, but it cannot *be* a string.Indeed.
And an array may *contain* a string, but it cannot *be* a string.
The term "string" refers to a data layout, not a data type.
No, foo is a pointer to char, the first element in the anonymous array.So this is a string:
char *foo = "I am a string";
No, foo is a pointer to a string.
No. An array in C has no defined value. The value of bar[0] is '1' and the value of bar is '&bar[0]', the address (of type char*) of the first element of the array (the '1').and this is a string:
char bar[4] = "123";
bar, an array object, contains a string. I suppose you could say that
the value of bar is a string.
Indeed.but this is not a string, it's just an array because it does not have
a terminating zero:
char barbad[4] = "1234";
and neither is this:
char badbar[3] = {'a', 'b', 'c'};
Correct, neither barbad nor badbar contains a string.
I think we overload 'array' a little. Correctly..
char arr[80] = "Hello";
char *arp = malloc(80);
strcpy(arp, arr);
...barring error, it is arr which is the array and arp a pointer to contiguous memory. And 'strcmp(arr, arp) == 0'. Both are strings.
--
Joe Wright
"If you think Health Care is expensive now, wait until it's free."
.
- Follow-Ups:
- Re: chars
- From: Keith Thompson
- Re: chars
- Prev by Date: Re: C Bibliography
- Next by Date: Re: Which Visual C++?
- Previous by thread: Re: chars
- Next by thread: Re: chars
- Index(es):
Relevant Pages
|