By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

integer array

P: n/a
Hi,
int *p;
p++; here p now increments itself with the size of integer.

similarly,
I wanted to know, how to declare an pointer to an array ( say array of
integers)
where in it we do a p++ it increments itself with the size of the
array each time it is incremented with 1.
like
p++; ie p = p + 1; // should increment with 10 if 10 is the size of
the array.

The pointer to an array, should increment the pointer with the size of
an array each time it is incremented with 1. Can we declare such a
pointers ?

one probable solution for this is

#define SIZE_ARRAY
int *p[SIZE_ARRAY][SIZE_ARRAY];

// to increment the pointer with the size of the array.

p = p + SIZE_ARRAY; // increment each time with the size.

but this does not declare a pointer to array as it simply moves to
next location if we do p = p + 1;
I know array is not the primitive data type. but is there a way to do
it ?
Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
sh***********@sify.com wrote:
int *p;
p++; here p now increments itself with the size of integer. similarly,
I wanted to know, how to declare an pointer to an array ( say array of
integers)
where in it we do a p++ it increments itself with the size of the
array each time it is incremented with 1.
like
p++; ie p = p + 1; // should increment with 10 if 10 is the size of
the array. The pointer to an array, should increment the pointer with the size of
an array each time it is incremented with 1. Can we declare such a
pointers ? one probable solution for this is #define SIZE_ARRAY
int *p[SIZE_ARRAY][SIZE_ARRAY];


No, that would be a two-dimensional array of int pointers.

What you need is (I think) something like this:

int a[ SIZE_ARRAY_1 ][ SIZE_ARRAY_2 ];
int ( *p ) [ SIZE_ARRAY_2 ] = &a[ 0 ];

because that's a pointer to an int array of size SIZE_ARRAY_2.
p++ would than be a pointer to a[ 1 ] etc.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

P: n/a
sh***********@sify.com wrote in message news:<62**************************@posting.google. com>...
Hi,
int *p;
p++; here p now increments itself with the size of integer.
No, p is incremented with the size of a data pointer. What you're
saying is sizeof(int) == sizeof(int *) which is not necessarily true,
and I don't think this is what you meant to say.

similarly,
I wanted to know, how to declare an pointer to an array ( say array of
integers)
where in it we do a p++ it increments itself with the size of the
array each time it is incremented with 1.
like
p++; ie p = p + 1; // should increment with 10 if 10 is the size of
the array.


Not possible. What you're asking for are custom-defined pointers.
Data pointers and function pointers are implementation defined, not
user defined.

You could use pointers to pointers ...

int *p[3];

Set *p[0], *p[1] and *p[2] to point to different arrays. Now p++ will
reference the start of each array in turn.

Regards,
Mark.
Nov 14 '05 #3

P: n/a

<sh***********@sify.com> wrote in message
news:62**************************@posting.google.c om...
Hi,
int *p;
p++; here p now increments itself with the size of integer.

similarly,
I wanted to know, how to declare an pointer to an array ( say array of
integers)
where in it we do a p++ it increments itself with the size of the
array each time it is incremented with 1.
like
p++; ie p = p + 1; // should increment with 10 if 10 is the size of
the array.

The pointer to an array, should increment the pointer with the size of
an array each time it is incremented with 1. Can we declare such a
pointers ?

one probable solution for this is

#define SIZE_ARRAY
int *p[SIZE_ARRAY][SIZE_ARRAY];

// to increment the pointer with the size of the array.

p = p + SIZE_ARRAY; // increment each time with the size.

but this does not declare a pointer to array as it simply moves to
next location if we do p = p + 1;
I know array is not the primitive data type. but is there a way to do
it ?


struct p_size_array {int p[SIZE_ARRAY];};

A pointer of this (special limited case of) struct should then behave in the
method you have listed.

Michael Steve
Nov 14 '05 #4

P: n/a
Mark R.Bannister wrote:
sh***********@sify.com wrote in message news:<62**************************@posting.google. com>...
Hi,
int *p;
p++; here p now increments itself with the size of integer.
No, p is incremented with the size of a data pointer. What you're
saying is sizeof(int) == sizeof(int *) which is not necessarily true,
and I don't think this is what you meant to say.


No; `p' has advanced by sizeof(*p) == sizeof(int) bytes
(assuming it was pointing someplace valid to begin with).
sizeof(int*) doesn't come into the picture at all.
similarly,
I wanted to know, how to declare an pointer to an array ( say array of
integers)
where in it we do a p++ it increments itself with the size of the
array each time it is incremented with 1.
like
p++; ie p = p + 1; // should increment with 10 if 10 is the size of
the array.


Not possible.


Most definitely possible. Using typedefs for clarity
(this can be written without them):

typedef int Array[10];
Array x[2]; // x[0] is a ten-int array, x[1] another
Array *p = x; // p points to x[0]
++p; // p points to x[1], ten ints away
What you're asking for are custom-defined pointers.
Data pointers and function pointers are implementation defined, not
user defined.


I'm not sure what you're trying to say here. Pointers
(both kinds) are certainly "user-defined" in the sense that
the user can invent new data and function types and can then
form pointers to those types. The implementation defines
the way those pointers are represented, but that's a red
herring: The user is still able to build types and pointers
that the implementation has never heard of until his source
code came along.

--
Er*********@sun.com

Nov 14 '05 #5

P: n/a
sh***********@sify.com wrote:

I wanted to know, how to declare an pointer to an array ( say array of
integers)
where in it we do a p++ it increments itself with the size of the
array each time it is incremented with 1.


int (*p)[SIZE];

Note that the parentheses are important since without them you get an
array of pointers rather than a pointer to an array. Note also that
pointers to arrays are not usually useful except in conjunction with
multi-dimensional arrays.

-Larry Jones

These things just seem to happen. -- Calvin
Nov 14 '05 #6

P: n/a
Michael Steve <ms****@columbus.rr.com> wrote:
<sh***********@sify.com> wrote in message
one probable solution for this is

#define SIZE_ARRAY
int *p[SIZE_ARRAY][SIZE_ARRAY];

// to increment the pointer with the size of the array.

p = p + SIZE_ARRAY; // increment each time with the size.

but this does not declare a pointer to array as it simply moves to
next location if we do p = p + 1;
I know array is not the primitive data type. but is there a way to do
it ?
struct p_size_array {int p[SIZE_ARRAY];}; A pointer of this (special limited case of) struct should then behave in the
method you have listed.


I wouldn't be sure - there are always those nasty padding bytes. As
far as I can see structures always have to start at an address that
is compatible with strictest alignment requirements on a machine to
be able to create arrays of them, so additional padding bytes might
be required. And then the size of an int array with SIZE_ARRAY ele-
ments could differ from the one of the structure.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #7

P: n/a

<Je***********@physik.fu-berlin.de> wrote in message
news:2p************@uni-berlin.de...
Michael Steve <ms****@columbus.rr.com> wrote:
<sh***********@sify.com> wrote in message
one probable solution for this is

#define SIZE_ARRAY
int *p[SIZE_ARRAY][SIZE_ARRAY];

// to increment the pointer with the size of the array.

p = p + SIZE_ARRAY; // increment each time with the size.

but this does not declare a pointer to array as it simply moves to
next location if we do p = p + 1;
I know array is not the primitive data type. but is there a way to do
it ?

struct p_size_array {int p[SIZE_ARRAY];};

A pointer of this (special limited case of) struct should then behave in
the
method you have listed.


I wouldn't be sure - there are always those nasty padding bytes. As
far as I can see structures always have to start at an address that
is compatible with strictest alignment requirements on a machine to
be able to create arrays of them, so additional padding bytes might
be required. And then the size of an int array with SIZE_ARRAY ele-
ments could differ from the one of the structure.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


A structures alignment is dictated by the first basic data type (and nested
structs) of the struct. In this case, it would be int. The structure is also
a multiple of size int. So the structure requires no padding if an array of
this structure were created. Since all the internal members of the struct
also fall on their respective alignments, no internal padding within is
necessary. So in this special limited case of struct, the sizes will
coincide.

He asked for a way. He didn't ask for a way that didn't cause headaches.
Thanks for the user beware.

Michael Steve

Nov 14 '05 #8

P: n/a
Michael Steve wrote:
<Je***********@physik.fu-berlin.de> wrote in message
news:2p************@uni-berlin.de...
Michael Steve <ms****@columbus.rr.com> wrote:
struct p_size_array {int p[SIZE_ARRAY];};
A pointer of this (special limited case of) struct should then behave in
the
method you have listed.


I wouldn't be sure - there are always those nasty padding bytes. As
far as I can see structures always have to start at an address that
is compatible with strictest alignment requirements on a machine to
be able to create arrays of them, so additional padding bytes might
be required. And then the size of an int array with SIZE_ARRAY ele-
ments could differ from the one of the structure.


A structures alignment is dictated by the first basic data type (and nested
structs) of the struct.


struct counterexample {
char c;
long double ld;
};

The "first basic data type" here is `char', yet the struct's
alignment requirement is at least as strict as that of a
`long double', and may be stricter.
In this case, it would be int. The structure is also
a multiple of size int.
I guess you mean sizeof(struct p_size_array) is a multiple
of sizeof(int). This is probably true of most implementations,
but is not guaranteed by the C Standard.
So the structure requires no padding if an array of
this structure were created. Since all the internal members of the struct
also fall on their respective alignments, no internal padding within is
necessary. So in this special limited case of struct, the sizes will
coincide.


Same comment: Probably true of most implementations, but
not guaranteed by the language itself.

--
Er*********@sun.com

Nov 14 '05 #9

P: n/a
Eric Sosman <Er*********@sun.com> wrote in message news:<41**************@sun.com>...
typedef int Array[10];
Array x[2]; // x[0] is a ten-int array, x[1] another
Array *p = x; // p points to x[0]
++p; // p points to x[1], ten ints away

Umm, Eric, your solution is (for all intents and purposes) identical
to mine!

If you recall the original question stated "int *p; p++; here p now
increments itself with the size of integer." This statement is not
true, and it assumes the following ...

int p[2];
assert( ( &p[1] - &p[0] ) == sizeof( int ) );

This isn't correct, yes p++ will advance such that it points to the
next integer, but you can make no assumptions about just how far the
pointer has advanced in memory. Like I say, this is implementation
defined.

Regards,
Mark.
Nov 14 '05 #10

P: n/a
Mark R.Bannister wrote:

Eric Sosman <Er*********@sun.com> wrote in message news:<41**************@sun.com>...
typedef int Array[10];
Array x[2]; // x[0] is a ten-int array, x[1] another
Array *p = x; // p points to x[0]
++p; // p points to x[1], ten ints away
Umm, Eric, your solution is (for all intents and purposes) identical
to mine!

If you recall the original question stated "int *p; p++; here p now
increments itself with the size of integer." This statement is not
true, and it assumes the following ...

int p[2];
assert( ( &p[1] - &p[0] ) == sizeof( int ) );

This isn't correct,


It's not correct because the types of &p[1] and &p[0]
are pointer to int, and the difference between them, is 1.
yes p++ will advance such that it points to the
next integer, but you can make no assumptions about just how far the
pointer has advanced in memory. Like I say, this is implementation
defined.


if you have
int p[2];
then
(char *)(p + 1) - (char *)(p) == sizeof(int)

The sizeof an array of N int, is (N * sizeof(int)), exactly.
The result of the sizeof operator, is in bytes.

--
pete
Nov 14 '05 #11

P: n/a
pete <pf*****@mindspring.com> wrote in message news:<41***********@mindspring.com>...
Mark R.Bannister wrote:

Eric Sosman <Er*********@sun.com> wrote in message news:<41**************@sun.com>...
typedef int Array[10];
Array x[2]; // x[0] is a ten-int array, x[1] another
Array *p = x; // p points to x[0]
++p; // p points to x[1], ten ints away


Umm, Eric, your solution is (for all intents and purposes) identical
to mine!

If you recall the original question stated "int *p; p++; here p now
increments itself with the size of integer." This statement is not
true, and it assumes the following ...

int p[2];
assert( ( &p[1] - &p[0] ) == sizeof( int ) );

This isn't correct,


It's not correct because the types of &p[1] and &p[0]
are pointer to int, and the difference between them, is 1.
yes p++ will advance such that it points to the
next integer, but you can make no assumptions about just how far the
pointer has advanced in memory. Like I say, this is implementation
defined.


if you have
int p[2];
then
(char *)(p + 1) - (char *)(p) == sizeof(int)

The sizeof an array of N int, is (N * sizeof(int)), exactly.
The result of the sizeof operator, is in bytes.

So you never get any word alignment problems with arrays?
Nov 14 '05 #12

P: n/a
Mark R.Bannister wrote:

pete <pf*****@mindspring.com> wrote in message news:<41***********@mindspring.com>...
Mark R.Bannister wrote:

Eric Sosman <Er*********@sun.com> wrote in message news:<41**************@sun.com>...
> typedef int Array[10];
> Array x[2]; // x[0] is a ten-int array, x[1] another
> Array *p = x; // p points to x[0]
> ++p; // p points to x[1], ten ints away

Umm, Eric, your solution is (for all intents and purposes) identical
to mine!

If you recall the original question stated "int *p; p++; here p now
increments itself with the size of integer." This statement is not
true, and it assumes the following ...

int p[2];
assert( ( &p[1] - &p[0] ) == sizeof( int ) );

This isn't correct,


It's not correct because the types of &p[1] and &p[0]
are pointer to int, and the difference between them, is 1.
yes p++ will advance such that it points to the
next integer, but you can make no assumptions about just how far the
pointer has advanced in memory. Like I say, this is implementation
defined.


if you have
int p[2];
then
(char *)(p + 1) - (char *)(p) == sizeof(int)

The sizeof an array of N int, is (N * sizeof(int)), exactly.
The result of the sizeof operator, is in bytes.


So you never get any word alignment problems with arrays?


Not since I've learned how to avoid alignment problems with arrays.

If you have
char array[sizeof(int)];
then
*(int *)array = 0;
might give you an alignment problem.
In terms of portable code, it's a definite alignment problem.

But, if you have
int array_2[1];
then
array_2[0] = 0;
is fine.
There is no padding between elements in an array.
All of the elements in an array are the same size.
All of the elements in an array are contiguous.
(sizeof array / sizeof *array) equals the number of elements in array.
--
pete
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.