| « What the GPL v3 isn't | Forking GNU/Linux » |
Sun, Sep 24, 2006
![[Icon]](rsc/img/chain_link.gif)
I just got to the bit about pointers and arrays.
NNNnngg...
So. . . if you want strings, you either have a character array or a character pointer.
So you can define the string "foobar" by either:
char word[]="foobar"
or
char * word="foobar";
And then if you want to change "foobar" to "FooBar", you would use
word[0]='F';word[3]='B';
or
word="FooBar";
respectively. The array changes in-place, but the pointer drops "foobar" from memory and points to the newly-created string "FooBar" stored someplace else in memory.
You run into problems because functions like gets() can't distinguish between arrays and pointers, so always try to change in-place. So if you use gets(word) and the user types "Foooobaaaarrrr", the array would ignore everything that was too long to fit, but the pointer would accept the whole lot, potentially over-writing a piece of memory in use by some other function and causing problems.
So you either have to use arrays, which have fixed lengths and have to be changed character-by-character; pointers which can be any size and can be changed in one go, but you can't use functions like gets(); or a pointer to an array, which can be changed all in one go but can only be as long as the array.
Is that about right?
char foo[] = "foobar";
foo = "Foobar";
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
Facebook Syndication Error
04/02/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
Submersible houseboat