f



Pointer-to-pointer-to-pointer question

The code example below shows the dynamic allocation of a 2D array.  I
must admit that it took quite a while for me to get there (I already
have another posting to that effect), but I am glad that I finally got
it working.  Now here's the problem:

I am able to get the 2D array dynamically allocated correctly as long
as I am doing it "in-line"  (i.e. without invoking any function).  The
moment I try to do it in another function, I get a core dump.  Any help
will be appreciated.

Since this function is expected to update a pointer-to-pointer type, I
am actually passing the pointer-to-pointer-to-pointer type to the
function.  What am I missing here?

You can see that the source code works correctly when I am perform 2D
array initialization "in-line" (i.e. by not invoking a function
call) simply by un-commenting the line

/* #define INLINE_INIT */

Masood

/******************************************************/
#include <stdio.h>
#include <stdlib.h>

/*#define INLINE_INIT*/

#define MAXROWS    3
#define MAXCOLS    5


void
buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
{
*tblPtr = (int **)malloc(numRows*sizeof(int*));
/* C++ : *tblPtr = new (int*)[numRows]; */

for(size_t i = 0; i < numCols; i++)
*tblPtr[i] = (int *)malloc(numCols*sizeof(int));
/* C++: *tblPtr[i] = new (int)[numCols]; */
}


main()
{
int startVal = 5;

int **tbl;

#ifdef INLINE_INIT
tbl = (int **)malloc(MAXROWS*sizeof(int*));
/* C++ : tbl = new (int*)[MAXROWS]; */

for(size_t i = 0; i < MAXCOLS; i++)
tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl[i] = new (int)[MAXCOLS]; */
#else
buildTbl(&tbl, MAXROWS, MAXCOLS);
#endif

for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
tbl[row][col] = startVal++;

for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
printf("Row: %d, Col: %d => %d\n",
row, col, tbl[row][col]);
  return 0;
}

0
2/3/2005 4:26:42 AM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

10 Replies
1057 Views

Similar Articles

[PageSpeed] 12

masood.iqbal@lycos.com wrote:

> Masood
> 
> /******************************************************/
> #include <stdio.h>
> #include <stdlib.h>
> 
> /*#define INLINE_INIT*/
> 
> #define MAXROWS    3
> #define MAXCOLS    5
> 
> 
> void
> buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
> {
> *tblPtr = (int **)malloc(numRows*sizeof(int*));
> /* C++ : *tblPtr = new (int*)[numRows]; */
> 
> for(size_t i = 0; i < numCols; i++)
> *tblPtr[i] = (int *)malloc(numCols*sizeof(int));

(*tblPtr)[i] = ...

Note that x = malloc(n * sizeof *x);
is preferred to x = (type *) malloc(n * sizeof(type);
You should check that malloc doesn't return NULL.
You could change the return type of buildTbl to int ** and
return the array that way instead of through the parameter.

Do I remember that stuff Ok? Nice to see most of you are 
still here!

Tobias

> /* C++: *tblPtr[i] = new (int)[numCols]; */
> }
> 
> 
> main()
> {
> int startVal = 5;
> 
> int **tbl;
> 
> #ifdef INLINE_INIT
> tbl = (int **)malloc(MAXROWS*sizeof(int*));
> /* C++ : tbl = new (int*)[MAXROWS]; */
> 
> for(size_t i = 0; i < MAXCOLS; i++)
> tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
> /* C++: tbl[i] = new (int)[MAXCOLS]; */
> #else
> buildTbl(&tbl, MAXROWS, MAXCOLS);
> #endif
> 
> for(size_t row = 0; row < MAXROWS; row++)
> for(size_t col = 0; col < MAXCOLS; col++)
> tbl[row][col] = startVal++;
> 
> for(size_t row = 0; row < MAXROWS; row++)
> for(size_t col = 0; col < MAXCOLS; col++)
> printf("Row: %d, Col: %d => %d\n",
> row, col, tbl[row][col]);
>   return 0;
> }

0
tobiasoed (9)
2/3/2005 6:55:44 AM
masood.iqbal@lycos.com wrote:
> The code example below shows the dynamic allocation of a 2D array.  I
> must admit that it took quite a while for me to get there (I already
> have another posting to that effect), but I am glad that I finally got
> it working.  Now here's the problem:
> 
> I am able to get the 2D array dynamically allocated correctly as long
> as I am doing it "in-line"  (i.e. without invoking any function).  The
> moment I try to do it in another function, I get a core dump.  Any help
> will be appreciated.
> 
> Since this function is expected to update a pointer-to-pointer type, I
> am actually passing the pointer-to-pointer-to-pointer type to the
> function.  What am I missing here?

You can get lost with the intricacies of C.
One "trick" to get around it: Use a local "pointer-to-pointer type"
variable to do as you always did and assign its value to the object
your "pointer-to-pointer-to-pointer type" argument points to.
-> Essentially the same code, one additional line.
(You perform all operations on *tblPtr which essentially means that
you could also work with a int **tbl and put *tblPtr=tbl at the end
if everything worked out)
If you follow Tobias's advice, it becomes even easier.

> You can see that the source code works correctly when I am perform 2D
> array initialization "in-line" (i.e. by not invoking a function
> call) simply by un-commenting the line
> 
> /* #define INLINE_INIT */
> 
> Masood
[snip: Code]

Tobias Oed has already mentioned everything I can see.
Follow _every_ point of his advice regarding malloc().

BTW: Thank you for providing a good minimal example!


Cheers
  Michael
-- 
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
0
Michael.Mair (1492)
2/3/2005 7:38:49 AM
masood.iqbal@lycos.com wrote:

> void
> buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
> {
> *tblPtr = (int **)malloc(numRows*sizeof(int*));
> /* C++ : *tblPtr = new (int*)[numRows]; */
> 
> for(size_t i = 0; i < numCols; i++)

The condition should be i < numRows.

> *tblPtr[i] = (int *)malloc(numCols*sizeof(int));

This will access memory not belonging to tblPtr if i is greater than 0.
Change it to the following:
(*tblPtr)[i] = malloc(numCols*sizeof(int));
It's also a good idea to check the return value of malloc().


Christian
0
2/3/2005 7:44:33 AM
masood.iqbal@lycos.com wrote:
> 
> /* #define INLINE_INIT */
> 
> Masood

> void
> buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
> {
> *tblPtr = (int **)malloc(numRows*sizeof(int*));
> /* C++ : *tblPtr = new (int*)[numRows]; */
> 
> for(size_t i = 0; i < numCols; i++)
> *tblPtr[i] = (int *)malloc(numCols*sizeof(int));
   (*tblPtr)[i] = (int *)malloc(numCols*sizeof(int));  <------
> /* C++: *tblPtr[i] = new (int)[numCols]; */
> }
> 

I am not a master of C, but what i can see is the problem of precedence.
And yes you should check the return value of malloc also.
0
2/3/2005 8:12:46 AM
masood.iqbal@lycos.com wrote:
> The code example below shows the dynamic allocation of a 2D array.  I
> must admit that it took quite a while for me to get there (I already
> have another posting to that effect), but I am glad that I finally got
> it working.  Now here's the problem:
> 
> I am able to get the 2D array dynamically allocated correctly as long
> as I am doing it "in-line"  (i.e. without invoking any function).  The
> moment I try to do it in another function, I get a core dump.  Any help
> will be appreciated.
> 
> Since this function is expected to update a pointer-to-pointer type, I
> am actually passing the pointer-to-pointer-to-pointer type to the
> function.  What am I missing here?
> 
> You can see that the source code works correctly when I am perform 2D
> array initialization "in-line" (i.e. by not invoking a function
> call) simply by un-commenting the line
> 
> /* #define INLINE_INIT */
> 
> Masood
> 
> /******************************************************/
> #include <stdio.h>
> #include <stdlib.h>
> 
> /*#define INLINE_INIT*/
> 
> #define MAXROWS    3
> #define MAXCOLS    5
> 
> 
> void
> buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
> {

Return an int** instead.

> *tblPtr = (int **)malloc(numRows*sizeof(int*));
> /* C++ : *tblPtr = new (int*)[numRows]; */
> 
> for(size_t i = 0; i < numCols; i++)
> *tblPtr[i] = (int *)malloc(numCols*sizeof(int));
> /* C++: *tblPtr[i] = new (int)[numCols]; */
> }

Instead of allocating memory so many times, a better method
would be to call malloc only once to allocate all the
memory you need.  This would improve the efficiency of
your code. (See example below.)

> 
> 
> main()
> {

int main (void)... is better.

> int startVal = 5;
> 
> int **tbl;
> 
> #ifdef INLINE_INIT
> tbl = (int **)malloc(MAXROWS*sizeof(int*));

Don't cast malloc or any other function that
returns a pointer unless you really know what you are
doing.

Should be tbl = malloc(...);

> /* C++ : tbl = new (int*)[MAXROWS]; */
> 
> for(size_t i = 0; i < MAXCOLS; i++)

for (size_t i ... ) won't work with C'89.
Avoid it.

> tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));

See above.

> /* C++: tbl[i] = new (int)[MAXCOLS]; */
> #else
> buildTbl(&tbl, MAXROWS, MAXCOLS);
> #endif
> 
> for(size_t row = 0; row < MAXROWS; row++)
> for(size_t col = 0; col < MAXCOLS; col++)
> tbl[row][col] = startVal++;
> 
> for(size_t row = 0; row < MAXROWS; row++)
> for(size_t col = 0; col < MAXCOLS; col++)
> printf("Row: %d, Col: %d => %d\n",
> row, col, tbl[row][col]);
>   return 0;
> }
> 

I've seen code that did not have proper braces
to block the scope of such for loops.  The problem
I faced was that the formatting of the code was
lost and the code that followed that for
statement was a large "if statement".
Let me tell you, it wasn't easy to reformat that
code manually.  Properly placing braces can save you a lot of time.
Use them.

Additionally, I don't see why, after you've been told before,
you don't use a proper NG client to post.  It is *difficult*
to read unindented code.  Please use a good client.


/* grid_alloc.c. */
#include <stdlib.h>

int **grid_alloc (size_int rows, size_int cols)
{
   int **mem;    /* allocated memory */
   int *p;       /* temporary ptr */
   size_int r;   /* row index */

   mem = malloc (rows * cols * sizeof(int) + rows * sizeof(*mem));
   if(mem != NULL)
   {
     /* memory layout:
      * [ pointers: one pointer to each row of data ][ data ]
      */
     for (r = 0, p = (int*)mem + rows; r < rows; ++r, p += cols)
     {
       mem[r] = p;
     }
   }

   return mem;
}

/* ... */

This allows you to use array syntax to address elements.
Don't forget to use free() when you're done with it, however.

Regards,
Jonathan.

-- 
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"We must do something.  This is something.  Therefore, we must do this."
  - Keith Thompson
0
jburd (21)
2/3/2005 8:17:59 AM
Jonathan Burd wrote:
> masood.iqbal@lycos.com wrote:
> 
>> The code example below shows the dynamic allocation of a 2D array.  I
>> must admit that it took quite a while for me to get there (I already
>> have another posting to that effect), but I am glad that I finally got
>> it working.  Now here's the problem:
>>
>> I am able to get the 2D array dynamically allocated correctly as long
>> as I am doing it "in-line"  (i.e. without invoking any function).  The
>> moment I try to do it in another function, I get a core dump.  Any help
>> will be appreciated.
>>
>> Since this function is expected to update a pointer-to-pointer type, I
>> am actually passing the pointer-to-pointer-to-pointer type to the
>> function.  What am I missing here?
>>
>> You can see that the source code works correctly when I am perform 2D
>> array initialization "in-line" (i.e. by not invoking a function
>> call) simply by un-commenting the line
>>
>> /* #define INLINE_INIT */
>>
>> Masood
>>
>> /******************************************************/
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> /*#define INLINE_INIT*/
>>
>> #define MAXROWS    3
>> #define MAXCOLS    5
>>
>>
>> void
>> buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
>> {
> 
> 
> Return an int** instead.
> 
>> *tblPtr = (int **)malloc(numRows*sizeof(int*));
>> /* C++ : *tblPtr = new (int*)[numRows]; */
>>
>> for(size_t i = 0; i < numCols; i++)
>> *tblPtr[i] = (int *)malloc(numCols*sizeof(int));
>> /* C++: *tblPtr[i] = new (int)[numCols]; */
>> }
> 
> 
> Instead of allocating memory so many times, a better method
> would be to call malloc only once to allocate all the
> memory you need.  This would improve the efficiency of
> your code. (See example below.)
> 
>>
>>
>> main()
>> {
> 
> 
> int main (void)... is better.
> 
>> int startVal = 5;
>>
>> int **tbl;
>>
>> #ifdef INLINE_INIT
>> tbl = (int **)malloc(MAXROWS*sizeof(int*));
> 
> 
> Don't cast malloc or any other function that
> returns a pointer unless you really know what you are
> doing.
> 
> Should be tbl = malloc(...);
> 
>> /* C++ : tbl = new (int*)[MAXROWS]; */
>>
>> for(size_t i = 0; i < MAXCOLS; i++)
> 
> 
> for (size_t i ... ) won't work with C'89.
> Avoid it.
> 
>> tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
> 
> 
> See above.
> 
>> /* C++: tbl[i] = new (int)[MAXCOLS]; */
>> #else
>> buildTbl(&tbl, MAXROWS, MAXCOLS);
>> #endif
>>
>> for(size_t row = 0; row < MAXROWS; row++)
>> for(size_t col = 0; col < MAXCOLS; col++)
>> tbl[row][col] = startVal++;
>>
>> for(size_t row = 0; row < MAXROWS; row++)
>> for(size_t col = 0; col < MAXCOLS; col++)
>> printf("Row: %d, Col: %d => %d\n",
>> row, col, tbl[row][col]);
>>   return 0;
>> }
>>
> 
> I've seen code that did not have proper braces
> to block the scope of such for loops.  The problem
> I faced was that the formatting of the code was
> lost and the code that followed that for
> statement was a large "if statement".
> Let me tell you, it wasn't easy to reformat that
> code manually.  Properly placing braces can save you a lot of time.
> Use them.
> 
> Additionally, I don't see why, after you've been told before,
> you don't use a proper NG client to post.  It is *difficult*
> to read unindented code.  Please use a good client.
> 
> 
> /* grid_alloc.c. */
> #include <stdlib.h>
> 
> int **grid_alloc (size_int rows, size_int cols)
> {
>   int **mem;    /* allocated memory */
>   int *p;       /* temporary ptr */
>   size_int r;   /* row index */

Typos. Should be size_int should be size_t. The editor
I was using to search-n-replace goofed. :|

>   mem = malloc (rows * cols * sizeof(int) + rows * sizeof(*mem));
>   if(mem != NULL)
>   {
>     /* memory layout:
>      * [ pointers: one pointer to each row of data ][ data ]
>      */
>     for (r = 0, p = (int*)mem + rows; r < rows; ++r, p += cols)
>     {
>       mem[r] = p;
>     }
>   }
> 
>   return mem;
> }
> 
> /* ... */
> 
> This allows you to use array syntax to address elements.
> Don't forget to use free() when you're done with it, however.
> 
> Regards,
> Jonathan.
> 


-- 
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"We must do something.  This is something.  Therefore, we must do this."
  - Keith Thompson
0
jburd (21)
2/3/2005 10:15:42 AM
On 2 Feb 2005 20:26:42 -0800, masood.iqbal@lycos.com wrote:

>The code example below shows the dynamic allocation of a 2D array.  I
>must admit that it took quite a while for me to get there (I already
>have another posting to that effect), but I am glad that I finally got
>it working.  Now here's the problem:
>
>I am able to get the 2D array dynamically allocated correctly as long
>as I am doing it "in-line"  (i.e. without invoking any function).  The
>moment I try to do it in another function, I get a core dump.  Any help
>will be appreciated.
>
>Since this function is expected to update a pointer-to-pointer type, I
>am actually passing the pointer-to-pointer-to-pointer type to the
>function.  What am I missing here?
>
>You can see that the source code works correctly when I am perform 2D
>array initialization "in-line" (i.e. by not invoking a function
>call) simply by un-commenting the line
>
>/* #define INLINE_INIT */
>
>Masood
>
>/******************************************************/
>#include <stdio.h>
>#include <stdlib.h>
>
>/*#define INLINE_INIT*/
>
>#define MAXROWS    3
>#define MAXCOLS    5
>
>
>void
>buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
>{
>*tblPtr = (int **)malloc(numRows*sizeof(int*));
>/* C++ : *tblPtr = new (int*)[numRows]; */
>
>for(size_t i = 0; i < numCols; i++)
>*tblPtr[i] = (int *)malloc(numCols*sizeof(int));

This line is interpreted as
	*(tblPtr[i]) = ...

You want 
	(*tblPtr)[i] = ...

The parentheses are not optional.

>/* C++: *tblPtr[i] = new (int)[numCols]; */
>}
>
>
>main()
>{
>int startVal = 5;
>
>int **tbl;
>
>#ifdef INLINE_INIT
>tbl = (int **)malloc(MAXROWS*sizeof(int*));
>/* C++ : tbl = new (int*)[MAXROWS]; */
>
>for(size_t i = 0; i < MAXCOLS; i++)
>tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
>/* C++: tbl[i] = new (int)[MAXCOLS]; */
>#else
>buildTbl(&tbl, MAXROWS, MAXCOLS);
>#endif
>
>for(size_t row = 0; row < MAXROWS; row++)
>for(size_t col = 0; col < MAXCOLS; col++)
>tbl[row][col] = startVal++;
>
>for(size_t row = 0; row < MAXROWS; row++)
>for(size_t col = 0; col < MAXCOLS; col++)
>printf("Row: %d, Col: %d => %d\n",
>row, col, tbl[row][col]);
>  return 0;
>}



<<Remove the del for email>>
0
schwarzb (661)
2/4/2005 1:47:11 AM
On 2 Feb 2005 20:26:42 -0800, masood.iqbal@lycos.com wrote:

>The code example below shows the dynamic allocation of a 2D array.  I
>must admit that it took quite a while for me to get there (I already
>have another posting to that effect), but I am glad that I finally got
>it working.  Now here's the problem:
>
>I am able to get the 2D array dynamically allocated correctly as long
>as I am doing it "in-line"  (i.e. without invoking any function).  The
>moment I try to do it in another function, I get a core dump.  Any help
>will be appreciated.
>
>Since this function is expected to update a pointer-to-pointer type, I
>am actually passing the pointer-to-pointer-to-pointer type to the
>function.  What am I missing here?
>
>You can see that the source code works correctly when I am perform 2D
>array initialization "in-line" (i.e. by not invoking a function
>call) simply by un-commenting the line
>
>/* #define INLINE_INIT */
>
>Masood
>
>/******************************************************/
>#include <stdio.h>
>#include <stdlib.h>
>
>/*#define INLINE_INIT*/
>
>#define MAXROWS    3
>#define MAXCOLS    5
>
>
>void
>buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
>{
>*tblPtr = (int **)malloc(numRows*sizeof(int*));
>/* C++ : *tblPtr = new (int*)[numRows]; */
>
>for(size_t i = 0; i < numCols; i++)
>*tblPtr[i] = (int *)malloc(numCols*sizeof(int));

This line is interpreted as
	*(tblPtr[i]) = ...

You want 
	(*tblPtr)[i] = ...

The parentheses are not optional.

>/* C++: *tblPtr[i] = new (int)[numCols]; */
>}
>
>
>main()
>{
>int startVal = 5;
>
>int **tbl;
>
>#ifdef INLINE_INIT
>tbl = (int **)malloc(MAXROWS*sizeof(int*));
>/* C++ : tbl = new (int*)[MAXROWS]; */
>
>for(size_t i = 0; i < MAXCOLS; i++)
>tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
>/* C++: tbl[i] = new (int)[MAXCOLS]; */
>#else
>buildTbl(&tbl, MAXROWS, MAXCOLS);
>#endif
>
>for(size_t row = 0; row < MAXROWS; row++)
>for(size_t col = 0; col < MAXCOLS; col++)
>tbl[row][col] = startVal++;
>
>for(size_t row = 0; row < MAXROWS; row++)
>for(size_t col = 0; col < MAXCOLS; col++)
>printf("Row: %d, Col: %d => %d\n",
>row, col, tbl[row][col]);
>  return 0;
>}



<<Remove the del for email>>
0
schwarzb (661)
2/4/2005 1:51:30 AM
On 2 Feb 2005 20:26:42 -0800, masood.iqbal@lycos.com wrote:

>The code example below shows the dynamic allocation of a 2D array.  I
>must admit that it took quite a while for me to get there (I already
>have another posting to that effect), but I am glad that I finally got
>it working.  Now here's the problem:
>
>I am able to get the 2D array dynamically allocated correctly as long
>as I am doing it "in-line"  (i.e. without invoking any function).  The
>moment I try to do it in another function, I get a core dump.  Any help
>will be appreciated.
>
>Since this function is expected to update a pointer-to-pointer type, I
>am actually passing the pointer-to-pointer-to-pointer type to the
>function.  What am I missing here?
>
>You can see that the source code works correctly when I am perform 2D
>array initialization "in-line" (i.e. by not invoking a function
>call) simply by un-commenting the line
>
>/* #define INLINE_INIT */
>
>Masood
>
>/******************************************************/
>#include <stdio.h>
>#include <stdlib.h>
>
>/*#define INLINE_INIT*/
>
>#define MAXROWS    3
>#define MAXCOLS    5
>
>
>void
>buildTbl(int ***tblPtr, size_t numRows, size_t numCols)
>{
>*tblPtr = (int **)malloc(numRows*sizeof(int*));
>/* C++ : *tblPtr = new (int*)[numRows]; */
>
>for(size_t i = 0; i < numCols; i++)
>*tblPtr[i] = (int *)malloc(numCols*sizeof(int));

This line is interpreted as
	*(tblPtr[i]) = ...

You want 
	(*tblPtr)[i] = ...

The parentheses are not optional.

>/* C++: *tblPtr[i] = new (int)[numCols]; */
>}
>
>
>main()
>{
>int startVal = 5;
>
>int **tbl;
>
>#ifdef INLINE_INIT
>tbl = (int **)malloc(MAXROWS*sizeof(int*));
>/* C++ : tbl = new (int*)[MAXROWS]; */
>
>for(size_t i = 0; i < MAXCOLS; i++)
>tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
>/* C++: tbl[i] = new (int)[MAXCOLS]; */
>#else
>buildTbl(&tbl, MAXROWS, MAXCOLS);
>#endif
>
>for(size_t row = 0; row < MAXROWS; row++)
>for(size_t col = 0; col < MAXCOLS; col++)
>tbl[row][col] = startVal++;
>
>for(size_t row = 0; row < MAXROWS; row++)
>for(size_t col = 0; col < MAXCOLS; col++)
>printf("Row: %d, Col: %d => %d\n",
>row, col, tbl[row][col]);
>  return 0;
>}



<<Remove the del for email>>
0
schwarzb (661)
2/4/2005 2:06:22 AM
[Side note: the article to which I am posting a follow-up got posted
three times, with three different message-IDs.  Software glitch, I
presume.]

>On 2 Feb 2005 20:26:42 -0800, masood.iqbal@lycos.com wrote:
>>for(size_t i = 0; i < numCols; i++)
>>*tblPtr[i] = (int *)malloc(numCols*sizeof(int));

In article <c0l501ltldv0qfsha0ilq4an00rh7mq282@4ax.com>
Barry Schwarz  <schwarzb@deloz.net> wrote:
>This line is interpreted as
>       *(tblPtr[i]) = ...
>
>You want 
>       (*tblPtr)[i] = ...
>
>The parentheses are not optional.

Right.  Note, however, that if for some reason one finds the
parenthesized form objectionable, (*p)[i] can be rewritten as
p[0][i].  (Likewise, p->field can be rewritten as p[0].field.)

I think this particular rewrite is misleading and would avoid
it myself.  In fact, I would probably use code of the form:

    nonvoid_return_type some_fn(T ***ptable, ...) {
        T **table;

        ... do all the local work with table and table[i] ...

        *ptable = table;
        return other_useful_return_value;
    }

or, as in this particular case (where the function had "void" as
its return type), just return a "T **" value and eliminate the
three-levels-deep pointer argument.
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
0
nospam252 (1722)
2/4/2005 2:57:09 AM
Reply:

Similar Artilces:

pointers, pointers, pointers...
Hi There, I came up with following sample code to demonstrate my issue: #include <18F87K22.h> #device HIGH_INTS=TRUE, adc=16, ICD=TRUE #fuses NOWDT //No Watch Dog Timer #fuses WDT128 //Watch Dog Timer uses 1:128 #fuses HSM //Hi-Speed crystal oscillator #fuses NOBROWNOUT //No brownout reset #fuses NOPLLEN //No PLL enabled #fuses BBSIZ1K //1K words Boot Block size #fuses NOXINST //Extended set extension and Indexed typedef struct{ int8 foo, test; } Th...

Pointers, pointers, pointers.......
Hi group. Asking again some help to better understand the basic principles of RPL.... In RPLMAN they say: >>>In the most general definition of RPL, I is an object pointer >>>pointing to a composite object that is the top of a stack of >>>composite objects called the runstream. >>>R points to the rest of the runstream stack. What is really meant by *the rest of the runstream* ? Is R pointing to the object right after the one pointed by I? Is R pointing to the rest of the stack *as a whole* (how's it possible?) Anything else I didn't catch? >>...

Pointer to pointer or reference to pointer
The way I understand it Reference To pointer as function parameter eg. void RP(int *&rpMem) {} rpMem is actually an alias for the pointer outside the function right? So I would access it exactly with the same syntax just like outside pointer right? e.g. void RP(int &*rpMem) { *rpMem = 125; rpMem++; } int Mem = 123; int *pMem = &Mem; // pMem = for example 5000 *pMem = 124; // Mem = 124; RP(pMem); // Mem = 125, pMem = 5004 Is the above correct? A <a@a.a> wrote: > e.g. > > void RP(int &*rpMem) > { > *rpMem = 125; > rpMem++; &g...

Matlab, pointers and pointers to pointers
Matlab stores matrices using simple pointers in order to allocate memory. Using pointers to pointers, according to my experience, we could obtain the following benefits: - code would run faster (I think up to 70%) - memory would be allocated in a better way Why this solution is not used? There are some limitations? Luigi Luigi: <SNIP longing for pointers - not to heaven - to memory... > Why this solution is not used? There are some limitations... <luigi> a fact of life that we all have to live with: it IS just NOT happening... -or- at least it is not happening since 1984... d...

pointer vs pointer to pointer
AIM: To write a queue with 3 operations. WHAT I GOT: It works PROBLEM: Have a question: Why do "pointer to pointer" in enqueue() and "a pointer" to deleteElement() both work fine ? Will enqueue() work fine if I pass just "a pointer" to it ? Will deleteElement() work fine if I pass "pointer to pointer" ? /* A queue implementataion with the operations that I need: * (1) Add element to the front of the queue * (2) remove an element with a unique ID (string constant) * (3) compare 2 elements (string comparison) * (4) print queue * ...

C to M2 ... Any pointers on Pointer to Pointer conversion?
Hi, I'm trying to do up some socket libraries for XDS. So I'm trying to convert C programs to Modula-2 to test them. In the book Beginning Linux Programming by Neil Matthew and Richard Stones (Wiley) I'm stumped by some code relating to Pointers to Pointers. The original C source is at http://linux.aliboom.com:8000/getname.c.html and since I don't know how well what I am about to paste will be formatted, there is also a copy at http://linux.aliboom.com:8000/getname.mod.html I have two questions, the first one deals with the C code shown shortly after the firs...

pointer to pointer
Its just a very simple and basic program to learn exactly what is described in comments here. Can you provide a better (and short) exercise ? /* A program to learn how to use a pointer to a pointer. Here we take an array of char pointers. A char* in the array, of course, points * to some char. Array has only one element. We change the value of char pointed by array's element using a function. * * VERSION 0.0 */ #include <stdio.h> void replace_char_value(char* [], char); int main(void) { char* arrc[2] = {0}; char c = 'c'; arrc[0] = &c;...

pointer to a pointer
My situation: I have a function that takes a pointer to an FSref as an argument. In the body of the function is a function call that also takes a pointer to an FSRef. I need to pass my original FSRef, but since I have only a pointer to it, I am assuming I need to dereference it twice when I call the second function. However, using something like &&myFSRef in the second function doesn't work. Can anyone tell me the proper way to do this? Thanks. In article <1105802448.cd52b3255e6c95476ce23f7be215a6a3@teranews>, Steven Daedelus <sdaedelus@ira.gov> w...

pointers to pointers
Hi, i understood what pointers are and all stuff, and now i'm at the part with pointers to pointers and my book doesn't really help on this issue. let be: int a,*p,**q; how to make use of **q,*q and q ? Which who's address and/or who's value contains. I would like some examples like p=&a;// p contains the address of a and *p contains it's value but regarding **q,*q respectively q. Thanks. apropo wrote: > Hi, > i understood what pointers are and all stuff, and now i'm at the part > with pointers to pointers and my book doesn't really help on...

Pointers pointers.
I thought I could assign an array of chars to a pointer to pointer. However, when I do the following: #include <stdio.h> int main(void) { char buff[] ="test"; char **output = &buff; return 0; } I get: $gcc -g ptr.c -o ptr ptr.c: In function `main': ptr.c:5: warning: initialization from incompatible pointer type What am I doing wrong here? Chad Chad wrote: > I thought I could assign an array of chars to a pointer to pointer. > However, when I do > the following: > > #include <stdio.h> > > int main(void) { > char buf...

pointer of pointer
Hello, I have to make an array which size of the lines depends of the line. I am sure I am not clear so here is an exemple of what I mean : a is an array of n lines each line contain a different number of elements I have tried to implement something like this a=PTRARR(n) FOR i=0,n-1 DO BEGIN a(i)=Ptr_New ENDFOR First I am not sure it is the best way to compute this, second if yes I don't how to implemente the value of the "a(i,j)" element. If somebody can help me... Best regards, Claire Hey Claire, If you absolutely must have each element of "a" be an array...

Pointers to pointers
How can you use a "pointer to a pointer" as a two dimensional array using 'new' and 'delete' rather than 'malloc' and 'free'? Thanks, Jonas Jonas Cord wrote: > How can you use a "pointer to a pointer" as a two dimensional array > using 'new' and 'delete' rather than 'malloc' and 'free'? Pretty much the same. int main() { int** data; data = new int*[100]; for (int i = 0; i < 100; ++i) data[i] = new int[100]; //use it for (int i = 0; i < 100; ++i) delete []...

More on pointers to pointers.
I suspect I'm missing a broader concept here. Okay, given the following code: #include <stdio.h> int main(void) { char *ptr[] = {"Garbage", "test", "work"}; char **arg; arg = ptr; printf("The string is: %s\n", ptr[1]); return 0; } Why am I not allowed to do something like: arg = &ptr; ? I guess even more to the point, why is this legal? arg = &(*ptr) ; Chad Chad said: > I suspect I'm missing a broader concept here. Okay, given the following > code: > > #include <stdio.h> > > in...

pointer of pointer
Hi I have a structure, B_t is another structure typedef struct{ int len; unsigned char code[12]; B_t *b; }A_t Now I need to pass A_t into a function to evaluate I use pointer of pointer void evaluate(A_t **a) My question is how to evaluate the component of this structure (*a)->b??? seems not right Thanks a lot! <qianz99@gmail.com> wrote in message news:1178915812.306611.242380@y80g2000hsf.googlegroups.com... > Hi > > I have a structure, B_t is another structure > > typedef struct{ > int len; > unsigned char code[12]; > B_t *b; > }A_t > >...

Web resources about - Pointer-to-pointer-to-pointer question - comp.lang.c

Resources last updated: 2/11/2016 4:09:48 AM