Thread: Pointer (*p)[3] Issue

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    10

    Pointer (*p)[3] Issue

    I declare a pointer as
    unsigned char (*p)[3];
    does that mean a pointer point to a array of 3 int elements?

    then I wrote codes as below:
    Code:
    unsigned char a[3]={1,2,3};
    unsigned char b,c;
    unsigned char (*p)[3];
    p=a;
    b=*p;
    I supposed that the value of 'b' is 1
    During my debugging, I found that the value of b is not 1 but equals to p (0x75).(the value of a is 0x75,the address of a[0],so is p)

    Does anyone know why's that?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    If you have a pointer to an integer, what do you get when you dereference? You will have yourself an integer.

    You have a pointer to an array. What do you get when you dereference?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    10
    sorry...I don't get it well......would u mind providing me a detailed example to better illustration?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I think p should also be like
    Code:
    p=&a;

  5. #5
    Registered User
    Join Date
    May 2014
    Posts
    10
    Hi Satya, why? the array name is the address of the first element of the array, isn't it? means a = &a[0].
    if p=&a => p = &(&a)

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    &arr yields a pointer, of type pointer-to-array-of-T, to the entire array.
    A simple reference (without an explicit &) to an array yields a pointer, of type pointer-to-T, to the array's first element.
    Most important thing to remember is the type.
    So for example the array is
    Code:
    int array[3]={1,2,3}
    if you say
    Code:
    array or &array[0] gives the address and the type int  but if you say &array it gives address but the type is int [3], 3 being the size of array.
    Hence if
    Code:
    int array[3]={1,2,3};
    int *p;
    p = array; // p is pointing to array first element
    int (*q)[3] // q is pointing to int array of 3 
    q = &array;
    if you increment p then it will increment to one element of int or it points to the next element of array that is 2 here
    but if you increment q it will increment beyond the entire array.
    I am not sure if i am clear. If any mistakes in my explanation please correct me.

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    90
    I've found his to be particularly useful to get your head around this stuff:
    cdecl: C gibberish ↔ English

  8. #8
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Code:
    type (*p)[3];
    This is a pointer to an array of 3 elements of some type...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to Pointer manipulation issue
    By workisnotfun in forum C Programming
    Replies: 5
    Last Post: 10-16-2012, 11:31 AM
  2. pointer issue
    By Nuggnugg in forum C Programming
    Replies: 15
    Last Post: 03-31-2011, 01:52 PM
  3. Issue with pointer
    By ph5 in forum C Programming
    Replies: 6
    Last Post: 12-07-2010, 05:52 AM
  4. Pointer issue
    By pfs in forum C Programming
    Replies: 2
    Last Post: 09-26-2008, 11:37 AM
  5. C++ pointer issue
    By yongzai in forum C++ Programming
    Replies: 8
    Last Post: 12-22-2006, 04:27 AM