I am working on a project with pointers and I havethe code working, but I think I might not be understanding pointers very well.
I am creating a two dimensional array with dynamic memory allocation and I think I am accessing the pointers wrong.
The question Comes down to how I am sending information into my simulated display. I have the expanded code below but here is a simplified version.
int* pToInt;
pToInt = (int*) malloc (sizeof(int) * 4) ;
Now how should I place the values into these pointers
pToInt[0] = 19 ; // Is this placing 19 into pToInt or in this way am I telling it to point to memory address 19. I ask this because if I
printf("%d",pToInt[0]);
I get 19.
Should I be placing all my values in like this
*pToInt[0] = 19 ;
printf("%d",*pToInt[0]);
Please look over my code below and explain. The code works and gives me the right results I just think I am doing it the wrong way and If I am not can you please explain why that works.
Here is the code with some comments.
I just cut the relevant code in sections.
float** pTable = NULL ;
// Allocate Memory For Simulated Multidimensional Array
/* This next section of code takes a pointer to pointer of type float and allocates iNumberOfSprokets * iNumberChainWheels of them */
if ( ( pTable = (float **) malloc ( ( sizeof( float * ) * (iNumberOfSprokets * iNumberChainWheels ) ) ) ) == NULL )
{
printf(" \n Unable To Allocate Memory For The Table.");
return 0;
}
/* The next section of code allocates enough memory for pTable[0] to hold 4 * iNumberOfSpokets * iNumberChainWheels of floats. So right now if there was
3 chain wheels and 2 spokets,I have space to hold 24 floats. */
if ( ( pTable[0]= (float *) malloc ( ( sizeof( float ) * (4 * iNumberOfSprokets * iNumberChainWheels ) ) ) ) == NULL )
{
printf(" \n Unable To Allocate Memory For The Table.");
return 0;
}
// Set Pointer Locations
/* This next section takes the remaining pointers pTable[iLoopVariable] and assignes them every (sizeof(float) * ColumnWidth Of Table ( Which is 4) * the loopvariable. */
for ( iLoopVariable = 1 ; iLoopVariable < (iNumberOfSprokets * iNumberChainWheels ) ; iLoopVariable++ )
{
pTable[iLoopVariable] = pTable[0] + ( ( sizeof(float) * 4 * iLoopVariable ) ) ;
}
// Put Values Into The pointers;
/* This next section places the values held into a pointer array and fills the table. Am I placing the info into the pTable the correct way?? */
gear = 1; // resetgear
for ( iLoopVariable = 0; iLoopVariable < iNumberChainWheels ; iLoopVariable++ )
{
for ( tempsproket = iNumberOfSprokets; tempsproket > 0 ; tempsproket--)
{
pTable[gear-1][0] = gear ;
pTable[gear-1][1] = pChainWheelSizes[iLoopVariable] ;
pTable[gear-1][2] = pSproketSizes[tempsproket-1] ;
pTable[gear-1][3] = 1.0 ;
gear++;
}
}
//Display Table To Screen from table pointer
gear=1;
printf("Table To Screen \n");
for ( iLoopVariable = 0; iLoopVariable < iNumberChainWheels ; iLoopVariable++ )
{
for ( tempsproket = iNumberOfSprokets; tempsproket > 0 ; tempsproket--)
{
printf(" %.0f %.0f %.0f %.0f \n", pTable[gear-1][0],pTable[gear-1][1],pTable[gear-1][2],pTable[gear-1][3]);
gear++;
}
}
Thank You For Your Time.
Darrell