Fog Creek Software
Discussion Board

Please, don't desert me now

Back to pointers... (I'm even scared about asking this...)

When I have a pointer to a struct like struct name *var;

why do I have to do (*var).field = something; to write to it and var->field to read from it? Isn't the arrow notation supposed to be just another way of writing the asteric notation?

Monday, June 7, 2004

BTW, I'm learning linked lists now. If anyone has any good pointers on linked lists, please, do point them out.

Monday, June 7, 2004

Um, you don't - for example in this code both of the assigments are valid:

RECT* a = new RECT;
a->left = 5;
(*a).left = 5;

What made you think otherwise?

Monday, June 7, 2004

Btw, this is in C....
I had a struct where some of the fields were char arrays. When I tried to strcpy() one char array to another using the -> notation, it blew on me. When I used the (*struct).field notation, things went smoothly.

Monday, June 7, 2004

its pretty simple.

if you have:

myClass* myInstancePointer;



is correct, but if you have

myClass myInstance;


myInstance.member=value is correct

which means of course that

myClass* myInstancePointer;

is also correct since


Monday, June 7, 2004

"I had a struct where some of the fields were char arrays. When I tried to strcpy() one char array to another using the -> notation, it blew on me. When I used the (*struct).field notation, things went smoothly."

ahh.  <g> thats also pretty simple to understand, but Ill leave it as an exercise for the reader.

seriously, you need to get a grip on this stuff yourself, just follow through and make sure you understand which bits are pointers, which bits are the actual data and what exactly strcpy() takes and returns.

Monday, June 7, 2004

Hi noob, welcome back.

'C' strings, inside structures, have a few things you must insure.

struct mystruct
  int IsSet;
  char String1[20];  // Actually allocates 20 chars to hold string.
  char * String2;  // Only allocates a pointer --
    // memory for this string MUST be 'malloc'ed, or
    // the actual string bytes stored somewhere else.

  struct mystruct MyThing;
  struct mystruct * MyThingPtr;

  MyThingPtr = & MyThing;  // '&' is 'Address Of' operator


Now, using these declarations:

strcpy(MyThing.String1, "Hi");  // This works.
// This puts 'H', 'i', '\0' into the String1 space.

MyThing.String2 = malloc(40);  // Allocate space for string
strcpy(MyThing.String2, "Hi Again");
// Now, 'String2' points to malloc'ed space, and we can
// copy a string into it.  If we had NOT done the malloc,
// then we'd copy a string into a null pointer.  Program Crash.

One more thing:
  char Message[] = "Hello there.";

  MyThing.String2 = Message;  // This works, too.
  // In this case, you are not copying the string around.
  // Instead, you are merely assigning to String2 the
  // address of the 'Message' string.  You have to be careful
  // doing this, since 'Message' could go out-of-scope.
  // Then 'String2' would point to an out-of-scope piece of
  // memory -- which may or may not continue to hold a
  // valid 'C' string.  So, only do this if you KNOW that
  // 'Message' won't go out-of-scope.
  // You can guarantee that by making it global, or making it 'static'.
Note you can replace "MyThing." with "MyThingPtr->" everywhere, as long as you've set MyThingPtr to the address of MyThing.

Monday, June 7, 2004


Make sure you have a copy of Kernighan & Ritchie, and read chapter 6, doing the exercises.  That chapter all by itself is worth a year long university class.  I bought the book after teaching a data structures and algorithms class, and could have kicked myself for not using that as a supplemental text. It was a lot smaller and more concise than the text we used, and didn't leave anything important out.

Clay Dowling
Monday, June 7, 2004

I'll be reading that chapter first thing tomorrow. Thanks a lot.

Monday, June 7, 2004

*  Recent Topics

*  Fog Creek Home