array question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Indy Tech

    #1

    array question

    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


  • Surendra Singhi

    #2
    Re: array question

    Indy Tech wrote:
    [color=blue]
    > 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
    >
    >[/color]
    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.

    --
    Surendra Singhi


    Comment

    • David Lindauer

      #3
      Re: array question



      Surendra Singhi wrote:
      [color=blue]
      > Indy Tech wrote:
      >[color=green]
      > > 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
      > >
      > >[/color]
      > 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.
      >[/color]

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



      Comment

      • Nick Forrington

        #4
        Re: array question

        Indy Tech wrote:[color=blue]
        > 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
        >
        >[/color]
        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...

        Comment

        • Watson Davis

          #5
          Re: array question

          "Indy Tech" <indytechREMOVE @att.net> wrote in
          news:Pfbnd.8616 $7o7.6812@newss vr16.news.prodi gy.com:
          [color=blue]
          > 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++?[/color]

          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

          Comment

          • Jonathan Mcdougall

            #6
            Re: array question

            > I have a vague memory of a way to define arrays that they do not contain a[color=blue]
            > 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++?[/color]

            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

            Comment

            • Alwyn

              #7
              Re: array question

              In article <Xns95A5E7BB44F D9thepencilneck yahooco@206.66. 12.207>, Watson
              Davis <watson@watsonm usic.com> wrote:[color=blue]
              >
              > 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.[/color]

              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

              Comment

              • Ingo Buescher

                #8
                Re: array question

                Indy Tech wrote:
                [color=blue]
                > 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++?[/color]
                [color=blue]
                > JP[/color]
                [color=blue]
                >[/color]

                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_SUC CESS);
                }

                IB
                --
                =============== =============== =============== =============== ===============
                Ingo Buescher <usenet-2004@webmounty. de>
                "To get something done, a committee should consist of no more than three
                men, two of them absent." -- unknown

                Comment

                • Karl Heinz Buchegger

                  #9
                  Re: array question

                  Ingo Buescher wrote:[color=blue]
                  >
                  > Indy Tech wrote:
                  >[color=green]
                  > > 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++?[/color]
                  >[color=green]
                  > > JP[/color]
                  >[color=green]
                  > >[/color]
                  >
                  > 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_SUC CESS);
                  > }[/color]

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

                  --
                  Karl Heinz Buchegger
                  kbuchegg@gascad .at

                  Comment

                  • beliavsky@aol.com

                    #10
                    Re: array question

                    David Lindauer <camille@bluegr ass.net> wrote in message news:<419D4EB2. EE08E42D@bluegr ass.net>...[color=blue]
                    > Surendra Singhi wrote:
                    >[color=green]
                    > > Indy Tech wrote:
                    > >[color=darkred]
                    > > > 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
                    > > >
                    > > >[/color]
                    > > 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.[/color][/color]

                    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 multidimensiona l arrays. Fortran, especially
                    Fortran 90 and later versions, is better at handling arrays than
                    almost any other language.

                    Comment

                    • James Dennett

                      #11
                      Re: array question

                      Alwyn wrote:[color=blue]
                      > In article <Xns95A5E7BB44F D9thepencilneck yahooco@206.66. 12.207>, Watson
                      > Davis <watson@watsonm usic.com> wrote:
                      >[color=green]
                      >>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.[/color]
                      >
                      >
                      > 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";[/color]

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

                      if I recall correctly from those dark days.
                      [color=blue]
                      > and the compiler would insert the length were the '\p' is.
                      >
                      > Of course, this is no use for strings that are longer than 255.[/color]

                      (on an 8-bit machine).

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

                      -- James

                      Comment

                      • Alwyn

                        #12
                        Re: array question

                        In article <sKond.150993$h j.92232@fed1rea d07>, James Dennett
                        <jdennett@acm.o rg> wrote:
                        [color=blue]
                        > Alwyn wrote:[color=green]
                        > >
                        > > const char* pString = "\pSome string";[/color]
                        >
                        > unsigned char * const pString = "\pLength preceded string";
                        >
                        > if I recall correctly from those dark days.[/color]

                        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 * ConstStr255Para m;

                        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

                        Comment

                        • Dan Chirillo

                          #13
                          Re: array question

                          You can do it in VB (pre-.net versions): dim states(1 to 50 ) as string

                          You cannot do it in C/C++.




                          Hello beliavsky@aol.c om,
                          [color=blue]
                          > David Lindauer <camille@bluegr ass.net> wrote in message
                          > news:<419D4EB2. EE08E42D@bluegr ass.net>...
                          >[color=green]
                          >> Surendra Singhi wrote:
                          >>[color=darkred]
                          >>> Indy Tech wrote:
                          >>>
                          >>>> 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.[/color][/color]
                          > 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 multidimensiona l arrays. Fortran, especially
                          > Fortran 90 and later versions, is better at handling arrays than
                          > almost any other language.
                          >[/color]

                          Comment

                          • Ed Dore [MSFT]

                            #14
                            Re: array question

                            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.


                            Comment

                            • Richard Vannoy

                              #15
                              Re: array question

                              >>I doubt if there is a way to do that in C++ or C.[color=blue][color=green]
                              >>Definitely not in Basic.[/color][/color]

                              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

                              Comment

                              Working...