array question

I

Indy Tech

howdy all,

I am getting back into programming after a long enough absence that the
whole landscape has changed. I'm currently working with GCC 3.2.2 and
Visual C++ 6.0.

I have a vague memory of a way to define arrays that they do not contain a
member array(0), but rather start at 1. Don't even recall what lamguage it
is from. Can anyone tell me if there is a way to do this in C++?

Thanks

JP
 
S

Surendra Singhi

Indy said:
howdy all,

I am getting back into programming after a long enough absence that the
whole landscape has changed. I'm currently working with GCC 3.2.2 and
Visual C++ 6.0.

I have a vague memory of a way to define arrays that they do not contain a
member array(0), but rather start at 1. Don't even recall what lamguage it
is from. Can anyone tell me if there is a way to do this in C++?

Thanks

JP
I doubt if there is a way to do that in C++ or C.
Definitely not in Basic.
Maybe in fortran, I am not sure.
 
D

David Lindauer

Surendra said:
I doubt if there is a way to do that in C++ or C.
Definitely not in Basic.
Maybe in fortran, I am not sure.

you can do it in Pascal I believe...
David
 
N

Nick Forrington

Indy said:
howdy all,

I am getting back into programming after a long enough absence that the
whole landscape has changed. I'm currently working with GCC 3.2.2 and
Visual C++ 6.0.

I have a vague memory of a way to define arrays that they do not contain a
member array(0), but rather start at 1. Don't even recall what lamguage it
is from. Can anyone tell me if there is a way to do this in C++?

Thanks

JP
As far as I know it can't be done in C/C++

maybe you were thinking of Visual Basic?
Dim name(1 To 10) as String
or something like that...
 
W

Watson Davis

howdy all,

I am getting back into programming after a long enough absence that
the whole landscape has changed. I'm currently working with GCC 3.2.2
and Visual C++ 6.0.

I have a vague memory of a way to define arrays that they do not
contain a member array(0), but rather start at 1. Don't even recall
what lamguage it is from. Can anyone tell me if there is a way to do
this in C++?

IIRC, in Pascal, you have to define the beginning and the ending of an
array.

IIRC, in BASIC, you dimension an array and it actually creates one extra.
For example, if you dimension something to 10, it actually goes from 0 to
10 but if all your loops go from 1 to 10, then you don't even know the 0th
subscript exists.

IIRC, in Mumps, you can use either number or string as an array subscript.
Arrays are not dimensioned. You "insert" new things into an array by
assigning to a previously non-existent array location and it puts them in
"in order". If you want your array to go from 1 to 10, then you initialize
it in a loop that goes from 1 to 10.

In C or C++, you can actually create arrays that go from 1-10 using the
same logic that BASIC used. Namely, create the array like (int anarray
[11]) and then ignore the 0th subscript. It's a waste of space and you'll
confuse the hell out of any experienced C or C++ coder, but there's nothing
stopping you from doing it... other than basic humanity and consideration
for your fellow human beings.

HTH.

Watson (the pencil neck) Davis
 
J

Jonathan Mcdougall

I have a vague memory of a way to define arrays that they do not contain a
member array(0), but rather start at 1. Don't even recall what lamguage it
is from. Can anyone tell me if there is a way to do this in C++?

In terms of the C++'s standards features, no, but it is quite easy to
create such an array as a class.

template <typename T>
class BoundedArray
{
public:
BoundsArray(std::size_t low, std::size_T high);

T &operator[](std::size_t index);

// and so on
};

But please, note that in the C++ community (and most modern languages),
programmers are used to work with 0-based arrays.


Jonathan
 
A

Alwyn

Watson said:
In C or C++, you can actually create arrays that go from 1-10 using the
same logic that BASIC used. Namely, create the array like (int anarray
[11]) and then ignore the 0th subscript. It's a waste of space and you'll
confuse the hell out of any experienced C or C++ coder, but there's nothing
stopping you from doing it... other than basic humanity and consideration
for your fellow human beings.

Old Macintosh programmers may remember the 'Pascal string' data type,
also known as 'Str255'. This was an array of 'char' with the length at
position 0, so the actual string representation started at position 1.
You could declare one as follows:

const char* pString = "\pSome string";

and the compiler would insert the length were the '\p' is.

Of course, this is no use for strings that are longer than 255.


Alwyn
 
I

Ingo Buescher

Indy said:
I have a vague memory of a way to define arrays that they do not contain a
member array(0), but rather start at 1. Don't even recall what lamguage it
is from. Can anyone tell me if there is a way to do this in C++?

thinking of something like this?

#include <stdlib.h>
int main(int argc, char *argv[])
{
int array[10];
int *ptr = &array[0]-1;
ptr[1] = 123;
printf("Result: %i", array[0]);
return(EXIT_SUCCESS);
}

IB
 
K

Karl Heinz Buchegger

Ingo said:
Indy said:
I have a vague memory of a way to define arrays that they do not contain a
member array(0), but rather start at 1. Don't even recall what lamguage it
is from. Can anyone tell me if there is a way to do this in C++?

thinking of something like this?

#include <stdlib.h>
int main(int argc, char *argv[])
{
int array[10];
int *ptr = &array[0]-1;
ptr[1] = 123;
printf("Result: %i", array[0]);
return(EXIT_SUCCESS);
}

Maybe he think of something like that.
But beware: It is undefined bahaviour to do that.
 
B

beliavsky

In Fortran, the lower bound of arrays is 1 by default, but other
integers can be specified.

real x(3),y(-1:2)

declares arrays with elements [x(1),x(2),x(3)] and
[y(-1),y(0),y(1),y(2)]

This generalizes to multidimensional arrays. Fortran, especially
Fortran 90 and later versions, is better at handling arrays than
almost any other language.
 
J

James Dennett

Alwyn said:
Watson said:
In C or C++, you can actually create arrays that go from 1-10 using the
same logic that BASIC used. Namely, create the array like (int anarray
[11]) and then ignore the 0th subscript. It's a waste of space and you'll
confuse the hell out of any experienced C or C++ coder, but there's nothing
stopping you from doing it... other than basic humanity and consideration
for your fellow human beings.


Old Macintosh programmers may remember the 'Pascal string' data type,
also known as 'Str255'. This was an array of 'char' with the length at
position 0, so the actual string representation started at position 1.
You could declare one as follows:

const char* pString = "\pSome string";

unsigned char * const pString = "\pLength preceded string";

if I recall correctly from those dark days.
and the compiler would insert the length were the '\p' is.

Of course, this is no use for strings that are longer than 255.

(on an 8-bit machine).

Though variants using two or more bytes for the length also exist(ed).

-- James
 
A

Alwyn

James Dennett said:
unsigned char * const pString = "\pLength preceded string";

if I recall correctly from those dark days.

Yes, you're right. 'unsigned char' would be more logical, and, having
looked it up, I find that Apple has defined:

typedef unsigned char Str255[256];
typedef const unsigned char * ConstStr255Param;

Space could be saved by having shorter variants, like Str63 and Str31.
This 'legacy' is, of course, due to the fact that an extended Pascal
was for a long period the system programming language of choice at
Apple, and code examples in even the later volumes of their 'Inside
Macintosh' series were written with the Pascal version first and the C
one second.


Alwyn
 
E

Ed Dore [MSFT]

Hi JP,

I seem to recall an OPTION BASE statement that you could use in earlier
basic programs, that would allow you to create 0 or 1 based arrays. This
particular feature wasn't carried over to VB .Net, and doesn't appear in
other languages like VC++ or C#.

Sincerely,
Ed Dore [MSFT]

This post is 'AS IS' with no warranties, and confers no rights.
 
R

Richard Vannoy

I doubt if there is a way to do that in C++ or C.
incorrect...

In Basic or Visual Basic 6.0 you can do

OPTION BASE 1

in the form level code. This starts all arrays at 1 vice 0. or...

Dim Whatever(1 to MAX) as Variabletype
 

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

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top