# Thread: Array of pointers help

1. ## 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. 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. Although it's effectively the same thing, strategy b gets my vote as you're dealing with static arrays of a known size.

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

5. > 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. thank you for the help. Can i now begin with the rest of the program though?

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

8. 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. 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. 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. 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] );
}
}

}```