Thread: confusion on structure access

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    Question confusion on structure access

    Look at the comments please.
    Code:
    #include <stdio.h>
    
    #define MAXLEN 20
    
    struct proto_type {
            int i;
            char s[MAXLEN];
    };
    
    main() {
            struct proto_type stru_1, *p;
            int i;
    
            p = &stru_1;
    
            for (i = 0; i < MAXLEN-1; i++)
                    (&stru_1)->s[i] = '0';
            (&stru_1)->s[i] = '\0';         /* necessary when using (&stru_1)
                                               to access a member in a structure */
            printf("before:\n%s\n", (&stru_1)->s);
    
            for (i = 0; i < MAXLEN-1; i++)
                    p->s[i] = '0';
            /* p->s[i] = '\0'; */           /* unnecessary when using pointer 'p'
                                               to access a member in a structure */
            printf("after:\n%s\n", (&stru_1)->s);
    }
    Last edited by thinhare; 01-06-2005 at 05:03 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You've got it wrong. You don't use the address-of operator to dereference (ie: access). And if you did, you'd use the dot operator, not the arrow operator.
    Code:
    struct foo instance, *pointer;
    pointer = &instance;
    
    pointer->x = 10; /* assign the value of 10 to the member variable x via a pointer*/
    instance.x = 10; /* assign the value of 10 to the member variable x directly*/
    
    (*pointer).x = 10; /* assign the value of 10 to the member variable x via a pointer*/
    As you can see, there's no point to doing the latter, when you can just use the arrow. (That's what they made it for!)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > Quote:
    > #define MAXLEN 20
    Better than nothing, but please use the code tags, not the quote tags.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    Quote Originally Posted by Salem
    > Quote:
    > #define MAXLEN 20
    Better than nothing, but please use the code tags, not the quote tags.
    okay, I didn't notice the code button before, and I'll use it definitely afterwards.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    yes, exactly I do know what you mentioned about.
    but my point is for the case I don't wanna define
    one more variable of pointer by using "&" instead.
    That might not be a good programming habit
    but I'm just curious.

    Quote Originally Posted by quzah
    No. You've got it wrong. You don't use the address-of operator to dereference (ie: access). And if you did, you'd use the dot operator, not the arrow operator.

    As you can see, there's no point to doing the latter, when you can just use the arrow. (That's what they made it for!)

    Quzah.
    Last edited by thinhare; 01-06-2005 at 05:04 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't understand. Why if you only want to use an instance, aren't you simply using the dot operator? What's your fascination with the arrow operator that you feel you must use it?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    Wink

    it's not that I MUST use it but for curiosity.
    "&instance" and "pointer to instance" do the same thing, don't they?
    could you explain what cause the difference in the code above?
    Thanks!

    Quote Originally Posted by quzah
    I don't understand. Why if you only want to use an instance, aren't you simply using the dot operator? What's your fascination with the arrow operator that you feel you must use it?

    Quzah.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    I think they are same,
    What difference are they?

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I see what you're saying:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct proto_type {
    	char c;
    };
    
    int main(void) {
    	struct proto_type stru_1, *p;
    	p = &stru_1;
    	
    	p->c = 'p';
    
    	// We get a segment fault here...
    	//putchar((&stru_1)->c); <-- The -> seems to only work with "real" pointers...
    	puts(NULL);
    
    	return 0;
    }
    But I don't think it's possible... :(

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Nothing wrong with the putchar - that works fine

    It's the puts(NULL) which gets a segfault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  4. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM