On Sunday 17 April 2005 12:22, Colin Carter wrote:
On Monday 18 April 2005 00:09, Jerry Feldman wrote:
On Sat, 16 Apr 2005 13:17:22 +1000
Colin Carter
wrote: Yes. In particular FORTRAN had a nice standard of first BYTE holding size.
Now it "appears" that C++ does not need to have this number because the string ends with a NULL (or strictly correct being a NUL. I hate
Bill Gates and mates just changing definitions.)
NULL is a macro in C referring to the null pointer. The string termination is simply a null byte, not NULL.
A macro you say! This I didn't know. I think of a NUL as being the ASCII character/byte being a full house of zero bits. The same as used at the end of a C string. You know, like ACK, BEL, HT, ETX, et cetera To make a FORTRAN character string acceptable to a C routine one might use: StringFred(1:6) = "Hello" // char(0) where // is the FORTRAN symbol for concatenate.
How does the C macro work?
Your perspective of the C string is by and large correct.
In C macro and pre-processor defined value are interchangeable terms. Whatever is defined as NULL for your compiler ultimately translates to the value zero.
NULL is most correctly used to describe a pointer value, but depending on it's context in the code, NULL could be used as a 0 value char, long, pointer, etc, though some compilers might cough up warnings about type conversions. Hi Synthetic, and thanks, This is what I thought, but because, as you said, some programmers use
On Monday 18 April 2005 12:00, Synthetic Cartoonz wrote: the symbol NULL as a substitute for an integer zero, I became confused.
Though there is really nothing wrong with it, most C code wouldn't terminate a string by stuffing NULL into a character position. (assuming char a[2] and char * b pointing to something usable...)
a[0] = NULL; or *b = NULL;
Instead you'll usually see:
a[0] = '\0'; or *b = '\0';
How about: char Buffer[] = "Hello Charlie"; Buffer[5] = '\0' Is this 'stuffing a NUL' ? Or would you do a strncpy() ? In FORTRAN we have a nice (called obsolete) facility called EQUIVALENCE used like: integer (kind=4):: iArray(64) character(kind=1,len=256):: cArray equivalence (iArray, cArray) This same memory can be referenced as a character string or an array of integers. Maybe someone can answer this: In the old cpu's the copy of an integer array required, in Assembler, the loading of each element into a register followed by the storage of the element into the new array. And the associated maintanace of a counter in another register. Whereas copy of a string involved placing the address of each string into a register, and the number of characters to move, followed by one assembler instruction and the hardware did the rest at the speed of light. Thus, copying the string equivalent was much faster than copying an array of integers. I have done no Assembler since M$ Windows 95. Does anybody know enough about modern cpu's to answer my question? Regards, Colin