Like Tree2Likes
  • 2 Post By AndrewHunter

Explain the difference between b and *b?

This is a discussion on Explain the difference between b and *b? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void main() { int a[]={1,2,3,4,5}; int (*b)[5]; b=a; printf("%u %u %u\n",b,*b,*b[0]); printf("%u %u %d",sizeof(b),sizeof(*b),*b[0]); } o/p: 1000 1000 ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20

    Explain the difference between b and *b?

    Code:
    #include<stdio.h>
    void main()
    {
        int a[]={1,2,3,4,5};
        int (*b)[5];
        b=a;
        printf("%u %u %u\n",b,*b,*b[0]);
        printf("%u %u %d",sizeof(b),sizeof(*b),*b[0]);
    }
    o/p:
    1000 1000 1
    4 20 1

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    b is of type "pointer to an array of five int". *b is of type "array of five int".
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20
    If *b is of type "array of five int" ,then Is any extra array getting created for this purpose?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No array is created -- when you set a pointer, the idea is that you are pointing it at something.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You may want to take a look at Prelude's tutorial on pointers. It should answer most of your questions.
    stahta01 and ackr1201 like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20
    If no extra array is created, then how can we say *b is of type "array of five int"...? Is that the property it is holding??

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    That's why it is of type "array of five int". As in it is the declaration of what kind of object it is. Read the link I posted for you.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by ackr1201 View Post
    If no extra array is created, then how can we say *b is of type "array of five int"...? Is that the property it is holding??
    Remember, b is of type "pointer to array of 5 ints", so *b is of type "array of 5 ints". Just like with int *p, p is of type "pointer to int" and *p is of type "int". You're dereferencing the pointer when you put that * in front of it, so the resulting type is whatever is pointed to.

    EDIT: And remember, you set b to point to a, so dereferencing it doesn't create an array, but it does yield the array pointed to by b

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    Quote Originally Posted by ackr1201 View Post
    If no extra array is created, then how can we say *b is of type "array of five int"...? Is that the property it is holding??
    The type of a variable (b, in this case) is a property of the variable. The value is another property of that variable.

    The two do not automatically go together (except in some special circumstances that are not particularly relevant here). That means creating a pointer does not magically create a companion pointee.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz explain......
    By sxg in forum C Programming
    Replies: 2
    Last Post: 12-11-2010, 09:10 AM
  2. Can anyone explain this one?
    By BEN10 in forum Tech Board
    Replies: 16
    Last Post: 09-06-2009, 11:36 AM
  3. Plz explain difference between functions returning ref
    By dudeomanodude in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2008, 01:03 PM
  4. Explain Me Please
    By abyu in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2006, 05:29 AM
  5. Replies: 1
    Last Post: 05-07-2002, 04:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21