dynamic allocation to array of pointers HELP PLEASE!!!

This is a discussion on dynamic allocation to array of pointers HELP PLEASE!!! within the C Programming forums, part of the General Programming Boards category; it is crashing when allocation memory. This is hard;/ HERE IS THE PROGRAM:::: ////////////////////////////////PROGRAM REQUIREMENTS/////////////// Write a program to input ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    dynamic allocation to array of pointers HELP PLEASE!!!

    it is crashing when allocation memory. This is hard;/
    HERE IS THE PROGRAM::::
    ////////////////////////////////PROGRAM REQUIREMENTS///////////////
    Write a program to input the number of readings of floating-point numbers that are to be input from the user (key board). Then input the floating-point values from the user.

    Then sort the numbers using an array of pointers to the floating point numbers. Do not alter the order of the original array.

    Output the elements of the array sorted in ascending order, in the original order, and in descending order

    There is to be no subscript array notation i.e. no square brackets. Function main() may contain only declarations, function calls to your declared functions, and comments (calls to malloc/calloc must be in a function other than main()). You must use three functions in addition to main().
    ///////////////////////MAIN//////////////////////////////////


    #include <stdio.h>
    #include <stdlib.h>

    //Prototype Declarations

    float* getData(int* num);
    void printData (float** ptr, float* arr, int num);
    void sortData(float **ptr, int num);
    float** allocPtr(float* arr, int num);

    ////////////////////////////////Main//////////////////////////////////////////
    int main (void)
    {

    //declarations
    [code] float* arr; [\code]
    [code] float** ptr;[\code]
    [code] int num;[\code]

    //statements

    [code] arr = getData(&num);[\code]
    [code] ptr = allocPtr(arr, num);[\code]
    [code] sortData(ptr, num);[\code]
    [code] printData (ptr, arr, num);[\code]

    return 0;

    } // main


    /* :::::::::::::::::::::::getData:::::::::::::::::::: :::::::

    */
    float* getData(int* num)
    {

    //Local Declarations

    [code] int i; [\code]
    [code] float* arr;[\code]

    //statements

    [code] printf("Enter number of floats you wish to input: ");[\code]
    [code] scanf ("%d", num); [\code]

    //Allocate memory
    [code] arr = (float*) calloc (*num, sizeof(float));[\code]
    [code] if(arr == NULL)[\code]
    [code] {printf("Out of memory"); exit(100);}
    [code] free(arr);[\code]

    //input data

    [code] printf ("Enter the values: ");[\code]
    [code] for(i = 0; i < *num; i++)[\code]
    [code] scanf(" %f", (arr + i));[\code]

    [code] return arr; [\code]

    }//getdata

    float** allocPtr(float* arr, int num)
    {
    //decs
    [code] int i;[\code]
    [code] float** ptr;[\code]
    //statement


    [code] ptr = (float**) calloc (num, sizeof(float*));[\code]
    [code] if(ptr == NULL)[\code]
    [code] {printf("Out of memory"); exit(100);}[\code]


    [code] for (i = 0; i < num; i++)[\code]
    [code] **(ptr + i) = *(arr + i);[\code]

    [code] free(ptr);[\code]
    [code] return ptr;[\code]

    }//alocPtr

    void printData (float** ptr, float* arr, int num)
    {
    //decs
    [code] int i;[\code]

    //statements

    [code] printf ("The Array you entered is:\n");[\code]
    [code] for(i = 0; i < num; i++)[\code]
    [code] printf("\n\t%.2f\n", *(arr + i));[\code]

    [code] printf("\n");[\code]

    [code] printf ("Decending Order:\n");[\code]
    [code] for(i = 0; i < num; i++)[\code]
    [code] printf("\n\t%.2f\n", **(ptr + i));[\code]

    [code] printf("\n");[\code]

    [code] printf ("Ascending Order:\n");[\code]
    [code] for(i = num - 1; i <= 0; i--)[\code]
    [code] printf("\n\t%.2f\n", **(ptr + i));[\code]

    [code] printf("\n");[\code]
    [code] return;[\code]
    }


    void sortData(float **ptr, int num)
    {

    //decs
    [code] float *temp;[\code]
    [code] int i; [\code]

    //statements
    [code] for(i=0;i<(num-1);i++)[\code]
    [code] {[\code]
    [code] if(**(ptr + i + 1 ) < **(ptr + i))[\code]
    [code] {[\code]
    [code] temp = *(ptr + i);[\code]
    [code] *(ptr + i) = *(ptr + i + 1);[\code]
    [code] *(ptr + i + 1) = temp;[\code]
    [code] }
    }

    return;

    }
    Last edited by vlad_i; 02-15-2011 at 05:39 PM. Reason: REVISED

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    First off, I would suggest you brush up on the forum rules

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    and what rules am i not following?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    C Board - Announcements in Forum : C Programming

    << !! Posting Code? Read this First !! >>

    Code tags and proper indentation means we wont throw up and try to stab our eyes out when we see your code. Lack of vomiting and blindness greatly increases our efficacy in helping people like you.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    i tried to fix it a little. Thanks for the input

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Quote Originally Posted by vlad_i View Post
    i tried to fix it a little. Thanks for the input
    Did you read those links I posted? Click on the red words.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    not sure how to use the tags. And for the link, I need help why my program is crashing when allocating memory.

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Ptr is no longer valid after you free it, thus you can't return it after calling free().
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Did you read both links I posted all the way through? The second one has a pretty clear explination.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Quote Originally Posted by nimitzhunter View Post
    Ptr is no longer valid after you free it, thus you can't return it after calling free().
    All i need help with now is the selection sort. How to sort with just the pointers.

    Thanks,


    void sortData(float *ptr, int num)
    {
    //decs
    float temp;
    int i;

    //statements
    for(i=0;i<num;i++)
    {
    if(*(ptr + i + 1 ) < *(ptr + i))
    {
    temp = *(ptr + i);
    *(ptr + i) = *(ptr + i + 1);
    *(ptr + i + 1) = temp;
    }
    }

    return;

    }

  11. #11
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Still no codetag. Man, can you just read the links anduril posted.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    can you explain to me how to use code tag?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [code] <-- start with that
    ... your code here <-- put code in here
    [/code] <-- end with that


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

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Thanks for cross-posting and not telling us this was already solved. Just a giant waste of everybody's time.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Talking

    anduril462 here is some code for you 8=========) ~ thanks for not helping whatsoever so I decided to ask somewhere else

    This doesn't portray to anyone else. You guys were alot of help and I will do the tags differently in future posts!

    And my I didn't have time to post here that I figured it out on my own.

    Thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-23-2010, 05:17 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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