pass an array throuh a function

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

    pass an array throuh a function

    Hello,
    can anyone tell me how to pass an array to a function ?
    I have this function , part of my class.
    It works if I do not put in int a[i] everywhere , but obviously , I need to
    add an array so I can keep everything neat and tidy, And to call the array
    whenever I want

    thanks
    kenny


    void Setting::SetCyl inder(int r, int h, int s, int a[i])
    {
    int value;
    for (int i=0; i<count; i++)
    {
    cout<<" Enter a time: " <<endl;
    cin>> value;
    a[i].hours = value;

    cout<<" Enter a minute: " <<endl;
    cin>> value;
    a[i].minutes = value;

    cout<<" Enter a second: " <<endl;
    cin>> value;
    a[i].seconds = value;
    }
    }


  • Leor Zolman

    #2
    Re: pass an array throuh a function

    On Tue, 25 May 2004 18:12:12 -0400, "Kenny" <lerameur@yah00 .c0m> wrote:
    [snip]

    I've replied to your identical post on alt.comp.lang.l earn.c-c++. Please do
    not multi-post; if you /must/ post to multiple groups (and there's seldom
    good reason to do so), at least "cross-post" by including all the groups in
    your To: list at once.
    Thanks,
    -leor

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Unforgiven

      #3
      Re: pass an array throuh a function

      Kenny wrote:[color=blue]
      > Hello,
      > can anyone tell me how to pass an array to a function ?
      > I have this function , part of my class.
      > It works if I do not put in int a[i] everywhere , but obviously , I
      > need to add an array so I can keep everything neat and tidy, And to
      > call the array whenever I want[/color]

      Your code seems to suggest that a should not be an array of int, for one
      thing, so that's wrong.

      You could use something like this:
      -----
      void Setting::SetCYl inder(int r, int h, int s, int a[], int count)
      {
      for (int i=0; i<count; i++)
      {
      /* do something */
      }
      }
      -----
      The disadvantage is that this can lead to hard to trace bugs if you pass a
      'count' value that's not the actual length of the array.

      The safe way is to use std::vector instead of arrays:
      -----
      void Setting::SetCYl inder(int r, int h, int s, std::vector<int > a)
      {
      for (int i=0; i<a.length(); i++)
      {
      /* do something */
      }
      }
      -----

      --
      Unforgiven

      Comment

      • Rolf Magnus

        #4
        Re: pass an array throuh a function

        Kenny wrote:
        [color=blue]
        > Hello,
        > can anyone tell me how to pass an array to a function ?[/color]

        You can't. You can - however - pass a pointer to the array's first
        element or you can pass a reference to an array.
        [color=blue]
        > I have this function , part of my class.
        > It works if I do not put in int a[i] everywhere , but obviously , I
        > need to add an array so I can keep everything neat and tidy, And to
        > call the array whenever I want
        >
        > thanks
        > kenny
        >
        >
        > void Setting::SetCyl inder(int r, int h, int s, int a[i])
        > {
        > int value;
        > for (int i=0; i<count; i++)[/color]

        Where do you get 'count' from?
        [color=blue]
        > {
        > cout<<" Enter a time: " <<endl;
        > cin>> value;
        > a[i].hours = value;
        >
        > cout<<" Enter a minute: " <<endl;
        > cin>> value;
        > a[i].minutes = value;
        >
        > cout<<" Enter a second: " <<endl;
        > cin>> value;
        > a[i].seconds = value;
        > }
        > }[/color]


        Either try:

        void Setting::SetCyl inder(int r, int h, int s, int* a)

        This will pass a pointer to the first element of the array. The rest can
        stay as it was. But you might need to add an additional parameter to
        pass the size.
        Alternatively, if the size is fixed at compile time:

        void Setting::SetCyl inder(int r, int h, int s, int (&a)[size])

        with 'size' being the size of the array.

        Comment

        • Kenny

          #5
          Re: pass an array throuh a function

          I wil try that thanks,

          I also get a syntax error at this line:

          a[i].SetCylinder(20 ,30,40, a[] );

          : : error C2059: syntax error : ']'

          Know what that means ?

          "Rolf Magnus" <ramagnus@t-online.de> wrote in message
          news:c90ks2$37r $06$1@news.t-online.com...[color=blue]
          > Kenny wrote:
          >[color=green]
          > > Hello,
          > > can anyone tell me how to pass an array to a function ?[/color]
          >
          > You can't. You can - however - pass a pointer to the array's first
          > element or you can pass a reference to an array.
          >[color=green]
          > > I have this function , part of my class.
          > > It works if I do not put in int a[i] everywhere , but obviously , I
          > > need to add an array so I can keep everything neat and tidy, And to
          > > call the array whenever I want
          > >
          > > thanks
          > > kenny
          > >
          > >
          > > void Setting::SetCyl inder(int r, int h, int s, int a[i])
          > > {
          > > int value;
          > > for (int i=0; i<count; i++)[/color]
          >
          > Where do you get 'count' from?
          >[color=green]
          > > {
          > > cout<<" Enter a time: " <<endl;
          > > cin>> value;
          > > a[i].hours = value;
          > >
          > > cout<<" Enter a minute: " <<endl;
          > > cin>> value;
          > > a[i].minutes = value;
          > >
          > > cout<<" Enter a second: " <<endl;
          > > cin>> value;
          > > a[i].seconds = value;
          > > }
          > > }[/color]
          >
          >
          > Either try:
          >
          > void Setting::SetCyl inder(int r, int h, int s, int* a)
          >
          > This will pass a pointer to the first element of the array. The rest can
          > stay as it was. But you might need to add an additional parameter to
          > pass the size.
          > Alternatively, if the size is fixed at compile time:
          >
          > void Setting::SetCyl inder(int r, int h, int s, int (&a)[size])
          >
          > with 'size' being the size of the array.
          >[/color]


          Comment

          • Alan Johnson

            #6
            Re: pass an array throuh a function

            Kenny wrote:[color=blue]
            > Know what that means ?
            >
            > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
            > news:c90ks2$37r $06$1@news.t-online.com...
            >[color=green]
            >>Kenny wrote:
            >>
            >>[color=darkred]
            >>>Hello,
            >>>can anyone tell me how to pass an array to a function ?[/color]
            >>
            >>You can't. You can - however - pass a pointer to the array's first
            >>element or you can pass a reference to an array.
            >>
            >>[color=darkred]
            >>>I have this function , part of my class.
            >>>It works if I do not put in int a[i] everywhere , but obviously , I
            >>>need to add an array so I can keep everything neat and tidy, And to
            >>>call the array whenever I want
            >>>
            >>>thanks
            >>> kenny
            >>>
            >>>
            >>>void Setting::SetCyl inder(int r, int h, int s, int a[i])
            >>>{
            >>> int value;
            >>> for (int i=0; i<count; i++)[/color]
            >>
            >>Where do you get 'count' from?
            >>
            >>[color=darkred]
            >>> {
            >>> cout<<" Enter a time: " <<endl;
            >>> cin>> value;
            >>> a[i].hours = value;
            >>>
            >>> cout<<" Enter a minute: " <<endl;
            >>> cin>> value;
            >>> a[i].minutes = value;
            >>>
            >>> cout<<" Enter a second: " <<endl;
            >>> cin>> value;
            >>> a[i].seconds = value;
            >>> }
            >>>}[/color]
            >>
            >>
            >>Either try:
            >>
            >>void Setting::SetCyl inder(int r, int h, int s, int* a)
            >>
            >>This will pass a pointer to the first element of the array. The rest can
            >>stay as it was. But you might need to add an additional parameter to
            >>pass the size.
            >>Alternatively , if the size is fixed at compile time:
            >>
            >>void Setting::SetCyl inder(int r, int h, int s, int (&a)[size])
            >>
            >>with 'size' being the size of the array.
            >>[/color]
            >
            > I wil try that thanks,
            >
            > I also get a syntax error at this line:
            >
            > a[i].SetCylinder(20 ,30,40, a[] );
            >
            > : : error C2059: syntax error : ']'
            >[/color]

            Quick array/pointer lesson:

            1. The name of an array, with no subscript, will serve a pointer to the
            first element in the array.

            2. a[i], when 'a' is a pointer, is shorthand for: *(a + i) ... the
            significance of this is that, if you have a pointer to the first element
            in an array, you can access the ith element with a[i].


            So ...

            void Setting::SetCyl inder(int r, int h, int s, int* a, int count)
            {
            int i;
            for (i = 0; i < count; i++)
            a[i] = i; // or whatever you want to do.
            }


            To call this function:

            Setting s;
            int a[50];

            s.SetCylinder(2 0, 30, 40, a, 50) ;



            Alan

            Comment

            • John Harrison

              #7
              Re: pass an array throuh a function


              "Kenny" <lerameur@yah00 .c0m> wrote in message
              news:KKQsc.3982 $9q1.3566@news2 0.bellglobal.co m...[color=blue]
              > I wil try that thanks,
              >
              > I also get a syntax error at this line:
              >
              > a[i].SetCylinder(20 ,30,40, a[] );
              >
              > : : error C2059: syntax error : ']'
              >
              > Know what that means ?
              >[/color]

              It means a[] is illegal, why did you think it was ok? You use the name of
              the array to stand for the array (but actually is is converted to a pointer
              to the start of the array).

              a[i].SetCylinder(20 ,30,40, a);

              The subject of pointers and arrays is very confusing to a newbie, get a good
              C++ book and read it carefully, remember its probably not saying what you
              think its saying.

              john


              Comment

              Working...