Thread: Advanced pointer use

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    44

    Advanced pointer use

    I'm reading a book on C and I stumbled upon the following piece of code with advanced pointer use:

    Code:
    int main()
    {
      float (*a)[5];
      int n=3, i, j;
    
      a = (float(*)[5])malloc(n*sizeof(float[5]));
      for(i=0; i < n; i++)
      for(j=0; j < 5; j++) a[i][j] = ...
    According to the book *a is an array of 5 float elements so a is a pointer to an array of 5 float elements. Also, the book says that the n rows are in a sequence. The last remark I can understand, because malloc allocates n*5 float elements. I just don't understand why a is a pointer to an array of 5 float elements. I would think it is an array to n*5 elements. Does anybody see what I'm overlooking here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by mvanrijnbach
    I just don't understand why a is a pointer to an array of 5 float elements. I would think it is an array to n*5 elements. Does anybody see what I'm overlooking here?
    It just happens to be the syntax of how to declare a pointer to an array. Think about the book's point that *a is the array (although it does not exist until later), thus a is a pointer to an array.

    By the way, you can simplify this:
    Code:
    a = (float(*)[5])malloc(n*sizeof(float[5]));
    to:
    Code:
    a = malloc(n * sizeof(*a));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    You are creating a two dimensional array. You know one dimension in your initialization (5). For every element of your original a you are allocating another n size array of which you return the address. So you are actually working with pointers to pointers.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    44

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by boxden
    You know one dimension in your initialization (5). For every element of your original a you are allocating another n size array of which you return the address.
    Not quite. It is more like space is allocated for n arrays of 5 floats, and a points to the first of these n arrays.

    Quote Originally Posted by boxden
    So you are actually working with pointers to pointers.
    No, pointer to an array, not pointer to a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    Quote Originally Posted by laserlight View Post
    Not quite. It is more like space is allocated for n arrays of 5 floats, and a points to the first of these n arrays.


    No, pointer to an array, not pointer to a pointer.
    The name of an array is a pointer?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Quote Originally Posted by boxden
    The name of an array is a pointer?
    Nope. In many contexts, an array is converted to a pointer to its first element. That would be so for a[i], but then it would not be correct to try and pass a where a float** is required.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, I am not sure if this is what laserlight was referring to, but the subject of pointer declarations and dereferencing is often a criticized design choice in C.

    A lot of beginners are confused by a similar notation that means two different things: when you define a pointer:

    Code:
    int *p;
    ;

    The SYMBOL * just identifies the fact that p is a pointer to type int.

    Code:
    *p = 5;
    Later on in the code if you do this, you are saying "write to the address pointed by p the value 5".

    What helped me greatly when learning C was my teacher always asking me what a pointer is when I got confused. The answer was always : "A POINTER IS AN ADDRESS".

    In your case:
    Code:
    float (*a)[5];
    *a identifies that a is a pointer (because it is a definition).
    (i.e. an address to a type float [5], so to an array of 5 floats, which are obviously stored consecutively in memory because that's what an array does).
    Last edited by claudiu; 03-03-2010 at 02:24 PM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Perhaps this will help. The data is stored exactly as if one had done this:
    Code:
    typedef struct foo {
        float bar[5];
    };
    
    struct foo *a;
    The only difference is that instead of having to write
    Code:
    a[i].bar[j]
    you can omit 'bar' and just write
    Code:
    a[i][j]
    As you can see, 'a' can point to some number of items that each contain 5 floats. You can also see from such a definition that this is not compatible with float** because there is no double-pointer involved.
    Just to reiterate, the second dimension is not a pointer, it is the array itself, with a defined size.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    44
    Precisely. The book didn't mention pointer-to-pointers and (*a)[5] is not an array of 5 pointers, which is what I thought at first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM