Re: chars



Keith Thompson wrote:
user923005 <dcorbit@xxxxxxxxx> writes:
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".

Yes. I would add "in memory" for the string.

So a pointer may *point* to a string, but it cannot *be* a string.
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.

Indeed.

So this is a string:

char *foo = "I am a string";

No, foo is a pointer to a string.

No, foo is a pointer to char, the first element in the anonymous array.

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.

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').

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.

Indeed.

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."
.



Relevant Pages

  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: problem with function
    ... Yes I did miss type the psz example, it is a pointer to char (not pointer to ... companies using Hungarian, and when I looked into one standard, I found ... >> If you intended copying the string, you need to use the strcpy function. ... > Isn't 'psz' a pointer a an array of characters containing a string? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: new IL: C (sort of...).
    ... C doesn't need a string type... ... variant of PL/1 which was very Pascal-ish. ... - C does implement an array declaration. ... effectively converted into a pointer that can be used with the offset ...
    (comp.lang.misc)
  • Re: Separating directory from file name: Code Review!
    ... > return a null pointer in this example, ... > handed a string that does not contain a '/' character. ... > memory for an array of characters and have it all zeroed, ... > calloc() will do both operations in one call. ...
    (comp.lang.c)