zero-based array, in function confucsion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dam_fool_2003@yahoo.com

    zero-based array, in function confucsion

    Hai,
    When I declare a zero-based array I get a compile time error stating
    that zero based array is not possible. But if I declare the same in
    the function argument I don't get any error. Is related with the
    function in C in which the argument's references are passed in the
    stack during the runtime. I am confused.
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: zero-based array, in function confucsion

    dam_fool_2003@y ahoo.com wrote:[color=blue]
    > When I declare a zero-based array I get a compile time error stating
    > that zero based array is not possible. But if I declare the same in
    > the function argument I don't get any error. Is related with the
    > function in C in which the argument's references are passed in the
    > stack during the runtime. I am confused.[/color]

    What do you mean by "zero-based array" and how do you declare it? The
    last time I checked all C arrays where zero-based in the sense that
    the index of the first element is 0 (and not 1 like in several other
    languages).
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oe rring

    Comment

    • Kris Wempa

      #3
      Re: zero-based array, in function confucsion

      Please post your code so we can see what you are trying to do. Thanks.


      <dam_fool_2003@ yahoo.com> wrote in message
      news:a3e883de.0 309290458.1a78b d4@posting.goog le.com...[color=blue]
      > Hai,
      > When I declare a zero-based array I get a compile time error stating
      > that zero based array is not possible. But if I declare the same in
      > the function argument I don't get any error. Is related with the
      > function in C in which the argument's references are passed in the
      > stack during the runtime. I am confused.[/color]


      Comment

      • Mike Wahler

        #4
        Re: zero-based array, in function confucsion


        <dam_fool_2003@ yahoo.com> wrote in message
        news:a3e883de.0 309290458.1a78b d4@posting.goog le.com...[color=blue]
        > Hai,
        > When I declare a zero-based array I get a compile time error stating
        > that zero based array is not possible. But if I declare the same in
        > the function argument I don't get any error. Is related with the
        > function in C in which the argument's references are passed in the
        > stack during the runtime. I am confused.[/color]

        What you're describing is not very clear. Show us
        the code that you're having trouble with.

        -Mike


        Comment

        • dam_fool_2003@yahoo.com

          #5
          Re: zero-based array, in function confucsion

          "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message news:<bl9eqb$2m m7@kcweb01.netn ews.att.com>...[color=blue]
          > Please post your code so we can see what you are trying to do. Thanks.
          >[/color]

          Sorry,
          void foo(int a[]); /*a[] possible*/
          int main(void)
          {
          unsigned int y[]; /*y[] *not possible*/
          return 0;
          }

          Comment

          • Mike Wahler

            #6
            Re: zero-based array, in function confucsion

            <dam_fool_2003@ yahoo.com> wrote in message
            news:a3e883de.0 309291651.58679 68c@posting.goo gle.com...[color=blue]
            > "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message[/color]
            news:<bl9eqb$2m m7@kcweb01.netn ews.att.com>...[color=blue][color=green]
            > > Please post your code so we can see what you are trying to do. Thanks.
            > >[/color]
            >
            > Sorry,[/color]

            So it looks like you're trying to define an array of
            unspecified size (what you called 'zero based').
            Correct terminology is important. You probably
            see that now, from the replies you've got so far.
            [color=blue]
            > void foo(int a[]); /*a[] possible*/[/color]

            Here, 'a' is a function parameter. While it *looks* like
            an array, it is not, it's a pointer. [1]

            (An array cannot be passed to or returned from functions,
            only a pointer to one of its elements can (typically the
            first). The [] form is just an alternate syntax.

            [color=blue]
            > int main(void)
            > {
            > unsigned int y[]; /*y[] *not possible*/[/color]

            No, not possible. Your definition says 'make me an array'
            An array is a sequence of *one or more* contigous objects of
            the same type. But you did not tell it how many. How can
            the compiler make the array? :-) How many 'unsigned ints'
            do you want/need? Just say so, e.g.

            unsigned int y[10]; /* array of ten unsigned ints */
            [color=blue]
            > return 0;
            > }[/color]

            May I ask which textbook(s) you're learning from?


            [1]

            void foo(int a[]);

            vs.

            void foo(int *a);

            Both of these declare that the parame ter'a' is of type
            'pointer to int' (int*). Some prefer the first form, others
            the latter, a case can be made for each.

            When such a function is intended to operate upon an
            array, some say that the a[] form conveys this intent,
            and that the *a form should only be used when passing
            the address of a single object.

            Others say that the a[] only unnecessarily 'disguises'
            the fact that the parameter is really a pointer.

            This seems to me to be a religious issue which I don't
            care to debate. I use the *a form, simply because it's
            a pointer. But that's just *my* style, it's neither right
            nor wrong (unless of course it violates coding standards
            to which I've agreed to ahere).

            HTH,
            -Mike


            Comment

            • Mike Wahler

              #7
              Re: zero-based array, in function confucsion


              "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
              news:VL4eb.9441 $NX3.7723@newsr ead3.news.pas.e arthlink.net...
              [color=blue]
              >[color=green]
              > > void foo(int a[]); /*a[] possible*/[/color]
              >
              > Here, 'a' is a function parameter. While it *looks* like
              > an array, it is not, it's a pointer.[/color]

              Forgot to add, even with a number inside the [],
              it's still not an array. The number will be ignored.
              This is a 'special case' for [], when used with a
              function parameter.

              -Mike


              Comment

              • CBFalconer

                #8
                Re: zero-based array, in function confucsion

                Kris Wempa wrote:[color=blue]
                > <dam_fool_2003@ yahoo.com> wrote in message
                >[color=green]
                > > When I declare a zero-based array I get a compile time error
                > > stating that zero based array is not possible. But if I
                > > declare the same in the function argument I don't get any
                > > error. Is related with the function in C in which the
                > > argument's references are passed in the stack during the
                > > runtime. I am confused.[/color]
                >
                > Please post your code so we can see what you are trying to do.
                > Thanks.[/color]

                Please refrain from top-posting (fixed) in c.l.c. Among other
                things, it is considered rude.

                --
                Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net> USE worldnet address!


                Comment

                • Mark McIntyre

                  #9
                  Re: zero-based array, in function confucsion

                  On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"
                  <mkwahler@mkwah ler.net> wrote:
                  [color=blue]
                  >
                  >"Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
                  >news:VL4eb.944 1$NX3.7723@news read3.news.pas. earthlink.net.. .
                  >[color=green]
                  >>[color=darkred]
                  >> > void foo(int a[]); /*a[] possible*/[/color]
                  >>
                  >> Here, 'a' is a function parameter. While it *looks* like
                  >> an array, it is not, it's a pointer.[/color]
                  >
                  >Forgot to add, even with a number inside the [],
                  >it's still not an array. The number will be ignored.[/color]

                  remind me what happens with multidimensiona l arrays
                  void munge(int board[8][8]);
                  as it were.


                  --
                  Mark McIntyre
                  CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                  CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>

                  Comment

                  • Mike Wahler

                    #10
                    Re: zero-based array, in function confucsion


                    "Mark McIntyre" <markmcintyre@s pamcop.net> wrote in message
                    news:dk3knv0g1o q2tcla55k6ifih9 ahv283ak5@4ax.c om...[color=blue]
                    > On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"
                    > <mkwahler@mkwah ler.net> wrote:
                    >[color=green]
                    > >
                    > >"Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
                    > >news:VL4eb.944 1$NX3.7723@news read3.news.pas. earthlink.net.. .
                    > >[color=darkred]
                    > >>
                    > >> > void foo(int a[]); /*a[] possible*/
                    > >>
                    > >> Here, 'a' is a function parameter. While it *looks* like
                    > >> an array, it is not, it's a pointer.[/color]
                    > >
                    > >Forgot to add, even with a number inside the [],
                    > >it's still not an array. The number will be ignored.[/color]
                    >
                    > remind me what happens with multidimensiona l arrays
                    > void munge(int board[8][8]);[/color]

                    That is equivalent to:

                    void munge(int board[][8]);

                    or

                    void munge(int (*board)[8]);

                    (param is pointer to array of 8 ints)

                    Write a function body for that, increment
                    'board' and compare to the original value
                    to see what it does.

                    But this is not what OP asked about.

                    -Mike


                    Comment

                    • dam_fool_2003@yahoo.com

                      #11
                      Re: zero-based array, in function confucsion

                      "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<VL4eb.944 1$NX3.7723@news read3.news.pas. earthlink.net>. ..[color=blue]
                      > <dam_fool_2003@ yahoo.com> wrote in message
                      > news:a3e883de.0 309291651.58679 68c@posting.goo gle.com...[color=green]
                      > > "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message[/color]
                      > news:<bl9eqb$2m m7@kcweb01.netn ews.att.com>...[color=green][color=darkred]
                      > > > Please post your code so we can see what you are trying to do. Thanks.
                      > > >[/color]
                      > >
                      > > Sorry,[/color]
                      >
                      > So it looks like you're trying to define an array of
                      > unspecified size (what you called 'zero based').
                      > Correct terminology is important. You probably
                      > see that now, from the replies you've got so far.
                      >[color=green]
                      > > void foo(int a[]); /*a[] possible*/[/color]
                      >
                      > Here, 'a' is a function parameter. While it *looks* like
                      > an array, it is not, it's a pointer. [1]
                      >
                      > (An array cannot be passed to or returned from functions,
                      > only a pointer to one of its elements can (typically the
                      > first). The [] form is just an alternate syntax.
                      >
                      >[color=green]
                      > > int main(void)
                      > > {
                      > > unsigned int y[]; /*y[] *not possible*/[/color]
                      >
                      > No, not possible. Your definition says 'make me an array'
                      > An array is a sequence of *one or more* contigous objects of
                      > the same type. But you did not tell it how many. How can
                      > the compiler make the array? :-) How many 'unsigned ints'
                      > do you want/need? Just say so, e.g.
                      >
                      > unsigned int y[10]; /* array of ten unsigned ints */
                      >[color=green]
                      > > return 0;
                      > > }[/color]
                      >
                      > May I ask which textbook(s) you're learning from?[/color]

                      Self-learner and some of the notes form net.

                      [color=blue]
                      >
                      > [1]
                      >
                      > void foo(int a[]);
                      >
                      > vs.
                      >
                      > void foo(int *a);
                      >
                      > Both of these declare that the parame ter'a' is of type
                      > 'pointer to int' (int*). Some prefer the first form, others
                      > the latter, a case can be made for each.
                      >
                      > When such a function is intended to operate upon an
                      > array, some say that the a[] form conveys this intent,
                      > and that the *a form should only be used when passing
                      > the address of a single object.
                      >
                      > Others say that the a[] only unnecessarily 'disguises'
                      > the fact that the parameter is really a pointer.
                      >
                      > This seems to me to be a religious issue which I don't
                      > care to debate. I use the *a form, simply because it's
                      > a pointer. But that's just *my* style, it's neither right
                      > nor wrong (unless of course it violates coding standards
                      > to which I've agreed to ahere).
                      >
                      > HTH,
                      > -Mike[/color]

                      Thanks for all the answers.

                      Comment

                      • Mike Wahler

                        #12
                        Re: zero-based array, in function confucsion


                        <dam_fool_2003@ yahoo.com> wrote in message
                        news:a3e883de.0 310010804.7af26 837@posting.goo gle.com...[color=blue]
                        > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message[/color]
                        news:<VL4eb.944 1$NX3.7723@news read3.news.pas. earthlink.net>. ..[color=blue][color=green]
                        > > <dam_fool_2003@ yahoo.com> wrote in message
                        > > news:a3e883de.0 309291651.58679 68c@posting.goo gle.com...[color=darkred]
                        > > > "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message[/color]
                        > > news:<bl9eqb$2m m7@kcweb01.netn ews.att.com>...[color=darkred]
                        > > > > Please post your code so we can see what you are trying to do.[/color][/color][/color]
                        Thanks.[color=blue][color=green][color=darkred]
                        > > > >
                        > > >
                        > > > Sorry,[/color]
                        > >
                        > > So it looks like you're trying to define an array of
                        > > unspecified size (what you called 'zero based').
                        > > Correct terminology is important. You probably
                        > > see that now, from the replies you've got so far.
                        > >[color=darkred]
                        > > > void foo(int a[]); /*a[] possible*/[/color]
                        > >
                        > > Here, 'a' is a function parameter. While it *looks* like
                        > > an array, it is not, it's a pointer. [1]
                        > >
                        > > (An array cannot be passed to or returned from functions,
                        > > only a pointer to one of its elements can (typically the
                        > > first). The [] form is just an alternate syntax.
                        > >
                        > >[color=darkred]
                        > > > int main(void)
                        > > > {
                        > > > unsigned int y[]; /*y[] *not possible*/[/color]
                        > >
                        > > No, not possible. Your definition says 'make me an array'
                        > > An array is a sequence of *one or more* contigous objects of
                        > > the same type. But you did not tell it how many. How can
                        > > the compiler make the array? :-) How many 'unsigned ints'
                        > > do you want/need? Just say so, e.g.
                        > >
                        > > unsigned int y[10]; /* array of ten unsigned ints */
                        > >[color=darkred]
                        > > > return 0;
                        > > > }[/color]
                        > >
                        > > May I ask which textbook(s) you're learning from?[/color]
                        >
                        > Self-learner and some of the notes form net.[/color]

                        Being a "Self-learner" doesn't release you from the
                        responsibility to yourself to use quality learning
                        materials. It also does not endow you with knowledge.
                        Which means you're trying to learn only from 'bits
                        and pieces' of material from the 'net.

                        I'll tell you right now that if you don't use textbooks,
                        you'll not get very far. And much more important, note
                        that most material on the 'net is simply wrong. You have
                        no way of knowing this, since you have no quality textbooks
                        to check the 'net material against.

                        Your original questions support my claims. :-)

                        See the book reviews www.accu.org for recommendations
                        and suggestions from the recognized industry experts.


                        Comment

                        • Mark McIntyre

                          #13
                          Re: zero-based array, in function confucsion

                          On Tue, 30 Sep 2003 23:56:07 GMT, in comp.lang.c , "Mike Wahler"
                          <mkwahler@mkwah ler.net> wrote:
                          [color=blue]
                          >"Mark McIntyre" <markmcintyre@s pamcop.net> wrote in message
                          >news:dk3knv0g1 oq2tcla55k6ifih 9ahv283ak5@4ax. com...[color=green]
                          >> On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"[/color][/color]
                          [color=blue][color=green][color=darkred]
                          >> >> > void foo(int a[]); /*a[] possible*/
                          >> >>
                          >> >> Here, 'a' is a function parameter. While it *looks* like
                          >> >> an array, it is not, it's a pointer.
                          >> >[/color]
                          >> remind me what happens with multidimensiona l arrays
                          >> void munge(int board[8][8]);[/color]
                          >
                          >That is equivalent to:
                          >[/color]
                          ....[color=blue]
                          >void munge(int (*board)[8]);[/color]

                          thats what I thought, and it shows that only the 1st dimension of an
                          array of arrays decays to a pointer upon passing to a fn.
                          [color=blue]
                          >But this is not what OP asked about.[/color]

                          but it /is/ what I wanted to get clear ! Thanks.
                          [color=blue]
                          >-Mike
                          >[/color]

                          --
                          Mark McIntyre
                          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                          CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>

                          Comment

                          • Mike Wahler

                            #14
                            Re: zero-based array, in function confucsion

                            "Mark McIntyre" <markmcintyre@s pamcop.net> wrote in message
                            news:aoimnv4jh1 98qgmrskfnkoikf vj9ud3248@4ax.c om...[color=blue]
                            > On Tue, 30 Sep 2003 23:56:07 GMT, in comp.lang.c , "Mike Wahler"
                            > <mkwahler@mkwah ler.net> wrote:
                            >[color=green]
                            > >"Mark McIntyre" <markmcintyre@s pamcop.net> wrote in message
                            > >news:dk3knv0g1 oq2tcla55k6ifih 9ahv283ak5@4ax. com...[color=darkred]
                            > >> On Tue, 30 Sep 2003 01:26:41 GMT, in comp.lang.c , "Mike Wahler"[/color][/color]
                            >[color=green][color=darkred]
                            > >> >> > void foo(int a[]); /*a[] possible*/
                            > >> >>
                            > >> >> Here, 'a' is a function parameter. While it *looks* like
                            > >> >> an array, it is not, it's a pointer.
                            > >> >
                            > >> remind me what happens with multidimensiona l arrays
                            > >> void munge(int board[8][8]);[/color]
                            > >
                            > >That is equivalent to:
                            > >[/color]
                            > ...[color=green]
                            > >void munge(int (*board)[8]);[/color]
                            >
                            > thats what I thought, and it shows that only the 1st dimension of an
                            > array of arrays decays to a pointer upon passing to a fn.[/color]

                            The name of an array, when used as a function argument,
                            always decays to a pointer to its first element.
                            "Dimensions " are irrelevant to that process. For a
                            'multidimension al' array, however, the type of a pointer
                            to an element (which is another array) of that array
                            necessarily includes the 'dimension' in the syntax which
                            expresses that pointer type.

                            E.g. when the name of the an array declared as

                            'int array[size][size]'

                            is passed to a function, it is *not* converted to

                            'int **array',

                            as many people seem to believe. It is converted to

                            'int (*array)[size]'
                            [color=blue][color=green]
                            > >But this is not what OP asked about.[/color]
                            >
                            > but it /is/ what I wanted to get clear ! Thanks.[/color]

                            OK, I thought you might have believed I had erred
                            in my post (which I do from time to time), and
                            your post was a challenge to it.

                            Thanks for clarifying the purpose of your question,
                            and you're welcome if I've helped you understand.

                            And if I've fouled up my above explanation, I'm
                            sure we'll hear about it. :-)


                            -Mike


                            Comment

                            • Mark McIntyre

                              #15
                              Re: zero-based array, in function confucsion

                              On Thu, 02 Oct 2003 20:37:45 GMT, in comp.lang.c , "Mike Wahler"
                              <mkwahler@mkwah ler.net> wrote:
                              [color=blue]
                              >"Mark McIntyre" <markmcintyre@s pamcop.net> wrote in message[color=green]
                              >> but it /is/ what I wanted to get clear ! Thanks.[/color]
                              >
                              >OK, I thought you might have believed I had erred
                              >in my post (which I do from time to time), and
                              >your post was a challenge to it.[/color]

                              not at all.
                              [color=blue]
                              >and you're welcome if I've helped you understand.[/color]

                              FWIW I specifically wanted someone* to clarify for /others/, as I was
                              a little concerned that there might be a belief that arrays of any
                              number of dimensions decayed entirely into pointers to whatever their
                              underlying datatype was.
                              [color=blue]
                              >And if I've fouled up my above explanation, I'm
                              >sure we'll hear about it. :-)[/color]

                              indeedy !

                              * and I wanted that someone not to be me, as if I'd done it, I'd
                              surely have made a trivial error and been Popped to death... :-)

                              --
                              Mark McIntyre
                              CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                              CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>

                              Comment

                              Working...