Thread: Create array of pointers dynamically

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by quzah View Post
    And as soon as you make them point at something, you've got yourself a 2D array.
    That is not true.

    The conventional definition - in mathematics and in computer science - of a two-dimensional array is a collection in which individual elements can be accessed using two subscripts (i.e. array[i][j] syntax in C, array(i,j) in Fortran, etc).

    If each pointer in the array was initialised to point to the head of a linked list, that array of pointers is not a 2D array. Using the array[i][j] syntax will give undefined behaviour (for any non-zero j) although, in C, it will sail merrily past the compiler.
    Quote Originally Posted by quzah View Post
    The point is, instead of us typing the same exact reply 1000 times, if he knows what to look for, this question has been answered countless times before.
    My point is that the question you have addressed is not the one asked. The question you answered (or, gave a pointer for) is a particular case of the one asked.

    If someone asked you for directions to a greengrocer (a store that sells multiple types of fruit and vegetables), then presumably you would see nothing wrong with giving directions to a stall that only sells apples. However, your answer would be completely useless to someone who wanted to buy oranges and bananas.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    The conventional definition - in mathematics and in computer science - of a two-dimensional array is a collection in which individual elements can be accessed using two subscripts (i.e. array[i][j] syntax in C, array(i,j) in Fortran, etc).

    If each pointer in the array was initialised to point to the head of a linked list, that array of pointers is not a 2D array. Using the array[i][j] syntax will give undefined behaviour (for any non-zero j) although, in C, it will sail merrily past the compiler.
    I hope you won't mind terribly while I prove you wrong again.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main( void )
    {
        char **foo;
        char c;
        foo = malloc( N * sizeof *foo ); /* make an array of N char* */
    
        foo[ 0 ] = &c;
        foo[ 0 ][ 0 ] ='a';
        
        printf( "\'%c\'", c );
    
        free( foo );
        return 0;
    }
    Yay me.
    Quote Originally Posted by grumpy View Post
    My point is that the question you have addressed is not the one asked. The question you answered (or, gave a pointer for) is a particular case of the one asked.
    My point is that you completely missed the correlation to making a dynamic 2D array and making an array of pointers. You can't make a dynamic 2D array without making a array of pointers.
    Quote Originally Posted by grumpy View Post
    If someone asked you for directions to a greengrocer (a store that sells multiple types of fruit and vegetables), then presumably you would see nothing wrong with giving directions to a stall that only sells apples. However, your answer would be completely useless to someone who wanted to buy oranges and bananas.
    That's a terrible analogy. He wanted to know how to paint. I showed him how to paint a car.


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

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by quzah View Post
    I hope you won't mind terribly while I prove you wrong again.
    I wouldn't mind, but your example does not achieve that.
    Quote Originally Posted by quzah View Post
    My point is that you completely missed the correlation to making a dynamic 2D array and making an array of pointers. You can't make a dynamic 2D array without making a array of pointers.
    I didn't miss the correlation. I don't consider that correlation particularly important in this case. Not all arrays of pointers are 2D arrays.
    Quote Originally Posted by quzah View Post
    That's a terrible analogy. He wanted to know how to paint. I showed him how to paint a car.
    Michelangelo would be spinning in his grave.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    I wouldn't mind, but your example does not achieve that.
    Sure it does. You expressly pointed out how 2D arrays means you access via [][], and I showed you that what you said can still be accessed the same way. It doesn't matter what you point at in your array of pointers, I can still access it via [][0].
    Quote Originally Posted by grumpy View Post
    Michelangelo would be spinning in his grave.
    Fortunately for us, there are no well known deceased fruit stand operators


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

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Well, if you want to create array of char dynamically, declare pointer to char, and allocate with malloc.

    To create array of type T dynamically, declare pointer to type T and allocate with malloc/...

    Since OP wants array of pointers dynamically, he needs pointer to pointers.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by quzah View Post
    It doesn't matter what you point at in your array of pointers, I can still access it via [][0].
    Are you also claiming that, if the pointers point at the head of a linked list, that you can access other elements of each linked list using array subscripts?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    Are you also claiming that, if the pointers point at the head of a linked list, that you can access other elements of each linked list using array subscripts?
    No, because linked lists have nothing to do with an array of pointers. So why on earth would I claim that? For that matter, why would you even assign the head of your linked list to a node in an array of pointers to nodes? I suppose if you're trying to make some kind of a hash table, but that's really the only reason I can see to do so.

    But I can still access the contents of the first element as if it were a 2D array, because it is. It's a [N][1] array. Because that's how you allocate 2D arrays, which is my entire point. If you know how to make dynamic 2D arrays, then you know how to make a dynamic array of pointers.

    It's also rather amusing that you brought linked linked lists to this discussion, since you whined about me mentioning 2D arrays.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically creating an array of pointers
    By cs32 in forum C Programming
    Replies: 4
    Last Post: 02-06-2008, 09:42 AM
  2. how to create an array of pointers
    By Dan17 in forum C++ Programming
    Replies: 12
    Last Post: 04-26-2006, 08:02 AM
  3. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Array of pointers help
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 02-14-2002, 12:37 PM