int pointer to int array 
Author Message
 int pointer to int array

Hello,

Is it possible to cast a pointer to an integer to an array of integers?
My intention is to reserve a certain amount of integers with malloc. After
that I want to access those integers as if the reserved memory is an array of
integers, so I want to cast the integer pointer to an array of integers. Is
that possible or is there a way to address the reserved memory as an array?

Grtz, Arthur



Sat, 04 Dec 1999 03:00:00 GMT  
 int pointer to int array

Quote:

> Hello,

> Is it possible to cast a pointer to an integer to an array of integers?
> My intention is to reserve a certain amount of integers with malloc. After
> that I want to access those integers as if the reserved memory is an array of
> integers, so I want to cast the integer pointer to an array of integers. Is
> that possible or is there a way to address the reserved memory as an array?

If I'm reading this correctly, the answer is yes:

#include <stdlib.h>

int main()
{
        int *iptr = malloc(4*sizeof(int));
        iptr[0] = 3;
        iptr[1] = 4;
        iptr[2] = 5;
        iptr[3] = 17;
        free(iptr);
        return 0;

Quote:
}

        -- Pete


Sat, 04 Dec 1999 03:00:00 GMT  
 int pointer to int array


Quote:

>Hello,

>Is it possible to cast a pointer to an integer to an array of integers?

No. A pointer can only be cast to another scalar type or to the type void.

Quote:
>My intention is to reserve a certain amount of integers with malloc. After
>that I want to access those integers as if the reserved memory is an array of
>integers, so I want to cast the integer pointer to an array of integers. Is
>that possible or is there a way to address the reserved memory as an array?

I think what you want is to cast the pointer to a ``pointer to an array of
integers''. You can never treat the dynamically allocated memory as an
actual array; all dynamic memory must be referenced through a pointer of
some sort. The question is whether you use a pointer to int, or a pointer
to a whole array of ints.

The more usual method is to displace the int pointer to get to the other
integers, e.g.:

    int *p = malloc(10 * sizeof *p);

    /* ... check results of allocation ...  */

    p[3] = 5;

Less common is this:

    int (*p)[10] = malloc(sizeof *p);

    /* ... check results of allocation ...  */

    (*p)[3] = 5;

The syntax of using pointers to arrays is a little clumsy, since you always
have to parenthesize the *p, since the postfix [] has a greater precedence.
There is nothing like the -> operator for structures to simplify the use.

Pointers to arrays are useful for abstraction and encapsulation purposes.
That is, you can declare an array type as an abstract data type and pass
pointers to that type. Eric Young's encryption library LibDES does this
with its  des_cblock  type, which represents an eight byte cipherblock.

It is declared:

    typedef unsigned char des_cblock[8];

Many LibDES functions take arguments of type ``pointer to des_cblock''.

A pointer to an array can be safely cast to a pointer to the first
element of the array and vice versa. Thus if you were implementing the
LibDES functions, you could cast the pointer to des_block to a pointer
to unsigned char and then work with simpler syntax.



Sat, 04 Dec 1999 03:00:00 GMT  
 int pointer to int array

Quote:

> Hello,

> Is it possible to cast a pointer to an integer to an array of integers?
> My intention is to reserve a certain amount of integers with malloc. After
> that I want to access those integers as if the reserved memory is an array of
> integers, so I want to cast the integer pointer to an array of integers. Is
> that possible or is there a way to address the reserved memory as an array?

> Grtz, Arthur

Arthur,

You certainly can:

  int   *pInt;
  ...
  pInt = malloc( MAXINTS*sizeof(int) );
  ...
  for( i=0; i<MAXINTS; i++ ) {
    sum += pInt[i];
    ...[or]...
    sum += *(pInt+i);
  }
  ...

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys
[Delete the "2" in eMail address to respond]



Sat, 04 Dec 1999 03:00:00 GMT  
 int pointer to int array

Quote:

> Hello,

> Is it possible to cast a pointer to an integer to an array of integers?
> My intention is to reserve a certain amount of integers with malloc. After
> that I want to access those integers as if the reserved memory is an array  
> of integers, so I want to cast the integer pointer to an array of integers.
> Is that possible or is there a way to address the reserved memory as an  >

array?
I thank all people who replied so quickly to my msg, but I forgot to mention
one thing: it's a 2-dimensional array of integers.

Some extra info: the array of integers are being used in a couple of
functions, so I just want to pass the pointer (to this array) to those
functions. Now I know the value of the "2nd dimension" ([][this_one]), which
is btw variable and is passed to the functions as well. Now I tried to define
the function with the pointer to the array and the value of the "2nd
dimension" something like this (it only doesn't work):

Call:

int *m, a;
/* malloc etc. */
matrix_func(m, a);

Function:

int matrix_func(int matrix[][n], int nr_i)

Where 'n' is a variable that indicates the size of the "2nd dimension" (and
is btw equal to 'nr_i').
However, using 'n' in the defintion doesn't seem to be allowed, it must be a
constant.

Greetings, Arthur



Sun, 05 Dec 1999 03:00:00 GMT  
 int pointer to int array

Groovy hepcat Arthur Rinkel was jivin' on Wed, 18 Jun 97 13:31:05 GMT
in comp.lang.c.
int pointer to int array again's a cool scene! Dig it!

Quote:
>I thank all people who replied so quickly to my msg, but I forgot to mention
>one thing: it's a 2-dimensional array of integers.

  Doesn't matter. It still works the same, nomatter how many
dimentions it has. An int is an int is an int, whether it is in a nine
million six hundred and seventy three dimentional array or all on it's
lonesome. :)
  An int pointer will always point to an int. That's all you need to
remember.

Quote:
>Some extra info: the array of integers are being used in a couple of
>functions, so I just want to pass the pointer (to this array) to those
>functions. Now I know the value of the "2nd dimension" ([][this_one]), which
>is btw variable and is passed to the functions as well. Now I tried to define
>the function with the pointer to the array and the value of the "2nd
>dimension" something like this (it only doesn't work):

  You don't need to define the subscripts of array arguments. But if
you do, the subscript should be integral constants, not variables.
  In fact, you could pass a pointer to the data type of the array (ie.
in this case, pointer to int).

Quote:
>int matrix_func(int matrix[][n], int nr_i)

  One more thing: if I were you I'd pass the bounds of both dimentions
of the array to the function. For example:

int matrix_func(int *matrix, int width, int height);

#define ARRAY_WIDTH 80
#define ARRAY_HEIGHT 25
int *array;

array = malloc(ARRAY_WIDTH * ARRAY_HEIGHT * sizeof(int));

...    /* do something to the data array points to */

matrix_func(array, ARRAY_WIDTH, ARRAY_HEIGHT);

  This ensures matrix_func() knows the dimentions of the array so it
doesn't try to access data beyond the end of the array.

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Sat, 11 Dec 1999 03:00:00 GMT  
 int pointer to int array

Groovy hepcat Arthur Rinkel was jivin' on Tue, 17 Jun 97 18:18:44 GMT
in comp.lang.c.
int pointer to int array's a cool scene! Dig it!

Quote:
>Is it possible to cast a pointer to an integer to an array of integers?
>My intention is to reserve a certain amount of integers with malloc. After
>that I want to access those integers as if the reserved memory is an array of
>integers, so I want to cast the integer pointer to an array of integers. Is
>that possible or is there a way to address the reserved memory as an array?

  You don't need to cast anithing here. A pointer to int will point to
a single int or an array of int. For example, to do what you want to
do here (if I understand your intention):

#define ARRAY_LEN 100  /* array length = 100 elements */
int *array;    /* declare a pointer to int */

array = malloc(ARRAY_LEN * sizeof(int));  /* allocate enough memory
for array of 100 int elements */

  Then to access an element of the array, you can simply use it the
same way you would any other array:

i = array[element];  /* assuming i and element have been declared, and
element is initialised to a valid subscript of array */

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Sat, 11 Dec 1999 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. int func(int) versus int func(int *) efficacy.

2. int to int array problem

3. (int/int) != int

4. Calling int f(int (*f)(int)) like function in DLL from VB

5. How to do int func(int size,int matrix[][size])

6. how to modify a pointer to an array one sizeof(int)

7. Sorting array of pointers to int

8. pointer to int array question

9. passing of pointers to int arrays?

10. Returning Int array pointers from functions

11. initializing array of pointers to int

12. extern int foo(int); vs int foo(int);

 

 
Powered by phpBB® Forum Software