pointer to array vs pointer 
Author Message
 pointer to array vs pointer

Hi

Whats the advantages /disadvantages b/w passing multi dimensional arrays as
pointers and passing them as pointers to arrays. I understand that as the
dimensions increase the first method will be more cumbersome (see the pgm
below). Are there any other differences? Is any method preferred over the
other?

Thanks in advance
Pirabhu

#include <stdio.h>

void simple_ptr(int * ptr);
void ptr_to_arr(int ptr[][5]);

int main(void)
{
        int arr[5][5];
        int i = -1;
        int j = -1;
        size_t size = -1;

        for(i=0;i<5;i++)
        for(j=0;j<5;j++)
        {
                arr[i][j] = 0;
        }

        fprintf(stderr,"\nProgram to demonstrate use of pointers in mul.
dim. arrays");
        size = sizeof(arr);
        fprintf(stderr,"\nValue returned by sizeof operator is %d",size);

        simple_ptr((int *)arr);
        ptr_to_arr(arr);

return 0;

Quote:
}

void simple_ptr(int * ptr)
{
        int i=-1;
        int j=-1;

        fprintf(stderr,"\nThis function receives two dim. array as
pointer");

        fprintf(stderr,"\nThe elements of the array are\n");

        for(i=0;i<5;i++)
        for(j=0;j<5;j++)
        {
                fprintf(stderr,"%d ",ptr[(i*j)]);
        }

Quote:
}

void ptr_to_arr(int (*ptr)[5])
{
        int i=-1;
        int j=-1;

        fprintf(stderr,"\nThis function receives two dim. array as pointer
to array");

        fprintf(stderr,"\nThe elements of the array are\n");

        for(i=0;i<5;i++)
        for(j=0;j<5;j++)
        {
                fprintf(stderr,"%d ",ptr[i][i]);
        }

Quote:
}



Wed, 20 Jul 2005 03:57:03 GMT  
 pointer to array vs pointer
Quote:

> void simple_ptr(int * ptr);
> void ptr_to_arr(int ptr[][5]);

                      ^^^^^^^^
I am not really sure, but this looks invalid. What do you want to say here?

Quote:
>         size_t size = -1;

size_t is an unsigned type. You cant assign negative values to it. And, ahm,
well, why do you assign anything to it if you want to overwrite it later
without using the former assigned value?

Quote:
>         int i=-1;
>         int j=-1;
>         for(i=0;i<5;i++)
>         for(j=0;j<5;j++)

Why do you always declare your variables as -1 if you dont use this -1?? I
think the following is much less confusing:

        int i, j;

        for(i=0; i<5; i++)
        for(j=0; j<5; j++)

or another way

        int i,j;
        i = j = 0;

        for(; j<5; j++)
        for(; i<5; i++)

I'd prefer the first one..

--

PGP Fingerprint: 066E AFD8 5A70 3F4D B33D  C51F 89F5 BB94 65C9 1285
-- Share what you know. Learn what you don't.



Wed, 20 Jul 2005 04:36:29 GMT  
 pointer to array vs pointer

Quote:

> Hi

> Whats the advantages /disadvantages b/w passing multi dimensional arrays
> as pointers and passing them as pointers to arrays. I understand that as
> the dimensions increase the first method will be more cumbersome (see the
> pgm below). Are there any other differences? Is any method preferred over
> the other?

> Thanks in advance
> Pirabhu

> #include <stdio.h>

> void simple_ptr(int * ptr);
> void ptr_to_arr(int ptr[][5]);

> int main(void)
> {
>         int arr[5][5];
>         int i = -1;
>         int j = -1;

That's a matter of taste but I wouldn't initiaize these here. At least not
to -1 but 0.

Quote:
>         size_t size = -1;

size_t is unsigned so this initialises size to SIZE_T_MAX (if that macro
existed). If you have to initialise, use 0.

- Show quoted text -

Quote:

>         for(i=0;i<5;i++)
>         for(j=0;j<5;j++)
>         {
>                 arr[i][j] = 0;
>         }

>         fprintf(stderr,"\nProgram to demonstrate use of pointers in mul.
> dim. arrays");
>         size = sizeof(arr);
>         fprintf(stderr,"\nValue returned by sizeof operator is %d",size);

>         simple_ptr((int *)arr);
>         ptr_to_arr(arr);

> return 0;

> }

> void simple_ptr(int * ptr)
> {
>         int i=-1;
>         int j=-1;

>         fprintf(stderr,"\nThis function receives two dim. array as
> pointer");

>         fprintf(stderr,"\nThe elements of the array are\n");

>         for(i=0;i<5;i++)
>         for(j=0;j<5;j++)
>         {
>                 fprintf(stderr,"%d ",ptr[(i*j)]);

I think you want ptr[i*5+j] here.

- Show quoted text -

Quote:
>         }

> }

> void ptr_to_arr(int (*ptr)[5])
> {
>         int i=-1;
>         int j=-1;

>         fprintf(stderr,"\nThis function receives two dim. array as pointer
> to array");

>         fprintf(stderr,"\nThe elements of the array are\n");

>         for(i=0;i<5;i++)
>         for(j=0;j<5;j++)
>         {
>                 fprintf(stderr,"%d ",ptr[i][i]);

and ptr[i][j] here.

Quote:
>         }

> }

As another alternative you could use an array of pointers to int, and malloc
away. That makes resizing easier.
Tobias.

--
unix http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.programmer.html
clc http://www.eskimo.com/~scs/C-faq/top.html
fclc (french): http://www.isty-info.uvsq.fr/~rumeau/fclc/



Tue, 19 Jul 2005 23:53:34 GMT  
 pointer to array vs pointer
         $ cat test.c
         #include <stdio.h>

         void simple_ptr(int n, int* ptr) {

           fprintf(stderr,
               "This function receives two dimensional array"
               " as pointer\n");

           fprintf(stderr, "The elements of the array are:\n");

           { int i = 0;
             for(i = 0; i < n; ++i) {
               int j = 0;
               for(j = 0; j < 5; ++j) {
                 fprintf(stderr, "%d ",ptr[(5*i+j)]);
                 }
               fprintf(stderr, "\n");
               }
             }
           }

         void ptr_to_arr(int n, int (*ptr)[5]) {

           fprintf(stderr,
               "This function receives two dimensional array"
               " as pointer to array\n");

           fprintf(stderr, "The elements of the array are:\n");

           { int i = 0;
             for(i = 0; i < n; ++i) {
               int j = 0;
               for(j = 0; j < 5; ++j) {
                 fprintf(stderr, "%d ", ptr[i][j]);
                 }
               fprintf(stderr, "\n");
               }
             }
           }

         int main(int argc, char* argv[]) {
           int arr[5][5];
           size_t size = sizeof(arr);

           { int i = 0;
             for(i = 0; i < 5; ++i) {
               int j = 0;
               for(j = 0; j < 5; ++j) {
                 arr[i][j] = 10*i + j;
                 }
               }
             }

           fprintf(stderr,
               "Program to demonstrate use of pointers"
               " in multidimensional arrays\n");
           fprintf(stderr,
               "Value returned by sizeof operator is %d\n", size);

           simple_ptr(5, (int *)arr);
           ptr_to_arr(5, arr);

           return 0;
           }

         $ gcc -O2 -Wall -ansi -pedantic -o test test.c
         $ ./test
         Program to demonstrate use of pointers in multidimensional arrays
         Value returned by sizeof operator is 100
         This function receives two dimensional array as pointer
         The elements of the array are:
         0 1 2 3 4
         10 11 12 13 14
         20 21 22 23 24
         30 31 32 33 34
         40 41 42 43 44
         This function receives two dimensional array as pointer to array
         The elements of the array are:
         0 1 2 3 4
         10 11 12 13 14
         20 21 22 23 24
         30 31 32 33 34
         40 41 42 43 44



Wed, 20 Jul 2005 05:40:21 GMT  
 pointer to array vs pointer

I noticed that I need [i*5 + j] just after posting. Thanks for
the reply.

Thanks
Pirabhu

--
Posted via http://dbforums.com



Wed, 20 Jul 2005 05:47:25 GMT  
 pointer to array vs pointer

Originally posted by Pirabhu Raman

Quote:
> Hi

> Whats the advantages /disadvantages b/w passing multi dimensional
> arrays as
> pointers and passing them as pointers to arrays. I understand
> that as the
> dimensions increase the first method will be more cumbersome
> (see the pgm
> below). Are there any other differences? Is any method preferred
> over the
> other?

> Thanks in advance
> Pirabhu

> #include

> void simple_ptr(int * ptr);
> void ptr_to_arr(int ptr[][5]);

> int main(void)
> {

>         size = sizeof(arr);
>         fprintf(stderr,"\nValue returned by sizeof operator is
>         %d",size);

> size should be printed as unsigned with the result cast appropriately.

> return 0;

> }

> void simple_ptr(int * ptr)
> {
> <snip>
>                 fprintf(stderr,"%d ",ptr[(i*5 + j)]);
> <snip>
> }

> In this method you telling the compiler that what you are passing is a
> pointer to an int.Consequently you had to do the arithmetic (i*5)+j
> depending on the interpretation that you want to give to this pointer.

> void ptr_to_arr(int (*ptr)[5])
> {

> <snip>
>         for(i=0;i<5;i++)
>         for(j=0;j<5;j++)
>         {
>                 fprintf(stderr,"%d ",ptr[j]);
>         }
> <snip>

> In this case you are telling the compiler the right thing. What is
> being passed is a pointer to an array 15 of ints. so i+1 is actually
> going to jump by 15 ints to point to the next row. so ptr[j] gives
> you the desired result as you were truthful to the compiler :-).
> -sridhar

--
Posted via http://dbforums.com


Wed, 20 Jul 2005 06:28:41 GMT  
 pointer to array vs pointer

Quote:

> > void simple_ptr(int * ptr);
> > void ptr_to_arr(int ptr[][5]);
>                       ^^^^^^^^
> I am not really sure, but this looks invalid. What do you want to say here?

It is quite valid.  A function parameter declared
immediately as an array can omit the bound,
and any bound that is specified is ignored,
since actually it is "adjusted" to a pointer to
the element type.  For a multidimensional array,
only the leftmost/outermost level is omittable,
so this looks unpleasantly asymmetric.

--
- David.Thompson 1 now at worldnet.att.net



Fri, 29 Jul 2005 08:46:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

2. Pointer of Pointers was Pointer of arrays...

3. Pointers: return of pointer to array of pointers to main

4. Array of pointers, pointer to array...

5. array pointer/pointer array

6. arrays pointers array of pointers

7. char pointers vs. int pointers

8. Huge pointers vs Far pointers

9. Pointer vs array compile-time distiction

10. Pointer vs array compile-time distinction (macros)

11. pointer vs. array

12. array vs pointer

 

 
Powered by phpBB® Forum Software