Thread: struct question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    struct question

    I have kind of a concept question...

    Code:
    struct node
    {
      int count;
      struct node *next;
    };
    
    int main()
    {
      int data = 0;
      struct node *getdata;
      getdata->count = data + 1;
      printf("%d", getdata->count);
    }
    there is a bug in the preceding code but I don't totally understand it. Could somebody explain what is going wrong?? Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This doesn't point to writable memory.
    Code:
    struct node *getdata;
    So if you try to write there, you're hosed.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Can you elaborate? Cause I really want to understand what's going wrong and I still don't really. What would I have to change to make this code work?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A pointer points. Where is your finger pointing at the moment? Nowhere? Same place your pointer is pointing: nowhere.

    You need to point to some writable memory before you can write to where it is pointing. Here you are writing to a dangling pointer.
    Code:
      struct node *getdata;
      getdata->count = data + 1;
    Since this appears to be a linked list type of question that you seem to be withholding, I'd say you need to look into dynamic allocation; since this is the C board, that means malloc/free.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Or if you didn't want to use dynamic memory allocation:

    Code:
    int main(void) {
      int data = 0;
      struct node d;
      d.count = data++;  //assigns data to d.count, then increments data by 1
      printf( "%d", d.count );
    }
    This code would simply print a '0' to the standard input, since data is incremented after it is assigned to count. Using ++data would increment data first, then assign it to count.

    But as Dave has said, look into memory allocation and how variables and initialized. Its always best to start with the basics.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think the problem was around dynamic memory allocation, not pre- vs. postfix increment operators, especially since the original program didn't have a ++ in it.

    Try googling for "c programming tutorial dynamic memory allocation" or something; I'm sure you'll turn up a page or two. You could also throw in "malloc".

    This tutorial might have some relevant information, but it's about pointers and doesn't have anything about memory allocation: http://www.cprogramming.com/tutorial/c/lesson6.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM