Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dereference

Reply
Thread Tools

dereference

 
 
Hans Vlems
Guest
Posts: n/a
 
      08-24-2012
On 23 aug, 03:26, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Keith Thompson wrote:
> > No, it isn't the same thing at all, and the difference is *exactly*
> > what we've been discussing.

>
> Ok I read Kenneth Brody's post. I guess the difference is the pointer and
> what it's pointing at.
>
> Bill


Imagine two drawers in a cabinet. The right drawer contains a one
dollar note. The left drawer contains a note that says "one dollar
note in right drawer".
The content of the left drawer points to the right drawer. Only the
right drawer holds the real thing (the money).
Hans
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-24-2012
Kenneth Brody wrote:
> Using GC, if I remove the note from the left drawer, what happens to
> the money in the right drawer?


Nothing ?

Bill


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-24-2012
Kenneth Brody wrote:
> Using GC, if I remove the note from the left drawer, what happens to
> the money in the right drawer?


That sounds like a null pointer.
char p[]="hello world\n";
p==NULL;


 
Reply With Quote
 
Andrew Cooper
Guest
Posts: n/a
 
      08-25-2012
On 24/08/2012 18:31, Kenneth Brody wrote:
> On 8/24/2012 6:00 AM, Hans Vlems wrote:
>> On 23 aug, 03:26, "Bill Cunningham" <(E-Mail Removed)> wrote:
>>> Keith Thompson wrote:
>>>> No, it isn't the same thing at all, and the difference is *exactly*
>>>> what we've been discussing.
>>>
>>> Ok I read Kenneth Brody's post. I guess the difference is the pointer
>>> and
>>> what it's pointing at.

>>
>> Imagine two drawers in a cabinet. The right drawer contains a one
>> dollar note. The left drawer contains a note that says "one dollar
>> note in right drawer".
>> The content of the left drawer points to the right drawer. Only the
>> right drawer holds the real thing (the money).

>
> Using GC, if I remove the note from the left drawer, what happens to the
> money in the right drawer?
>


Depends.

Is there another draw (the centre one perhaps) with a note saying "one
dollar in the right drawer"?

If yes, then nothing
If no, then the dollar turns into free space.

~Andrew
 
Reply With Quote
 
Adrian Ratnapala
Guest
Posts: n/a
 
      08-25-2012
On Saturday, 25 August 2012 01:36:12 UTC+2, Bill Cunningham wrote:
> Kenneth Brody wrote:
>
> > Using GC, if I remove the note from the left drawer, what happens to

>
> > the money in the right drawer?

>
> the money in the right drawer?


I think the "Nothing" answer was better. I think the right drawer doesn't have a note saying "one dollar in left drawer". Rather it has a note saying "Money, left drawer", where it is previously understood that the *right* drawer contains a treasure map to finding, say, auntie Rohini's life savings.

typedef int Dollars;
Dollars rohinis_stash = 1;
Dollars *treasure_map = &rohinis_stash;

rohinis_stash = 0;
assert(treasure_map == &rohinis_stash); // passes
assert(*treasure_map == 0); // passes

The pointer in the right draw is unchanged, and still correct, if Rohini empties out the here stash. What was a pointer to $1 is now a pointer to $0.A NULL pointer would be if Rohini keeps here money where it is, but replaces the pointer with a not saying "Money, up your nether hole".


typedef int Dollars;
Dollars rohinis_stash = 1;
Dollars *treasure_map = &rohinis_stash;

treasure_map = NULL;
assert(*treasure_map == 1); hopefully segfaults
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-25-2012
On Fri, 24 Aug 2012 19:36:12 -0400, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Kenneth Brody wrote:
>> Using GC, if I remove the note from the left drawer, what happens to
>> the money in the right drawer?

>
>That sounds like a null pointer.
>char p[]="hello world\n";
>p==NULL;
>


There are no pointer variables in your code. Go back and start again
from scratch while writing "Arrays and pointers are different objects"
1000 times.

--
Remove del for email
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-25-2012
Barry Schwarz wrote:

> There are no pointer variables in your code. Go back and start again
> from scratch while writing "Arrays and pointers are different objects"
> 1000 times.


This is something I know because its been drilled into me. But the thing
is I haven't yet caught onto the real meaning yet. For example I know of a
funtion whose prototype is char * but if you put a string into it the
function doesn't work correctly it's called cbc_crypt(). You have to pass
char key[]="string"; to char *key, the first parameter. Not quite sure why
yet but that's the way it works.

Bill


 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      08-25-2012
On 25/08/2012 15:46, Bill Cunningham wrote:
> Barry Schwarz wrote:
>
>> There are no pointer variables in your code. Go back and start again
>> from scratch while writing "Arrays and pointers are different objects"
>> 1000 times.

>
> This is something I know because its been drilled into me. But the thing
> is I haven't yet caught onto the real meaning yet. For example I know of a
> funtion whose prototype is char * but if you put a string into it the
> function doesn't work correctly it's called cbc_crypt(). You have to pass
> char key[]="string"; to char *key, the first parameter. Not quite sure why
> yet but that's the way it works.
>


All you need to know is explained here:
http://c-faq.com/aryptr/index.html

--
Vincenzo Mercuri
 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      08-25-2012
On 25/08/2012 15:46, Bill Cunningham wrote:
> Barry Schwarz wrote:
>
>> There are no pointer variables in your code. Go back and start again
>> from scratch while writing "Arrays and pointers are different objects"
>> 1000 times.

>
> This is something I know because its been drilled into me. But the thing
> is I haven't yet caught onto the real meaning yet. For example I know of a
> funtion whose prototype is char * but if you put a string into it the
> function doesn't work correctly it's called cbc_crypt(). You have to pass
> char key[]="string"; to char *key, the first parameter. Not quite sure why
> yet but that's the way it works.
>


I read your post in comp.unix.programmer, you also
need to read this: http://c-faq.com/decl/strlitinit.html

I'd suggest to read all the FAQ, of course

--
Vincenzo Mercuri
 
Reply With Quote
 
Adrian Ratnapala
Guest
Posts: n/a
 
      08-25-2012
On Saturday, 25 August 2012 15:46:50 UTC+2, Bill Cunningham wrote:
[regarding "array" is not a pointer"]
> This is something I know because its been drilled into me. But the thing
> is I haven't yet caught onto the real meaning yet. For example I know of a
> funtion whose prototype is char * but if you put a string into it the
> function doesn't work correctly it's called cbc_crypt(). You have to pass
> char key[]="string"; to char *key, the first parameter. Not quite sure why
> yet but that's the way it works.


That's a really nice example; (warning) I didn't know about cbc_crypt()
until just now, but I will try to explain it anyway. My man page says:

int cbc_crypt(char *key, char *data, unsigned datalen,
unsigned mode, char *ivec);

Notice it is `char *`, not `const char *`. This is a hint that the function will be read, and *write* into the buffer. So one way or another, `key` must point to writeable memory.

If we try:

int main(void)
{
char *key = "some stuff";
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dereference Adaptor Matthias Kaeppler C++ 3 02-28-2005 05:42 PM
How does dereference operator overloading really work? Joe Seigh C++ 18 09-22-2003 05:42 PM
NULL Pointer Dereference Denis Palmeiro C Programming 10 07-16-2003 12:33 PM
Re: how to properly dereference STL list item Howard C++ 0 07-01-2003 05:46 PM
Re: how to properly dereference STL list item Jakob Bieling C++ 0 07-01-2003 05:45 PM



Advertisments