Pointers pointers.

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

    #1

    Pointers pointers.

    I thought I could assign an array of chars to a pointer to pointer.
    However, when I do
    the following:

    #include <stdio.h>

    int main(void) {
    char buff[] ="test";
    char **output = &buff;

    return 0;
    }

    I get:
    $gcc -g ptr.c -o ptr
    ptr.c: In function `main':
    ptr.c:5: warning: initialization from incompatible pointer type

    What am I doing wrong here?

    Chad

  • Nils O. Selåsdal

    #2
    Re: Pointers pointers.

    Chad wrote:
    I thought I could assign an array of chars to a pointer to pointer.
    However, when I do
    the following:
    >
    #include <stdio.h>
    >
    int main(void) {
    char buff[] ="test";
    char **output = &buff;
    >
    return 0;
    }
    >
    I get:
    $gcc -g ptr.c -o ptr
    ptr.c: In function `main':
    ptr.c:5: warning: initialization from incompatible pointer type
    The address of a char[5] is not a char **, it is a char (*)[5];
    char (*output)[5] = &buff;

    Comment

    • Chad

      #3
      Re: Pointers pointers.


      Nils O. Selåsdal wrote:
      Chad wrote:
      I thought I could assign an array of chars to a pointer to pointer.
      However, when I do
      the following:

      #include <stdio.h>

      int main(void) {
      char buff[] ="test";
      char **output = &buff;

      return 0;
      }

      I get:
      $gcc -g ptr.c -o ptr
      ptr.c: In function `main':
      ptr.c:5: warning: initialization from incompatible pointer type
      >
      The address of a char[5] is not a char **, it is a char (*)[5];
      char (*output)[5] = &buff;
      Perhaps this might be going beyond the limits of decent programming,
      but would something like the following work:

      #include <stdio.h>
      #include <string.h>

      int main(void) {
      char buff[] ="test";

      size_t len = strlen(buff) + 1;

      char (*output)[len]=&buff;

      return 0;
      }

      Comment

      • lovecreatesbea...@gmail.com

        #4
        Re: Pointers pointers.

        Chad wrote:
        Nils O. Selåsdal wrote:
        Chad wrote:
        I thought I could assign an array of chars to a pointer to pointer.
        However, when I do
        the following:
        >
        #include <stdio.h>
        >
        int main(void) {
        char buff[] ="test";
        char **output = &buff;
        >
        return 0;
        }
        >
        I get:
        $gcc -g ptr.c -o ptr
        ptr.c: In function `main':
        ptr.c:5: warning: initialization from incompatible pointer type
        The address of a char[5] is not a char **, it is a char (*)[5];
        char (*output)[5] = &buff;
        >
        Perhaps this might be going beyond the limits of decent programming,
        but would something like the following work:
        >
        #include <stdio.h>
        #include <string.h>
        >
        int main(void) {
        char buff[] ="test";
        >
        size_t len = strlen(buff) + 1;
        >
        char (*output)[len]=&buff;
        char (*output)[sizeof buff]=&buff;
        return 0;
        }
        #include <stdio.h>

        int main(void) {
        char buff[] = "test";
        char *p = buff;

        printf("%s\n", p);
        return 0;
        }

        Comment

        • Keith Thompson

          #5
          Re: Pointers pointers.

          "Chad" <cdalten@gmail. comwrites:
          I thought I could assign an array of chars to a pointer to pointer.
          However, when I do
          the following:
          >
          #include <stdio.h>
          >
          int main(void) {
          char buff[] ="test";
          char **output = &buff;
          >
          return 0;
          }
          >
          I get:
          $gcc -g ptr.c -o ptr
          ptr.c: In function `main':
          ptr.c:5: warning: initialization from incompatible pointer type
          >
          What am I doing wrong here?
          The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
          "Arrays and Pointers".

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • lovecreatesbea...@gmail.com

            #6
            Re: Pointers pointers.


            Keith Thompson wrote:
            "Chad" <cdalten@gmail. comwrites:
            I thought I could assign an array of chars to a pointer to pointer.
            However, when I do
            the following:

            #include <stdio.h>

            int main(void) {
            char buff[] ="test";
            char **output = &buff;

            return 0;
            }

            I get:
            $gcc -g ptr.c -o ptr
            ptr.c: In function `main':
            ptr.c:5: warning: initialization from incompatible pointer type

            What am I doing wrong here?
            >
            The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
            "Arrays and Pointers".
            Hello Keith. The faq says:

            Given an array a and pointer p, <snipIf you were to assign the
            array's address to the pointer:
            p = a;
            then p[3] and a[3] would access the same element.

            If I write code as follows, is it right?

            char buff[] = "hello world";
            char *p = buff;

            Is a cast needed on the second line of code, i.e., should I write it as
            follows:

            char *p = (char *) buff;

            Thank you.

            Comment

            • Frederick Gotham

              #7
              Re: Pointers pointers.

              lovecreatesbea. ..@gmail.com posted:
              Is a cast needed on the second line of code, i.e., should I write it as
              follows:
              >
              char *p = (char *) buff;

              No. The rule of thumb is:

              Only use a cast if the code won't compile without one.

              There are exceptions to this rule of course, such as using casts to suppress
              compiler warnings:

              int Func(long const val)
              {
              return (int)(val / 89);
              }

              --

              Frederick Gotham

              Comment

              • lovecreatesbea...@gmail.com

                #8
                Re: Pointers pointers.

                lovecreatesbea. ..@gmail.com wrote:
                Keith Thompson wrote:

                The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
                "Arrays and Pointers".
                >
                Hello Keith. The faq says:
                >
                Given an array a and pointer p, <snipIf you were to assign the
                array's address to the pointer:
                p = a;
                then p[3] and a[3] would access the same element.
                >
                If I write code as follows, is it right?
                >
                char buff[] = "hello world";
                char *p = buff;
                >
                Is a cast needed on the second line of code, i.e., should I write it as
                follows:
                >
                char *p = (char *) buff;
                In K&R2, sec 5.3, it writes the code like this without a cast. It seems
                that the following code is right:

                char *p = buff;
                or
                p = buff;

                Is there a legal implicit conversion?

                Comment

                • osmium

                  #9
                  Re: Pointers pointers.

                  "Frederick Gotham" writes:
                  No. The rule of thumb is:
                  >
                  Only use a cast if the code won't compile without one.
                  Sounds like a bad rule of thumb to me.

                  Consider:

                  double mean;
                  int sum;
                  int n;
                  .....
                  mean = sum/n;

                  Also casts can contribute to the documentation for people who must
                  subsequently diddle with the code.


                  Comment

                  • lovecreatesbea...@gmail.com

                    #10
                    Re: Pointers pointers.


                    lovecreatesbea. ..@gmail.com wrote:
                    lovecreatesbea. ..@gmail.com wrote:
                    Keith Thompson wrote:
                    >
                    The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
                    "Arrays and Pointers".
                    Hello Keith. The faq says:

                    Given an array a and pointer p, <snipIf you were to assign the
                    array's address to the pointer:
                    p = a;
                    then p[3] and a[3] would access the same element.

                    If I write code as follows, is it right?

                    char buff[] = "hello world";
                    char *p = buff;

                    Is a cast needed on the second line of code, i.e., should I write it as
                    follows:

                    char *p = (char *) buff;
                    >
                    In K&R2, sec 5.3, it writes the code like this without a cast. It seems
                    that the following code is right:
                    >
                    char *p = buff;
                    or
                    p = buff;
                    >
                    Is there a legal implicit conversion?
                    I understand it now. The array name in expression is a pointer to the
                    first element of the array. There is no implicit conversion and no need
                    to add a cast.

                    Comment

                    • Keith Thompson

                      #11
                      Re: Pointers pointers.

                      "osmium" <r124c4u102@com cast.netwrites:
                      "Frederick Gotham" writes:
                      >No. The rule of thumb is:
                      >>
                      > Only use a cast if the code won't compile without one.
                      >
                      Sounds like a bad rule of thumb to me.
                      A rule of thumb, by definition, is not universal. The stated rule of
                      thumb is a very good one.
                      Consider:
                      >
                      double mean;
                      int sum;
                      int n;
                      ....
                      mean = sum/n;
                      Yes, that's a case where a cast would be appropriate.
                      Also casts can contribute to the documentation for people who must
                      subsequently diddle with the code.
                      In such cases, it's usually better to leave the cast out and depend on
                      anyone reading the code to understand how C works.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Pointers pointers.

                        Keith Thompson said:
                        "osmium" <r124c4u102@com cast.netwrites:
                        >"Frederick Gotham" writes:
                        >>No. The rule of thumb is:
                        >>>
                        >> Only use a cast if the code won't compile without one.
                        >>
                        >Sounds like a bad rule of thumb to me.
                        >
                        A rule of thumb, by definition, is not universal. The stated rule of
                        thumb is a very good one.
                        I beg to differ. There are very few situations where a cast is a good idea,
                        but IIRC in most of those very few situations, omitting the cast will not
                        necessarily lead to a diagnostic message being issued. For example, to*,
                        is*, and the printfing of pointers.
                        >
                        >Consider:
                        >>
                        >double mean;
                        >int sum;
                        >int n;
                        >....
                        >mean = sum/n;
                        >
                        Yes, that's a case where a cast would be appropriate.
                        <shrug>
                        mean = sum;
                        mean /= n;
                        </shrug>

                        Gain a cast to lose a line. Swings and roundabouts.
                        >Also casts can contribute to the documentation for people who must
                        >subsequently diddle with the code.
                        >
                        In such cases, it's usually better to leave the cast out and depend on
                        anyone reading the code to understand how C works.
                        Right. Trust the programmer, despite your instincts. :-)

                        --
                        Richard Heathfield
                        "Usenet is a strange place" - dmr 29/7/1999

                        email: rjh at above domain (but drop the www, obviously)

                        Comment

                        • CBFalconer

                          #13
                          Arithmetic and casts (was: Pointers pointers.)

                          osmium wrote:
                          "Frederick Gotham" writes:
                          >
                          >No. The rule of thumb is:
                          >>
                          > Only use a cast if the code won't compile without one.
                          >
                          Sounds like a bad rule of thumb to me.
                          >
                          Consider:
                          >
                          double mean;
                          int sum;
                          int n;
                          ....
                          mean = sum/n;
                          Bad example IMO. The above stores the truncated integral part from
                          the division, and may well be exactly what is desired. If this is
                          not desired, there are two possibilities:

                          a. mean = sum / (double)n;
                          b. mean = (double)sum / n;

                          and the point of each is to do the arithmetic in the floating point
                          processor, rather than the integer processor.

                          This is one area where Pascal avoids confusion, by having the /
                          operator for reals, and the DIV operator for integers. One more
                          argument against overloading of operators.

                          --
                          Chuck F (cbfalconer at maineline dot net)
                          Available for consulting/temporary embedded and systems.
                          <http://cbfalconer.home .att.net>


                          --
                          Posted via a free Usenet account from http://www.teranews.com

                          Comment

                          • osmium

                            #14
                            Re: Arithmetic and casts (was: Pointers pointers.)

                            "CBFalconer " writes:
                            osmium wrote:
                            >"Frederick Gotham" writes:
                            >>
                            >>No. The rule of thumb is:
                            >>>
                            >> Only use a cast if the code won't compile without one.
                            >>
                            >Sounds like a bad rule of thumb to me.
                            >>
                            >Consider:
                            >>
                            >double mean;
                            >int sum;
                            >int n;
                            >....
                            >mean = sum/n;
                            >
                            Bad example IMO. The above stores the truncated integral part from
                            the division, and may well be exactly what is desired.
                            You must be much older than me. I have *never* wanted that result. To each
                            his own.


                            Comment

                            • pete

                              #15
                              Re: Pointers pointers.

                              lovecreatesbea. ..@gmail.com wrote:
                              >
                              lovecreatesbea. ..@gmail.com wrote:
                              lovecreatesbea. ..@gmail.com wrote:
                              Keith Thompson wrote:

                              The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
                              "Arrays and Pointers".
                              >
                              Hello Keith. The faq says:
                              >
                              Given an array a and pointer p, <snipIf you were to assign the
                              array's address to the pointer:
                              p = a;
                              then p[3] and a[3] would access the same element.
                              >
                              If I write code as follows, is it right?
                              >
                              char buff[] = "hello world";
                              char *p = buff;
                              >
                              Is a cast needed on the second line of code, i.e., should I write it as
                              follows:
                              >
                              char *p = (char *) buff;
                              In K&R2, sec 5.3, it writes the code like this without a cast. It seems
                              that the following code is right:

                              char *p = buff;
                              or
                              p = buff;

                              Is there a legal implicit conversion?
                              >
                              I understand it now. The array name in expression is a pointer to the
                              first element of the array.
                              There is no implicit conversion and no need to add a cast.
                              N869
                              6.3.2 Other operands
                              6.3.2.1 Lvalues and function designators

                              [#3] Except when it is the operand of the sizeof operator or
                              the unary & operator, or is a string literal used to
                              initialize an array, an expression that has type ``array of
                              type'' is converted to an expression with type ``pointer to
                              type'' that points to the initial element of the array
                              object and is not an lvalue.

                              --
                              pete

                              Comment

                              Working...