|
|
|
#26 |
|
Messages: n/a
Hébergeur: |
> > You attempt to pass the non-strings when printf() is expecting strings. > Your program now exhibits undefined behavior. Nothing else about it > will tell you very much. > Thanks Brian...I think I am beginning to understand this.Not quite there yet...but getting closer!! Next onto function pointers!!! |
|
|
|
#27 |
|
Messages: n/a
Hébergeur: |
mdh said:
> On May 11, 8:16 pm, Richard Heathfield <r...@see.sig.invalid> wrote: >> >> Writing code "to figure things out" is a two-edged sword. It can >> certainly be ful for discovering how things *appear* to work, but it >> is a poor guide for discovering how things *must* work. > > > Point made and taken...thanks. > > >> > char arr [3][3] = {"One", "Two", "Lst"}; >> >> Legal, but dangerous. Don't treat these arrays of char as if they were >> strings. They aren't. > > OK..now let me show my total ignorance. > > So, can multidim char arrays ever be treated as strings and would > this then be the correct intialization? Yes. There is *nothing special* about "multidim" arrays. For example: char arr[6][15] = { "This", "string", "initialisation", "works", "just", "fine" }; for(i = 0; i < 5; i++) { printf("%s\n", arr[i]); } > char arr[] [] = "one, two"; > And, for a regular (one dim) All arrays are regular. A multidimensional array is simply a one-dimensional array, each of whose elements happens to be an array. Divide and conquer. > char array, would this be a string > intialization? char arr[]= "something" Yes. > or the use of strcpy? No, strcpy can never initialise anything, because initialisation is what you do to an object at the point where it is created. It says to the compiler: "at the point where you are making an object of THIS type for me, please give it THIS value". By the time you can call strcpy, it's too late to initialise the array into which you are copying the string, but of course it's not too late to update that array's members' values. > And, somewhat philosophically, a (one dim) char array is simply a > contiguous set of bytes. If one ? *knows* the array is NULL > terminated, A char array cannot be NULL terminated. C is a case sensitive language. NULL is a null pointer constant. Strings are terminated by a null character, not a NULL character (because there is no such thing as a NULL character). > then we can safely treat it as a string. Right. > But the *only* > way to know this is if it is correctly initialized in the first place. > Is this a fair way of looking at it? Yes. When you create an object, put something in it, so that it has a known value. That way, later on when you examine that object, you know you're /allowed/ to examine that object. It might not have the /right/ thing in it (i.e. your program might have a bug) but at least it contains a legitimate and reproducible value (i.e. debugging is made considerably easier). >> > p=&arr[i][j]; >> > printf( "%p\n", p); >> >> Although the representations of char * and void * are required to be the >> same, it is generally wiser to insist on void * for %p: >> >> printf("%p\n", (void *)p); > > I did read today about the need for a pointer to be cast to > void ...but is there a reason for this? Yes. The printf function specifies an interface that allows you to print a void * value, but doesn't specify an interface for pointer values of other types. If you would like a function that can print a pointer value of *any* type, feel free to write one - but I should warn you that it will take you infinitely long to write such a function in true generality. > Other than the language says it should be so. Isn't that a pretty powerful reason? -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#28 |
|
Messages: n/a
Hébergeur: |
Thanks Richard, as usual, for your insights.
|
|
|
|
#29 |
|
Messages: n/a
Hébergeur: |
In article <Z8adnbR2Ut4wgbTVnZ2dnUVZ8rednZ2d@bt.com>
Richard Heathfield <rjh@see.sig.invalid> wrote: >... strcpy can never initialise anything, because initialisation is what >you do to an object at the point where it is created. In the sense of "an initializer" (as used in Standard C), yes. However, one should be aware that many (perhaps even most?) people also refer to the first assignment to something as its "initialization", and then use the word "initialize" (or "initialise") to describe what happens at that point. That is: void f(void) { int i = 0; int j; ... code that does not touch j ... j = 0; ... } In Standard C terminology, the variable i has an initializer and is initialized to 0. The variable j has no initializer and is not initialized. The assignment "j = 0" is an ordinary assignment and does not "initialize" j ... but in many/most people's terminology, the assignment to j, which is its initial (i.e., first) assignment, does "initialize" j. In short, one must be careful with terminology, as the multiple meanings of various words do confuse people. :-) -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html |
|
|
|
#30 |
|
Messages: n/a
Hébergeur: |
Chris Torek said:
<snip> > The assignment "j = 0" is an ordinary assignment and > does not "initialize" j ... Right. > but in many/most people's terminology, > the assignment to j, which is its initial (i.e., first) assignment, > does "initialize" j. Well, Chris, I can't that. :-) > In short, one must be careful with terminology, as the multiple > meanings of various words do confuse people. :-) That's why I try very hard to stick to the terminology defined by ISO. Rather than people having to guess what I mean when I use a term, they have the opportunity to look it up in the Standard. Of course, not all terms are defined therein, and even when they are I don't suppose I always get them right. One does the best one can. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#31 |
|
Messages: n/a
Hébergeur: |
On May 11, 7:07pm, mdh <m...@comcast.net> wrote:
> > int main (int argc, const char * argv[]) > { Did you throw the const in just to annoy people? Is that "signature" properly documented on your system? How many other systems do you think will support it? |
|
|
|
#32 |
|
Messages: n/a
Hébergeur: |
mdh wrote:
>> You attempt to pass the non-strings when printf() is expecting strings. >> Your program now exhibits undefined behavior. Nothing else about it >> will tell you very much. >> > > > Thanks Brian...I think I am beginning to understand this.Not quite > there yet...but getting closer!! Next onto function pointers!!! A char arr[3] = "One"; B char arr[] = {'O','n','e'}; C char arr[4] = "One"; D char arr[] = {'O','n','e','\0'}; E char arr[] = "One"; Array definitions A and B, can be used interchangeably. Array definitions C, D and E, can be used interchangeably. -- pete |
|
![]() |
| Outils de la discussion | |
|
|