# Thread: Pointers to arrays problem

1. ## Pointers to arrays problem

Hi Guys. I'm writing a program to accept some values, store them in arrays, then pass these arrays back the to caller to be printed. But when I print, i get weird output. like...0.002.007. Can anyone please help me to identify the problem?

Code:
```void listValues()
{

int i;
char carModelArray[MAX] = {0};
float sLengthArray[MAX] = {0};
float rSlopeArray[MAX]= {0};
float speedArray[MAX] = {0};

for (i = 0; i < 2; i++)
{
printf("%.3f",speedArray[i]);
}
getch();

}

//Module that allows the user to enter the values
void readValues(char (*carModelArray)[MAX],float (*sLengthArray)[MAX],float (*rSlopeArray)[MAX],float (*speedArray)[MAX])
{
char carModel[25];
float speed,sLength = 0,rSlope;
int i = 0;
while(sLength != 999)
{

printf("\nPlease enter the model of the car ");
scanf("%s", &carModel[i]);
strcpy((*&carModelArray)[i],&carModel[i]);

printf("\nEnter the skid length made by the car");
scanf("%f", &sLength);

while(sLengthArray<=0 )
{
printf("\nThe skid length must be greater than zero <0>");
scanf("%f", &sLength);
}
(*sLengthArray)[i] = sLength;

printf("\nEnter the slope of the road ");
scanf("%f", &rSlope);

while(rSlope<-30|| rSlope >30)
{
printf("\nThe slope of the road must be greater than -30 and less than 30");
printf("\nEnter the slope of the road ");
scanf("%d", &rSlope);
}
(*rSlopeArray)[i] = rSlope;
i++;

speed = CalculateSpeed(sLength,rSlope);
(*speedArray)[i] = speed;
}
}

//Module to calculate the speed of the car

float CalculateSpeed(float skd_lgnth, float rd_slp)
{

return speed;

getch();
}```

2. first off idk if this is right
void readValues(char (*carModelArray)[MAX],float (*sLengthArray)[MAX],float (*rSlopeArray)[MAX],float (*speedArray)[MAX]);

shouldnt it be void readValues(char *carModelArray, float *sLengthArray, float *rSlopeArray, float *speedArray)?
because if you use the (*carModelArray) you are derefensing it with out assigning it to another array.
You could say
Code:
``` readValues( carModelArray, sLengthArray, rSlopeArray, speedArray);

void readValues(char *carModelArray, float *sLengthArray, float *rSlopeArray,float *speedArray)
{
char *Array = *carModelArray;
and at the end just switch it.
*carModelArray = *Array
}```
thats the quick fix way but your trying to pass the addresses of the array not the array itself. So if you step through the array you wont know where the end is because its just the address not the actual array.

3. No, it's correct. It's a pointer to an array, as opposed to a pointer to the first element.
However, it is suspect since you use the i variable as the index, and there is only one array, so i = 1 would try to access the second array which does not exist...

4. Thanks so far. Well I'm not sure wut you mean by "one array". readvalues loops until 999 is entered for sLength . And after every loop all arrays change to their second element. I dont get wut u mean by it does not exist. Do you mean in listvalues? Please explain it to me. Thanks alot.

5. I can't figure out why you don't declare our arrays in main(), (or in a function that calls all the others), and then pass them around from there?

You can have all the addresses you want, but if the array itself is out of scope, that's VERY dodgy indeed.

6. You have an array of MAX elements. You pass a pointer to the array into the function.
So if the array's name is a, then a[0] means the 1st array, the 0th element of the array of arrays.
a[1] doesn't exist because you have only one array.

Basically, you can see it like this:
Assume that in the computer memory, you have n arrays laid out after each other. Each array is a storage of n elements of n bytes. Total they take up m bytes. So each array takes up m bytes, located after each other (contiguous) in memory.

Then a[0] would access the array at the memory location where a is pointing, and a[1] would access the array at a + m bytes, and so on.
But you have only one array of m bytes, hence that would be undefined.
You can also create a pointer to the first element of an array, which allows you to travel each element in the array. Then a[0] would refer to the first element, and a[1] to the 2nd, and so on.

7. @ Adak. I have to do it this way.

@ Elysia. Thanks alot for the explanation. I think i understand what you're saying. However, I'm not sure how to go about making more than one array though. I thought i had the format correct =( Help me out please?

8. Do you need more than one array or just fill the individual elements of one array? It seems the later is more likely.

9. i only need to fill the elements of one array. But I have to fill elements into carModelArray, sLengthArray, rSlopeArray, speedArray. I tried using the pointers to simply edit the values by reference. But it doesnt seem to work out well. I think the problem lies within not being able to move on to the next address properly. Can you think of the solution?

10. If you would change to:
Code:
`void readValues(char* carModelArray, ...)`
Then carModelArray[0] would be the first element, carModelArray[1] would be the second element, and so on.
You could also do:
*carModelArray would get the first element.
carModelArray++;
*carModelArray would then get the second element.
Don't forget to pass along the size of the array and make sure that you don't write beyond the end!

11. Hi again. I did wut u advised. But now it tells me, "passing arg 1 of readValues from incompatible pointer type" and it says that for arg 2, 3 and 4 as well.

Code:
```void listValues()
{

int i;

for (i = 0; i < 2; i++)
{
printf("%.f",speedArray[i]);
}
getch();

}

//Module that allows the user to enter the values
void readValues(char *carModelArray,float *sLengthArray,float *rSlopeArray,float *speedArray)
{
char carModel[25];
float speed,sLength = 0,rSlope;

while(sLength != 999)
{

printf("\nPlease enter the model of the car ");
scanf("%s", &carModel);
strcpy(carModelArray,carModel);
carModelArray++;

printf("\nEnter the skid length made by the car");
scanf("%f", &sLength);

while(sLengthArray<=0 )
{
printf("\nThe skid length must be greater than zero <0>");
scanf("%f", &sLength);
}

*sLengthArray = sLength;
sLengthArray++;

printf("\nEnter the slope of the road ");
scanf("%f", &rSlope);

while(rSlope<-30|| rSlope >30)
{
printf("\nThe slope of the road must be greater than -30 and less than 30");
printf("\nEnter the slope of the road ");
scanf("%d", &rSlope);
}
*rSlopeArray = rSlope;
rSlopeArray++;

getch();

system("PAUSE");

speed = CalculateSpeed(sLength,rSlope);
*speedArray = speed;
speedArray++;
// printf("%.3f",(*speedArray)[0]);

}
}```

12. Assuming carModelArray is an array, remove the &,
So &carModelArray becomes carModelArray.