![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 13
| Pointers to arrays problem Code: void listValues()
{
int i;
char carModelArray[MAX] = {0};
float sLengthArray[MAX] = {0};
float rSlopeArray[MAX]= {0};
float speedArray[MAX] = {0};
readValues(&carModelArray,&sLengthArray,&rSlopeArray,&speedArray);
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)
{
float slopeRad,slopeTangent;
float speed,cosine,sine,answer;
slopeRad = rd_slp*(pi/180);
slopeTangent = (sin(slopeRad))/(cos(slopeRad));
answer = slopeTangent*(pi/180);
speed= sqrt(30*skd_lgnth*(answer+Brakefactor));
return speed;
getch();
}
|
| key4life is offline | |
| | #2 |
| Registered User Join Date: Feb 2010
Posts: 6
| 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
}
Last edited by kille6525; 02-09-2010 at 04:32 AM. Reason: quick note. |
| kille6525 is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| 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...
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #4 |
| Registered User Join Date: Nov 2009
Posts: 13
| 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. |
| key4life is offline | |
| | #5 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| 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. |
| Adak is offline | |
| | #6 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #7 |
| Registered User Join Date: Nov 2009
Posts: 13
| @ 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? |
| key4life is offline | |
| | #8 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Do you need more than one array or just fill the individual elements of one array? It seems the later is more likely.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #9 |
| Registered User Join Date: Nov 2009
Posts: 13
| 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? |
| key4life is offline | |
| | #10 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| If you would change to: Code: void readValues(char* carModelArray, ...) 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!
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 13
| 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;
readValues(&carModelArray,&sLengthArray,&rSlopeArray,&speedArray);
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]);
}
}
|
| key4life is offline | |
| | #12 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 16,078
| Assuming carModelArray is an array, remove the &, So &carModelArray becomes carModelArray.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x "Thanks Elysia. You're a programming master! How the hell do you know every thing?" "Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff." Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| vector of arrays of pointers to structures | Marksman | C++ Programming | 13 | 02-01-2008 04:44 AM |
| A question related to strcmp | meili100 | C++ Programming | 6 | 07-07-2007 02:51 PM |
| Need Help with pointers and arrays | Dan17 | C++ Programming | 6 | 11-23-2005 04:46 PM |
| Arrays, pointers and strings | Apropos | C++ Programming | 12 | 03-21-2005 11:25 PM |
| Crazy memory problem with arrays | fusikon | C++ Programming | 9 | 01-15-2003 09:24 PM |