Thread: Pointers and arrays

  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.

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

    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 euler_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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM