Pointers and arrays.

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

    #1

    Pointers and arrays.

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


    int main(int argc, char *argv[])
    {

    char s[10] = "welcome";

    char *p = s;
    char **prt = p; //error happens here
    return 0;
    }


  • Mike Wahler

    #2
    Re: Pointers and arrays.


    "raashid bhatt" <raashidbhatt@g mail.comwrote in message
    news:7c9c5378-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
    #include <string.h>
    #include <stdio.h>
    #include <windows.h>
    <windows.his not a standard C header. Please
    omit such from code posted here (it has nothing
    to do with your question anyway).
    >
    >
    int main(int argc, char *argv[])
    {
    >
    char s[10] = "welcome";
    >
    char *p = s;
    char **prt = p; //error happens here
    That's because you're trying to assign
    an incompatible type. Write:

    char **prt = &p;

    return 0;
    }

    I've shown you correct syntax. But what
    exactly are you trying to do?

    -Mike


    Comment

    • raashid bhatt

      #3
      Re: Pointers and arrays.

      On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
      "raashid bhatt" <raashidbh...@g mail.comwrote in message
      >
      news:7c9c5378-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
      >
      #include <string.h>
      #include <stdio.h>
      #include <windows.h>
      >
      <windows.his not a standard C header. Please
      omit such from code posted here (it has nothing
      to do with your question anyway).
      >
      >
      >
      int main(int argc, char *argv[])
      {
      >
      char s[10] = "welcome";
      >
      char *p = s;
      char **prt = p; //error happens here
      >
      That's because you're trying to assign
      an incompatible type.  Write:
      >
      char **prt = &p;
      >
      return 0;
      }
      >
      I've shown you correct syntax.  But what
      exactly are you trying to do?
      >
      -Mike
      this is wrong man why do i need &p as p consists the address

      Comment

      • Nick Keighley

        #4
        Re: Pointers and arrays.

        On 19 Aug, 15:01, raashid bhatt <raashidbh...@g mail.comwrote:
        On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
        >
        >
        >
        >
        >
        "raashid bhatt" <raashidbh...@g mail.comwrote in message
        >
        news:7c9c5378-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
        >
        #include <string.h>
        #include <stdio.h>
        #include <windows.h>
        >
        <windows.his not a standard C header. Please
        omit such from code posted here (it has nothing
        to do with your question anyway).
        >
        int main(int argc, char *argv[])
        {
        >
        char s[10] = "welcome";
        >
        char *p = s;
        char **prt = p; //error happens here
        >
        That's because you're trying to assign
        an incompatible type.  Write:
        >
        char **prt = &p;
        >
        return 0;
        }
        >
        I've shown you correct syntax.  But what
        exactly are you trying to do?
        >
        -Mike

        this is wrong man why do i need &p as p consists the address
        no it *isn't* wrong. Consider the types, p has type char* (ptr-to-
        char)
        prt has type char** (ptr-to -ptr-to-char). They are not the same.
        But &p is the address of a ptr-to-char so it has type ptr-to-ptr-to-
        char.
        So that assignment is ok.

        Get a good text book. And read it.


        --
        Nick Keighley




        Comment

        • Jens Thoms Toerring

          #5
          Re: Pointers and arrays.

          raashid bhatt <raashidbhatt@g mail.comwrote:
          #include <string.h>
          #include <stdio.h>
          #include <windows.h>
          Do you realize that this is a non-standard header (and that
          rather likely it's not needed)?
          int main(int argc, char *argv[])
          {
          char s[10] = "welcome";
          char *p = s;
          This works because in this context ('s' is used as a value)
          it is automatically converted to a pointer to the first
          char of 's'. So you have a char pointer on the left and
          n the right hand side of the assignment.
          char **prt = p; //error happens here
          But here you try to assign the value of a char pointer to a
          pointer to pointer to char, so you have different types on
          the left and right hand side. Did you try

          char **prt = &p;
          return 0;
          }
          Regards, Jens
          --
          \ Jens Thoms Toerring ___ jt@toerring.de
          \______________ ____________ http://toerring.de

          Comment

          • santosh

            #6
            Re: Pointers and arrays.

            raashid bhatt wrote:
            #include <string.h>
            #include <stdio.h>
            #include <windows.h>
            >
            >
            int main(int argc, char *argv[])
            {
            >
            char s[10] = "welcome";
            >
            char *p = s;
            char **prt = p; //error happens here
            prt is of type char** or "pointer to pointer to char". The
            expression 'p' on the RHS of the assignment resolves to a value of type
            char* which is not a compatible type for prt. You need to store the
            *address* of char* object, which you can derive in this case by
            applying the address-of operator to 'p'. Hence:

            char **prt = &p;

            is correct, as would be:

            char **prt = &s;

            or

            char **prt = &p[5];

            or even

            char **prt = &p[10];

            though the last one can be computed and stored but not deferenced.

            <snip>

            Comment

            • santosh

              #7
              Re: Pointers and arrays.

              raashid bhatt wrote:
              On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
              >"raashid bhatt" <raashidbh...@g mail.comwrote in message
              >>
              >>
              news:7c9c5378-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
              >>
              #include <string.h>
              #include <stdio.h>
              #include <windows.h>
              >>
              ><windows.his not a standard C header. Please
              >omit such from code posted here (it has nothing
              >to do with your question anyway).
              >>
              >>
              >>
              int main(int argc, char *argv[])
              {
              >>
              char s[10] = "welcome";
              >>
              char *p = s;
              char **prt = p; //error happens here
              >>
              >That's because you're trying to assign
              >an incompatible type.  Write:
              >>
              >char **prt = &p;
              >>
              return 0;
              }
              >>
              >I've shown you correct syntax.  But what
              >exactly are you trying to do?
              this is wrong man why do i need &p as p consists the address
              The expression 'p' evaluates to the *value* of 'p', *not* to it's
              address. Consider:

              int i = 5;
              int j = i;

              Now what does the expression on the RHS of the second declaration
              evaluate to? The address or the value of 'i'? Why do you think pointers
              are any different?

              int i = 5;
              int *ip = &i;
              int **ipp = &ip;

              In an expression context 'i' will evaluate to the value stored in the
              object that it designates. To initialise ip though we need the address
              of i, so we apply the address-of operator to get it. The expression ip
              will evaluate to the value that it contains, i.e., the address of the
              int object to which it has been set to point to, or the address NULL,
              or an indeterminate value. To get the *address* of ip we apply the
              address-of operator on it, exactly as was the case for i.

              As Eric said elsewhere, reading your textbook might be a better approach
              than asking multitudes of elementary questions. At the very least go
              over the CLC FAQ whose link I gave you earlier.

              Comment

              • Mike Wahler

                #8
                Re: Pointers and arrays.

                "raashid bhatt" <raashidbhatt@g mail.comwrote in message
                news:ac09bd76-aee9-4940-9858-2d37c42c2b4c@z6 g2000pre.google groups.com...
                On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                "raashid bhatt" <raashidbh...@g mail.comwrote in message
                >
                news:7c9c5378-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
                char s[10] = "welcome";
                >>
                char *p = s;
                char **prt = p; //error happens here
                >>
                >That's because you're trying to assign
                >an incompatible type. Write:
                >>
                >char **prt = &p;
                >
                return 0;
                }
                >>
                >I've shown you correct syntax. But what
                >exactly are you trying to do?
                >>
                >-Mike
                this is wrong man why do i need &p as p consists the address
                What I wrote is not wrong, it's correct. My assertion is
                supported by Kernighan & Ritchie (inventors of the C language),
                The international standard for the C language, and 20 years of
                experience using the C language.

                What supports your claim that what I wrote is wrong?

                -Mike


                Comment

                • raashid bhatt

                  #9
                  Re: Pointers and arrays.

                  On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                  "raashid bhatt" <raashidbh...@g mail.comwrote in message
                  >
                  news:ac09bd76-aee9-4940-9858-2d37c42c2b4c@z6 g2000pre.google groups.com...
                  On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                  >
                  >
                  >
                  "raashid bhatt" <raashidbh...@g mail.comwrote in message
                  >
                  news:7c9c5378-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
                  char s[10] = "welcome";
                  >
                  char *p = s;
                  char **prt = p; //error happens here
                  >
                  That's because you're trying to assign
                  an incompatible type. Write:
                  >
                  char **prt = &p;
                  >
                  return 0;
                  }
                  >
                  I've shown you correct syntax. But what
                  exactly are you trying to do?
                  >
                  -Mike
                  this is wrong man why do i need &p as p consists the address
                  >
                  What I wrote is not wrong, it's correct.  My assertion is
                  supported by Kernighan & Ritchie (inventors of the C language),
                  The international standard for the C language, and 20 years of
                  experience using the C language.
                  >
                  What supports your claim that what I wrote is wrong?
                  >
                  -Mike
                  This is wrong man correct ur thing

                  Comment

                  • Joachim Schmitz

                    #10
                    Re: Pointers and arrays.

                    raashid bhatt wrote:
                    On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                    >"raashid bhatt" <raashidbh...@g mail.comwrote in message
                    >>
                    >news:ac09bd7 6-aee9-4940-9858-2d37c42c2b4c@z6 g2000pre.google groups.com...
                    >On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                    >>
                    >>
                    >>
                    >>"raashid bhatt" <raashidbh...@g mail.comwrote in message
                    >>
                    >>news:7c9c53 78-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
                    >>>>char s[10] = "welcome";
                    >>
                    >>>>char *p = s;
                    >>>>char **prt = p; //error happens here
                    >>
                    >>>That's because you're trying to assign
                    >>>an incompatible type. Write:
                    >>
                    >>>char **prt = &p;
                    >>
                    >>>>return 0;
                    >>>>}
                    >>
                    >>>I've shown you correct syntax. But what
                    >>>exactly are you trying to do?
                    >>
                    >>>-Mike
                    >>this is wrong man why do i need &p as p consists the address
                    >>
                    >What I wrote is not wrong, it's correct. My assertion is
                    >supported by Kernighan & Ritchie (inventors of the C language),
                    >The international standard for the C language, and 20 years of
                    >experience using the C language.
                    >>
                    >What supports your claim that what I wrote is wrong?
                    >>
                    >-Mike
                    >
                    This is wrong man correct ur thing
                    If you know better, why did you ask in the first place?

                    Bye, Jojo


                    Comment

                    • santosh

                      #11
                      Re: Pointers and arrays.

                      raashid bhatt wrote:
                      On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                      >"raashid bhatt" <raashidbh...@g mail.comwrote in message
                      >>
                      >>
                      news:ac09bd76-aee9-4940-9858-2d37c42c2b4c@z6 g2000pre.google groups.com...
                      >On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                      >>
                      >>
                      >>
                      "raashid bhatt" <raashidbh...@g mail.comwrote in message
                      >>
                      >>
                      >news:7c9c537 8-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
                      char s[10] = "welcome";
                      >>
                      char *p = s;
                      char **prt = p; //error happens here
                      >>
                      >That's because you're trying to assign
                      >an incompatible type. Write:
                      >>
                      >char **prt = &p;
                      >>
                      return 0;
                      }
                      >>
                      >I've shown you correct syntax. But what
                      >exactly are you trying to do?
                      >>
                      >-Mike
                      this is wrong man why do i need &p as p consists the address
                      >>
                      >What I wrote is not wrong, it's correct.  My assertion is
                      >supported by Kernighan & Ritchie (inventors of the C language),
                      >The international standard for the C language, and 20 years of
                      >experience using the C language.
                      >>
                      >What supports your claim that what I wrote is wrong?
                      >>
                      >-Mike
                      >
                      This is wrong man correct ur thing
                      Right. I think we can file this one as another troll.

                      Comment

                      • Nick Keighley

                        #12
                        Re: Pointers and arrays.

                        On 19 Aug, 17:05, raashid bhatt <raashidbh...@g mail.comwrote:
                        On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                        "raashid bhatt" <raashidbh...@g mail.comwrote in message
                        news:ac09bd76-aee9-4940-9858-2d37c42c2b4c@z6 g2000pre.google groups.com...
                        On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                        "raashid bhatt" <raashidbh...@g mail.comwrote in message
                        >news:7c9c537 8-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com....
                        char s[10] = "welcome";
                        >
                        char *p = s;
                        char **prt = p; //error happens here
                        >
                        >That's because you're trying to assign
                        >an incompatible type. Write:
                        >
                        >char **prt = &p;
                        >
                        return 0;
                        }
                        >
                        >I've shown you correct syntax. But what
                        >exactly are you trying to do?
                        >
                        this is wrong man why do i need &p as p consists the address
                        >
                        What I wrote is not wrong, it's correct.  My assertion is
                        supported by Kernighan & Ritchie (inventors of the C language),
                        The international standard for the C language, and 20 years of
                        experience using the C language.
                        >
                        What supports your claim that what I wrote is wrong?
                        >
                        This is wrong man correct ur thing
                        what does that mean? Write in english.

                        plonk

                        --
                        Nick Keighley

                        Comment

                        • John Bode

                          #13
                          Re: Pointers and arrays.

                          On Aug 19, 11:05 am, raashid bhatt <raashidbh...@g mail.comwrote:
                          On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                          >
                          >
                          >
                          "raashid bhatt" <raashidbh...@g mail.comwrote in message
                          >
                          news:ac09bd76-aee9-4940-9858-2d37c42c2b4c@z6 g2000pre.google groups.com...
                          On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                          >
                          "raashid bhatt" <raashidbh...@g mail.comwrote in message
                          >
                          >news:7c9c537 8-ffd0-485b-8cd4-66e0f5f5e500@t1 g2000pra.google groups.com...
                          char s[10] = "welcome";
                          >
                          char *p = s;
                          char **prt = p; //error happens here
                          >
                          >That's because you're trying to assign
                          >an incompatible type. Write:
                          >
                          >char **prt = &p;
                          >
                          return 0;
                          }
                          >
                          >I've shown you correct syntax. But what
                          >exactly are you trying to do?
                          >
                          >-Mike
                          this is wrong man why do i need &p as p consists the address
                          >
                          What I wrote is not wrong, it's correct. My assertion is
                          supported by Kernighan & Ritchie (inventors of the C language),
                          The international standard for the C language, and 20 years of
                          experience using the C language.
                          >
                          What supports your claim that what I wrote is wrong?
                          >
                          -Mike
                          >
                          This is wrong man correct ur thing
                          It is NOT wrong; check your handy C reference manual or the FAQ. When
                          doing an assignment, the *types* of both the source and the target
                          have to be compatible. The *type* of p is char*, which is not the
                          same as the type of prt, which is char**.

                          prt is looking for a pointer-to-pointer-to-char. p is simply pointer-
                          to-char. However, &p evaluates to pointer-to-pointer-to-char. So the
                          answer, as several people have already pointed out, is to write

                          char **prt = &p;

                          If you still think this is wrong, then give up on programming in C;
                          you're just wasting your time otherwise.

                          Comment

                          • Anand Hariharan

                            #14
                            Re: Pointers and arrays.

                            On Tue, 19 Aug 2008 09:05:38 -0700, raashid bhatt <raashidbhatt@g mail.com>
                            wrote:
                            On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                            >"raashid bhatt" <raashidbh...@g mail.comwrote in message
                            >>
                            >news:ac09bd7 6-
                            aee9-4940-9858-2d37c42c2b4c@z6 g200...le groups.com...
                            >On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
                            >>
                            >>
                            >>
                            "raashid bhatt" <raashidbh...@g mail.comwrote in message
                            >>
                            >news:7c9c537 8-
                            ffd0-485b-8cd4-66e0f5f5e500@t1 g200...le groups.com...
                            char s[10] = "welcome";
                            >>
                            char *p = s;
                            char **prt = p; //error happens here
                            >>
                            >That's because you're trying to assign an incompatible type. Write:
                            >>
                            >char **prt = &p;
                            >>
                            (...)
                            this is wrong man why do i need &p as p consists the address
                            >>
                            Did you read Santosh's response to this question?

                            >What I wrote is not wrong, it's correct.  My assertion is supported by
                            >Kernighan & Ritchie (inventors of the C language), The international
                            >standard for the C language, and 20 years of experience using the C
                            >language.
                            >>
                            >What supports your claim that what I wrote is wrong?
                            >>
                            >-Mike
                            >
                            This is wrong man correct ur thing
                            And I suppose the compiler is wrong as well? Did you actually read
                            Mike's question?

                            - Anand

                            Comment

                            Working...