473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

integer array

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
12 2205
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
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

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

<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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
10
by: Peter Stojkovic | last post by:
I want store an integer-array of 1000 Values in a blob in a SQL-database. I will do this every 10 Seconds. How can I do this ???? What datatypes a have to use ??? Thanks
2
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some...
1
by: Sivaraman.S | last post by:
Hi, Can i pass integer array to methodInfo.Invoke(obj,args()). I am able to pass only string array to this function. This is the code i have written. Dim myType As Type = objClass.GetType()...
2
by: Eric A. Johnson | last post by:
I'm trying to figure out if there is any way to create a Property for a private array member. I have tried it like this: ' Property for the length of a side Public Property SideLength(ByVal...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
4
by: msosno01 | last post by:
I have Java client that connects to C++ server. The client sends integer in binary using DataOutputStream write function. I am reading these data into buffer. I have to convert this buffer back...
17
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1,...
1
by: Bob Palank | last post by:
' Given Dim v(19) As Integer ' a 20 element array Dim vSorted(19) As Integer Dim i As Integer ' The array v() is filled with random numbers
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.