Thread: Array of pointers help

  1. #1
    Unregistered
    Guest

    Array of pointers help

    OK I am having problems understanding how to get started on a program so if someone could try and explain this to me adn start me off or give me a clue or something it would be helpful. Im not a cse major and so its kind of fuzzy to me.

    For this assignment, assume experiment X has been run 3 times, and that each
    run produced 15 results, and each result is treated as an integer. So the data
    structure of choice will be an array of 3 pointers, each of which points to one
    of the arrays of 15 results of a given run of the experiment.

    Declare the array of pointers in main() and initialize each element to
    0 (zero). Then, for each element of the array of pointers, allocate memory
    for an array of 15 integers and initialize each element to the value -1.

    Then set up and call a function to store data from the user into the
    arrays of 15 integers. For this assignment, prompt for 3 integers
    at a time, with each of the 3 integers going into it's own array of 15
    integers, at the next available index, for a total of 15 prompts. For
    purposes of this assignment, and for help in debugging, print out the
    contents of each array as a new number is added. All numbers to be
    entered by the user are in the range 0 .. 9.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I disagree. I think the data structure of choice would be a two dimensional array [15][3], with a pointer to [0][1], [0][2], and [0][3].

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Although it's effectively the same thing, strategy b gets my vote as you're dealing with static arrays of a known size.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  4. #4
    Unregistered
    Guest
    We have to do it the way its stated on my message though so thats the problem. How would i start it?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Declare the array of pointers in main() and initialize each element to
    > 0 (zero). Then, for each element of the array of pointers, allocate memory
    > for an array of 15 integers and initialize each element to the value -1.
    I'll show you the declaration and malloc

    Code:
    #include <stdlib.h>
    
    int main ( ) {
       int *arr[3];
    
       // pointless exercise given the following malloc calls
       for ( i = 0 ; i < 3 ; i++ ) arr[i] = 0;  // same as NULL
    
       // 15 ints for each one
       for ( i = 0 ; i < 3 ; i++ ) arr[i] = malloc( 15 * sizeof(int) );
    
       for ( i = 0 ; i < 3 ; i++ ) {
          for ( j = 0 ; j < 15 ; j++ ) {
             arr[i][j] = -1;
          }
       }
    
       // now free the memory
      
       // and finally...
       return 0;
    }

  6. #6
    Unregistered
    Guest
    thank you for the help. Can i now begin with the rest of the program though?

  7. #7
    Unregistered
    Guest
    My code is not working I keep getting all types of errors can someone please help me.

  8. #8
    Unregistered
    Guest
    Help!!
    here is the code:

    #include <stdio.h>
    #define Size 3


    void store_data(int *arr);
    void process(int *arr);

    int main (void)
    {
    int *arr[Size],i,j;


    for(i=0; i<Size; i++)
    arr[i]=0;
    for(i=0; i<Size; i++)
    arr[i] = (int *) (malloc(15*sizeof(int)));
    if(arr == NULL){
    printf("Unable to get memory");
    return 1;
    }
    for(i=0; i<Size; i++) {
    for(j=0; j<15; j++) {
    arr[i][j]=-1;
    }
    }

    store_data(arr);


    free( (void *)arr);

    return 0;
    }

    void store_data(int *arr);
    {
    int i,j;

    for(i=0; i<3; i++) {
    for(j=0; j<15; j++) {
    printf("Enter three integer values: ");
    scanf("%d %d %d", arr[i][j], arr[i][j], arr[i][j]);
    }
    }

    }

    void process(int *arr)
    {
    /* not done yet */
    }

  9. #9
    Unregistered
    Guest
    I've added comments where I believe you should look to find errors.

    Code:
    // I don't know which compiler you are using, but shouldn't you
    // also include stdlib.h or malloc.h?
    #include <stdio.h>
    #define Size 3 
    
    
    void store_data(int *arr); 
    void process(int *arr); 
    
    int main (void) 
    { 
        int *arr[Size],i,j; 
    
    
        for(i=0; i<Size; i++) 
            arr[i]=0; 
    
        for(i=0; i<Size; i++) 
            arr[i] = (int *) (malloc(15*sizeof(int))); 
    
    // in the following don't you want to check each arr[i] to
    // be sure that the memory was allocated?
        if(arr == NULL){
            printf("Unable to get memory");
    
    // if memory was allocated for say, arr[0] and arr[1] but
    // not for arr[2], shouldn't you free the memory for
    // arr[0] and arr[1] before exiting?
            return 1; 
        } 
    
        for(i=0; i<Size; i++) { 
            for(j=0; j<15; j++) { 
            arr[i][j]=-1; 
            } 
        } 
    
        store_data(arr); 
    
    
        free( (void *)arr); 
    
        return 0; 
    } 
    
    
    void store_data(int *arr);  // <--- that  ";" shouldn't be there
    { 
        int i,j; 
    
    // look at the scanf(...) line, aren't you putting
    // all three of the integers that the user entered
    // into the exact same memory location?
    // Read the assignment description again, you should
    // do a total of 15 prompts, 3 integers each time, into
    // consecutive memory locations
        for(i=0; i<3; i++) { 
            for(j=0; j<15; j++) { 
                printf("Enter three integer values: "); 
                scanf("%d %d %d", arr[i][j], arr[i][j], arr[i][j]); 
            } 
        } 
    
    }

  10. #10
    Unregistered
    Guest
    what is teh problem with my function. Everytime i compile on Visual C++ it says that subscript requires array or pointer type one the line of my scanf.

    void store_data(int *arr)
    {
    int i,j;

    for(i=0; i<3; i++) {
    for(j=0; j<15; j++) {
    printf("Enter three integer values: ");
    scanf("%d %d %d", arr[i], arr[i], arr[i]);
    }
    }

    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h> 
    #include <stdlib.h> //!! needed for malloc/free
    
    #define Size 3 
    //!! one for all the 15's as well?
    
    void store_data( int *arr[] ); //!! wrong prototype
    
    int main (void) 
    { 
        int *arr[Size],i,j; 
    
        for(i=0; i<Size; i++) 
            arr[i]=0; 
    
        for(i=0; i<Size; i++) {
            arr[i] = malloc( 15 * sizeof(int) ); 
            if ( arr[i] == NULL ) { //!!
                printf("Unable to get memory"); 
                return 1; 
            }
        } 
    
        for(i=0; i<Size; i++) { 
            for(j=0; j<15; j++) { 
                arr[i][j]=-1; 
            } 
        } 
    
        store_data(arr); 
    
        //!! should be free[i] - to mirror the malloc
        //free( arr ); //!! (if you need the cast, you're compiling c++
    
        return 0; 
    } 
    
    void store_data( int *arr[] )
    { 
        int i,j; 
    
        for(i=0; i<Size; i++) { //!! you defined it, use it!
            for(j=0; j<15; j++) { 
                printf("Enter three integer values: "); 
                scanf("%d %d %d", &arr[i][j], &arr[i][j], &arr[i][j] ); 
            } 
        } 
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM