Thread: Need help working through this

  1. #1
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72

    Need help working through this

    Hey guys,

    I need help working through the logic here.

    Code:
    struct value_entry {
    	char str;
    	int valid;
    };
    
    struct input_number_entry {
    	struct value_entry *value;
    	int value_size_word;
    };
    I completely understand the concept of the first struct. What's getting me is the second struct. Inside the second struct, a pointer variable is declared of the type of the first struct.

    And, well, the question is I don't really understand what that physically means.

    Is it simply declaring a pointer of type value_entry that will point to a copy of 'struct value_entry' every time a variable of type 'input_number_entry' is created? I'm trying to piece this all together!

    Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    all it means is that an instance of input_number_entry can potentially refer to an instance of value_entry. it's a pointer so it doesn't have to. the pointer could be null or left uninitialized.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Meldreth View Post
    all it means is that an instance of input_number_entry can potentially refer to an instance of value_entry. it's a pointer so it doesn't have to. the pointer could be null or left uninitialized.
    But leaving it uninitialized is a BAD idea. ALWAYS initialize variables! It helps in the long run.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    i didn't say it should, but i'm sure if i didn't mention leaving it uninitialized, some nitpicker would jump on me about it.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Meldreth View Post
    i didn't say it should, but i'm sure if i didn't mention leaving it uninitialized, some nitpicker would jump on me about it.
    Someone probably would. I just wanted to emphasize that there are great benefits (especially when it comes to pointers) when they are initialized. Uninitialized pointers tend to be non-NULL, but not point to "good" memory, so they lead to all sorts of trouble.

    And to the orignal post: My guess would be that the intention is to dynamically allocate the right number of elements in the value pointer.

    By the way,
    Code:
    struct value_entry {
    	char str[nnn];
    	int valid;
    };
    Shouldn't there be an array, if str is supposed to be a string?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    i didn't say it should, but i'm sure if i didn't mention leaving it uninitialized, some nitpicker would jump on me about it.
    I'm here.

    Is it simply declaring a pointer of type value_entry that will point to a copy of 'struct value_entry' every time a variable of type 'input_number_entry' is created?
    No.

    Change "will" to "might" and you are correct.

    Suppose you have a struct containing information about the engine of a car. Now you want a struct containing all information about the car. Instead of giving it all the values from the engine's struct, you simply give it a pointer to such a struct.

    Of course you need to make sure that the value field of input_number_entry actually contains some meaningful address of memory. So in a typical example, you create an object X of type input_number_entry and an object Y of type value_entry and then assign X.value the address of Y. If you don't do exactly that, it's best to initialize X.value with NULL, so you know that there's still some work to do.

    Greets,
    Philip
    Last edited by Snafuist; 02-18-2009 at 03:49 PM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    
    struct value_entry {
    	char str;
    	int valid;
    };
    
    struct input_number_entry {
    	struct value_entry *value;
    	int value_size_word;
    };
    
    int main () {
    	printf("%d\n", sizeof(struct value_entry));
    	printf("%d\n", sizeof(struct input_number_entry));
    	return 0;
    }
    8
    16


    You may get slightly different answers. Physically, what that means is a "value_entry" is allocated 8 bytes, 1 for the char, 3 for padding, and 4 for the int. An "input_number_entry" is 16 bytes, 8 for the pointer (to hold 64-bit addresses), 4 for the int and 4 padding.

    A value_entry* (pointer) is the address of a struct value_entry, and you can perform operations on the data at this address proper to such a datatype, eg, "value_entry->valid". However, a value_entry* is not actually the struct itself and must "point to" 8 MORE properly allocated bytes, which could happen this way:
    Code:
    struct value_entry one;
    struct value_entry *ptr=&one;
    Since that doesn't happen automatically, the answer to your question is no.
    Last edited by MK27; 02-18-2009 at 03:51 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    PhysicistTurnedProgrammer Cell's Avatar
    Join Date
    Jan 2009
    Location
    New Jersey
    Posts
    72
    Great answers, guys. Very helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM