Re: "Mastering C Pointers"....

From: goose (ruse_at_webmail.co.za)
Date: 11/05/03


Date: 5 Nov 2003 00:03:58 -0800


"Roose" <nospam@nospam.nospam> wrote in message news:<TRIpb.13665$Jn.1970@newssvr14.news.prodigy.com>...
> > After reading the first 1/3 carefully, it seems to me that a pointer is
> very
> > much like a symlink.
>
> Uh, I'm not exactly sure what a symlink is, but if it is anything like a
> Windows shortcut, there is definitely an analogy there.
>
> The windows shortcut just stores the location of a file. A "pointer" to it.
> An address. The address is the _path name_. In C, the "address" is
> basically an integer,
   
no, it isn't *always* an integer type. in x86 real-mode it is *two*
integer types.

> indicating where in a huge array of memory the
> variable is.
>
> You can have as many shortcuts as you want that point to the file. A
> shortcut takes a small amount of storage, but not as much as a (typical)
> file. You can have whole directories of shortcuts that point to files all
> over the place, in order to organize them. Likewise, you can have
> collections of pointers, that point to various different things _scattered_
> all over memory.
>
> For example, on my hard disk, I have a whole bunch of (legal) MP3s organized
> by artist and by album. Now, some of them were ripped poorly and have
> clicks. So I *could* just copy those files to another folder, in order to
> note that I need to rip them again.
>
> However, it is *much* easier and efficient to just copy shortcuts those bad
> files into another folder -- called "bad". That way I don't duplicate any
> data. And I have all the bad files organized in one directory, and can
> access them easily.
>
> You could do the exact same thing with pointers. Suppose I had a whole
> bunch of strings in memory, that listed a ton of song names. I could create
> an array of pointers called "theBeatles" that pointed to every single song
> by the Beatles. Then I could have an array of pointers called "badFiles"
> that pointed to all the bad files, _some of which_ may be by the Beatles.
> Note that I haven't incurred the storage cost of repeating the strings,
> *just* pointers to them. Likewise I could create another array called
> "before1960" that points to all songs stored before 1960 -- you get the
> idea.
>
> Hm, I'm surprised this didn't come up earlier. This is an easy analogy.

but not entirely accurate. this is because you cannot do "shortcut
arithmetic" but you /can/ do pointer arithmetic.

<to OP>

a pointer variable holds an address of an object. for example
a "pointer to char" can hold the addess of a char, and a
"poniter to int" can hold the address of an int. to access
the object itself, one must dereference the pointer.

dereferencing means "get the object at that address". for example:

/* we declare an object of type int */
int foo = 12;

/* we declare a pointer to int */
int *bar;

/* now we set bar to point to foo, using the address of operator */
bar = &foo;

/* foo still contains 12, while bar contains the address of foo.
   now we dereference bar to get the value of foo (namely, 12) */
printf ("bar points to foo which has number %i\n", *bar);
/* that would have printed
   "bar points to foo which has number 12."
   */

hth
goose,
   busy as can bee



Relevant Pages

  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (comp.arch.embedded)
  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (sci.electronics.design)
  • Re: Interview questions
    ... and note that this means the implementor of Bar needs to ... Bar might want to inherit some virtual methods of Foo. ... is the first member of bar, we can say a Bar "is a" Foo. ... > You cannot pass a pointer to an object of type Bar ...
    (comp.lang.cpp)
  • Re: Circular dependency - I think..
    ... > I see that you are not initialising the pointer to BAR contained in FOO. ...
    (comp.lang.cpp)
  • Re: Getting the instance pointer from a called function
    ... >> pointer) which called bar(), from inside bar, when that happens. ... store a pointer to the current instance of Foo in ... will store pointers of Foo instances, but barhas a strict prototype and ...
    (comp.lang.cpp)