Pointers and arrays

This is a discussion on Pointers and arrays within the C++ Programming forums, part of the General Programming Boards category; Could anyone give me a crash course on using a dynamically allocated pointer array to use pointers within it to ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    26

    Pointers and arrays

    Could anyone give me a crash course on using a dynamically allocated pointer array to use pointers within it to point to values in an array?

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    do you mean creating pointers to pointers using malloc...
    like the one below..


    Code:
    ---------------------------------------
    
    [  ]
      |
      |
    [  ]------[][][][]
    [  ]------[][][][]
    [  ]------[][][][]
    
    
    
    ----------------------------------------

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Uh.. heres an attatched crappy photoshop of what I'm trying to do. Sorting an array using two pointer arrays full of pointers to the array, not values.
    Attached Images Attached Images  

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703

    Re: Pointers and arrays

    Originally posted by OttoDestruct
    Could anyone give me a crash course on using a dynamically allocated pointer array to use pointers within it to point to values in an array?

    char ** array = new char* [size];
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Alright... I'm now getting core dumps and I can't see why.. then again I don't understand pointers at all... this is supposed to look at array and sort the pointers of pAscending without modifying whats inside array...

    Code:
    void sortascend (int array[], int **pAscending)
    {
      int i;
    
      void exchangelargest (int[], int**, int);
    
      for (i = 0; i < 5; i++)
       exchangelargest (array, pAscending, i);
    } // sortascend  
    
    void exchangelargest (int array[], int **pAscending, int i)
    {
      int walker;
      int largest;
      int *temp = *pAscending;
      largest = i;
      
      for (walker = i+1; walker <= 5; walker++)
       {
        if (array[walker] > array[largest])
         largest = walker; // if
    
       temp = *(pAscending+i);
       *(pAscending+i) = *(pAscending+largest);
       *(pAscending+largest) = temp;
       } // for
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Read this

    Without a context for how you're calling your functions (like a main()), its hard to say what is wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

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