integer array

S

shailashri_sk

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 ?
 
J

Jens.Toerring

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
 
M

Mark R.Bannister

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.
 
M

Michael Steve

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
 
E

Eric Sosman

Mark said:
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.
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.
 
L

lawrence.jones

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
 
J

Jens.Toerring

Michael Steve said:
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
 
M

Michael Steve

Michael Steve said:
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

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
 
E

Eric Sosman

Michael said:
Michael Steve said:
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.
 
M

Mark R.Bannister

Eric Sosman said:
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.
 
P

pete

Mark said:
Eric Sosman said:
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.
 
M

Mark R.Bannister

pete said:
Mark said:
Eric Sosman said:
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?
 
P

pete

Mark said:
pete said:
Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top