Thread: Array of pointer

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Array of pointer

    Can I create an array of pointer with new operator?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Yes. Where are you having trouble?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    I'm sorry. Because I've just learned C++, I'm still confuse about pointer in C++. Would you like to tell me how to create a pointer of array with new operator?

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Type **ptr_array;
    ptr_array = new Type *[256];
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not sure exactly what you want, so here goes...

    A dynamically created array of int pointers:
    int **myArray = new int*[10];

    A dynamically created array of ints
    int *myArray = new int[10];

    Clarify what you want, try coding it yourself, and post your attempt here if you have trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Can somebody tell me if this is correct, too ?

    Code:
    typedef int* pint;
    pint *ppiRowIndices;
    
    ....
    
    ppiRowIndices= new pint[iCount];
    It seems to work but looks differnt then the code posted here. It wouldn't be too cool if it only worked because there are no overflow checks etc.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    basically you did the same thing. the typedef gives a name to an int * is the only difference.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by FillYourBrain
    basically you did the same thing. the typedef gives a name to an int * is the only difference.
    I did that because new didn't accept the "int*". Thats why I wondered if my code is correct. pint is just another name for "int*", so why would it accept that but not "int*"? Anyway, if it's correct and it works I'm happy.

    thanks!

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I did that because new didn't accept the "int*".
    what compiler? can I see that line of code?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Also, once you have your array, remember that none of those pointers have been initialized.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    Thank you. It's very helpful for me.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I understand the workings of an array of pointers, but I'm not sure about the application of it. Would this be a valid example?

    Code:
    void InitializePCharArray(char **array,int num)
    {
    for (int i=0;i<num;i++)
        *((&array)[num])=GetInput(); //return type char *
    }
    
    int main()
    {
    char **PPCharArray=new char*[10];
    
    InitializePCharArray(PPCharArray,10);
    return 0;
    }
    I'm really lost here!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Wow that was some serious overkill ey? I think I understand now, I drew a whole lot of diagrams out on paper, with arrows, boxes, and everything.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM