understanding pointers

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

    understanding pointers

    Can you please explain the below pointer initializations :

    char *cptr = "test"; /* gives no error and warnings */
    int *iptr = 10; /* gives only warning */
    float *fptr = 3.14; /* gives error */

    Why?

    Thanks
  • Joachim Schmitz

    #2
    Re: understanding pointers

    sumsin wrote:
    Can you please explain the below pointer initializations :
    >
    char *cptr = "test"; /* gives no error and warnings */
    Because there no error and nothing to warn about.
    cptr now points to the (read-only) first character of the charachter array
    "test"
    int *iptr = 10; /* gives only warning */
    Because 10 potentially could be an address, but most probably is not, at
    least none from the address space you program has been given by the OS.
    You can inititialize an int with 10, but should inititialize an in * with
    the address of an int
    float *fptr = 3.14; /* gives error */
    Because an address can't be a floating point value
    You can initialite a float with 3.14, but not a float *, here you'd need the
    address of a float.
    >
    Why?
    >
    Thanks
    Bye, Jojo


    Comment

    • Keith Thompson

      #3
      Re: understanding pointers

      sumsin <sumsin123@gmai l.comwrites:
      Can you please explain the below pointer initializations :
      >
      char *cptr = "test"; /* gives no error and warnings */
      "test" is a string literal, an expression of array type. In most
      contexts, including this one, an expression of array type is
      implicitly converted to a pointer to the array's first element.

      It would be better to declare
      const char *cptr = "test";
      You're not allowed to modify the contents of a string literal
      (attempting to do so is undefined behavior), but for historical
      reasons string literals are not const.

      See section 6 of the comp.lang.c FAQ, <http://www.c-faq.com>.
      int *iptr = 10; /* gives only warning */
      This is actually a constraint violation; the compiler is free to
      reject it rather than just print a warning. This doesn't cause iptr
      to point to an int object with the value 10; it attempts to use the
      value 10 (of type int) to initialize an object of type int*. Your
      compiler is probably letting you get away with treating 10 as an
      address, implicitly converted from int to int*.

      If you really wanted to do that, you could write:
      int *iptr = (int*)10;
      but its not likely to be meaningful.

      If you want iptr to point to an object with the value 10, you're going
      to have to create such an object:

      int obj = 10;
      int *iptr = &obj;
      float *fptr = 3.14; /* gives error */
      This attempts to assign a value of type double to an object of type
      float*. You can't do that. If you want ftpr to point to a float
      object with the value 3.14, you'll have to create such an object, as
      above.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • sumsin

        #4
        Re: understanding pointers

        On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
        digital.dewrote :
        sumsin wrote:
        Can you please explain the below pointer initializations :
        >
        char *cptr = "test"; /* gives no error and warnings */
        >
        Because there no error and nothing to warn about.
        cptr now points to the (read-only) first character of the character array
        "test"
        >
        So what will be the value of cptr? Will it be an address kind of value
        or the lateral string ("test") itself?
        Because when I try to print the different values like:

        printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
        &cptr = 0x7fbffff938

        printf("\ncptr = %p", cptr); /* prints value of cptr */
        cptr = 0x4005ec

        but when I try like this:
        printf("\ncptr = %s", cptr); /* prints value of cptr */
        cptr = test

        printf("\ncptr = %s", *cptr); /* prints value at cptr */
        Segmentation fault


        cptr (name)
        -------------------
        | What should come |
        | here? | <- (This should be some address kind of vale,
        right?)
        | |
        -------------------
        0x7fbffff938 (Address of cptr)


        Comment

        • Joachim Schmitz

          #5
          Re: understanding pointers

          sumsin wrote:
          On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
          digital.dewrote :
          >sumsin wrote:
          >>Can you please explain the below pointer initializations :
          >>
          >>char *cptr = "test"; /* gives no error and warnings */
          >>
          >Because there no error and nothing to warn about.
          >cptr now points to the (read-only) first character of the character
          >array "test"
          >>
          So what will be the value of cptr? Will it be an address kind of value
          or the lateral string ("test") itself?
          The address of the 1st character (the 1st 't') of the text literal ("test")
          Because when I try to print the different values like:
          >
          printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
          &cptr = 0x7fbffff938
          That's the address of the pointer
          printf("\ncptr = %p", cptr); /* prints value of cptr */
          cptr = 0x4005ec
          This is the value of the pointer, i.e. the address of the 1st character (the
          1st 't') of the text literal ("test")
          but when I try like this:
          printf("\ncptr = %s", cptr); /* prints value of cptr */
          cptr = test
          Indeed. printf's %s prints the \0 termitated character array that start at
          cprt.
          printf("\ncptr = %s", *cptr); /* prints value at cptr */
          Segmentation fault
          >
          >
          cptr (name)
          -------------------
          >What should come |
          >here? | <- (This should be some address kind of vale,
          > right?) |
          -------------------
          0x7fbffff938 (Address of cptr)

          Comment

          • Chris Thomasson

            #6
            Re: understanding pointers

            "sumsin" <sumsin123@gmai l.comwrote in message
            news:cc16cf18-a910-4ce8-a98c-37f58f5d3093@y2 2g2000prd.googl egroups.com...
            On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
            digital.dewrote :
            >sumsin wrote:
            Can you please explain the below pointer initializations :
            >>
            char *cptr = "test"; /* gives no error and warnings */
            >>
            >Because there no error and nothing to warn about.
            >cptr now points to the (read-only) first character of the character array
            >"test"
            >>
            So what will be the value of cptr? Will it be an address kind of value
            or the lateral string ("test") itself?
            Because when I try to print the different values like:
            >
            printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
            FWIW, this is undefined behavior. You need to ensure that your passing a
            single void* type for every %p formatter:

            printf("\n&cptr = %p", (void*)&cptr);


            [...]

            Comment

            • Dan

              #7
              Re: understanding pointers


              "sumsin" <sumsin123@gmai l.comwrote in message
              news:eed215a0-3241-4b24-ba77-26254a6ec00b@c1 9g2000prf.googl egroups.com...
              Can you please explain the below pointer initializations :
              >
              char *cptr = "test"; /* gives no error and warnings */
              A pointer that points to the start of a string.
              int *iptr = 10; /* gives only warning */
              A pointer pointing to address 10
              float *fptr = 3.14; /* gives error */
              No such thing as a floating address.
              Why?
              If you want to point to a value, you first need a variable to hold that
              value.

              float f=3.14;
              float *fptr = &f;

              Your confusion probably comes from the char *cptr = "test". C does special
              things with array's. They are not like other data types, whats its sort of
              really doing is:
              char c[5] = "test";
              char *cptr = &c[0]; (which is the same as char *cptr = c;)




              Comment

              • sumsin

                #8
                Re: understanding pointers

                On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                digital.dewrote :
                sumsin wrote:
                On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                digital.dewrote :
                sumsin wrote:
                >Can you please explain the below pointer initializations :
                >
                >char *cptr = "test"; /* gives no error and warnings */
                >
                Because there no error and nothing to warn about.
                cptr now points to the (read-only) first character of the character
                array "test"
                >
                So what will be the value of cptr? Will it be an address kind of value
                or the lateral string ("test") itself?
                >
                The address of the 1st character (the 1st 't') of the text literal ("test")
                printf("\ncptr = %p", cptr); /* prints value of cptr */
                cptr = 0x4005ec
                >
                This is the value of the pointer, i.e. the address of the 1st character (the
                1st 't') of the text literal ("test")but when I try like this:
                printf("\ncptr = %s", cptr); /* prints value of cptr */
                cptr = test
                >
                Indeed. printf's %s prints the \0 termitated character array that start at
                cprt.
                You mean cptr will contain the address of the 1st character of the
                text literal "test".
                Something like:
                cptr = 0x4005ec (is it ok?)
                Is it the address of 1st character of the text literal "test"?

                Then when I try to dereference(*cp tr) this pointer then why do I go
                that "Segmentati on fault"?

                Comment

                • santosh

                  #9
                  Re: understanding pointers

                  sumsin wrote:
                  On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                  digital.dewrote :
                  >sumsin wrote:
                  On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                  digital.dewrote :
                  >sumsin wrote:
                  >>Can you please explain the below pointer initializations :
                  >>
                  >>char *cptr = "test"; /* gives no error and warnings */
                  >>
                  >Because there no error and nothing to warn about.
                  >cptr now points to the (read-only) first character of the
                  >character array "test"
                  >>
                  So what will be the value of cptr? Will it be an address kind of
                  value or the lateral string ("test") itself?
                  >>
                  >The address of the 1st character (the 1st 't') of the text literal
                  >("test")
                  printf("\ncptr = %p", cptr); /* prints value of cptr */
                  cptr = 0x4005ec
                  >>
                  >This is the value of the pointer, i.e. the address of the 1st
                  >character (the 1st 't') of the text literal ("test")but when I try
                  >like this:
                  printf("\ncptr = %s", cptr); /* prints value of cptr */
                  cptr = test
                  >>
                  >Indeed. printf's %s prints the \0 termitated character array that
                  >start at cprt.
                  >
                  You mean cptr will contain the address of the 1st character of the
                  text literal "test".
                  Something like:
                  cptr = 0x4005ec (is it ok?)
                  Is it the address of 1st character of the text literal "test"?
                  >
                  Then when I try to dereference(*cp tr) this pointer then why do I go
                  that "Segmentati on fault"?
                  It's undefined behaviour to modify string literals. cptr points to one
                  and your compiler might have put in read-only memory.

                  Comment

                  • sumsin

                    #10
                    Re: understanding pointers

                    On May 29, 2:07 pm, santosh <santosh....@gm ail.comwrote:
                    sumsin wrote:
                    On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                    digital.dewrote :
                    sumsin wrote:
                    On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                    digital.dewrote :
                    sumsin wrote:
                    >Can you please explain the below pointer initializations :
                    >
                    >char *cptr = "test"; /* gives no error and warnings */
                    >
                    Because there no error and nothing to warn about.
                    cptr now points to the (read-only) first character of the
                    character array "test"
                    >
                    So what will be the value of cptr? Will it be an address kind of
                    value or the lateral string ("test") itself?
                    >
                    The address of the 1st character (the 1st 't') of the text literal
                    ("test")
                    printf("\ncptr = %p", cptr); /* prints value of cptr */
                    cptr = 0x4005ec
                    >
                    This is the value of the pointer, i.e. the address of the 1st
                    character (the 1st 't') of the text literal ("test")but when I try
                    like this:
                    printf("\ncptr = %s", cptr); /* prints value of cptr */
                    cptr = test
                    >
                    Indeed. printf's %s prints the \0 termitated character array that
                    start at cprt.
                    >
                    You mean cptr will contain the address of the 1st character of the
                    text literal "test".
                    Something like:
                    cptr = 0x4005ec (is it ok?)
                    Is it the address of 1st character of the text literal "test"?
                    >
                    Then when I try to dereference(*cp tr) this pointer then why do I go
                    that "Segmentati on fault"?
                    >
                    It's undefined behavior to modify string literals. cptr points to one
                    and your compiler might have put in read-only memory.
                    You mean compiler implicitly put string literal "test" at some read
                    only memory (is it un-named?) and to de-reference this memory is a
                    segmentation violation, is it?

                    Comment

                    • Bartc

                      #11
                      Re: understanding pointers


                      "sumsin" <sumsin123@gmai l.comwrote in message
                      news:eed215a0-3241-4b24-ba77-26254a6ec00b@c1 9g2000prf.googl egroups.com...
                      Can you please explain the below pointer initializations :
                      >
                      char *cptr = "test"; /* gives no error and warnings */
                      int *iptr = 10; /* gives only warning */
                      float *fptr = 3.14; /* gives error */
                      >
                      Why?
                      'Why' has been explained. But I think rewriting like this gives the effect
                      you want:

                      char *cptr = "test";
                      int iptr[] = {10};
                      float fptr[] = {3.14};

                      Access the values pointed to as *iptr and *fptr.

                      --
                      Bartc


                      Comment

                      • Barry Schwarz

                        #12
                        Re: understanding pointers

                        On Thu, 29 May 2008 02:03:39 -0700 (PDT), sumsin <sumsin123@gmai l.com>
                        wrote:
                        >On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                        >digital.dewrot e:
                        >sumsin wrote:
                        On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                        digital.dewrote :
                        >sumsin wrote:
                        >>Can you please explain the below pointer initializations :
                        >>
                        >>char *cptr = "test"; /* gives no error and warnings */
                        >>
                        >Because there no error and nothing to warn about.
                        >cptr now points to the (read-only) first character of the character
                        >array "test"
                        >>
                        So what will be the value of cptr? Will it be an address kind of value
                        or the lateral string ("test") itself?
                        >>
                        >The address of the 1st character (the 1st 't') of the text literal ("test")
                        printf("\ncptr = %p", cptr); /* prints value of cptr */
                        cptr = 0x4005ec
                        >>
                        >This is the value of the pointer, i.e. the address of the 1st character (the
                        >1st 't') of the text literal ("test")but when I try like this:
                        printf("\ncptr = %s", cptr); /* prints value of cptr */
                        cptr = test
                        >>
                        >Indeed. printf's %s prints the \0 termitated character array that start at
                        >cprt.
                        >
                        >You mean cptr will contain the address of the 1st character of the
                        >text literal "test".
                        >Something like:
                        >cptr = 0x4005ec (is it ok?)
                        >Is it the address of 1st character of the text literal "test"?
                        >
                        >Then when I try to dereference(*cp tr) this pointer then why do I go
                        >that "Segmentati on fault"?
                        You need to show how you dereference it. There are lots of wrong ways
                        to attempt to dereference cptr.

                        If you tried to change the value pointed to, as in
                        *cptr = 'w';
                        then you invoked undefined behavior by attempting to modify a string
                        literal.

                        If you attempted to print it as if it were a string, as in
                        printf("\ncptr = %s", *cptr);
                        then you invoked undefined behavior by passing the wrong argument type
                        to printf.

                        A segmentation fault is one of the best kinds of undefined behavior.
                        It quickly eliminates any false hope that your code is correct.

                        Show us what you did.


                        Remove del for email

                        Comment

                        • Barry Schwarz

                          #13
                          Re: understanding pointers

                          On Thu, 29 May 2008 03:07:14 -0700 (PDT), sumsin <sumsin123@gmai l.com>
                          wrote:
                          >On May 29, 2:07 pm, santosh <santosh....@gm ail.comwrote:
                          >sumsin wrote:
                          On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                          digital.dewrote :
                          >sumsin wrote:
                          On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                          digital.dewrote :
                          >sumsin wrote:
                          >>Can you please explain the below pointer initializations :
                          >>
                          >>char *cptr = "test"; /* gives no error and warnings */
                          >>
                          >Because there no error and nothing to warn about.
                          >cptr now points to the (read-only) first character of the
                          >character array "test"
                          >>
                          So what will be the value of cptr? Will it be an address kind of
                          value or the lateral string ("test") itself?
                          >>
                          >The address of the 1st character (the 1st 't') of the text literal
                          >("test")
                          printf("\ncptr = %p", cptr); /* prints value of cptr */
                          cptr = 0x4005ec
                          >>
                          >This is the value of the pointer, i.e. the address of the 1st
                          >character (the 1st 't') of the text literal ("test")but when I try
                          >like this:
                          printf("\ncptr = %s", cptr); /* prints value of cptr */
                          cptr = test
                          >>
                          >Indeed. printf's %s prints the \0 termitated character array that
                          >start at cprt.
                          >>
                          You mean cptr will contain the address of the 1st character of the
                          text literal "test".
                          Something like:
                          cptr = 0x4005ec (is it ok?)
                          Is it the address of 1st character of the text literal "test"?
                          >>
                          Then when I try to dereference(*cp tr) this pointer then why do I go
                          that "Segmentati on fault"?
                          >>
                          >It's undefined behavior to modify string literals. cptr points to one
                          >and your compiler might have put in read-only memory.
                          >
                          >You mean compiler implicitly put string literal "test" at some read
                          >only memory (is it un-named?) and to de-reference this memory is a
                          >segmentation violation, is it?
                          Memory doesn't have names. But you could consider the string literal
                          an anonymous array (one without a name). Whether you system stores
                          literals in read only memory is an implementation specific detail
                          which has no impact on the fact that you are not allowed to modify a
                          string literal. If you have read only memory, dereferencing an
                          address to this memory is only illegal if you attempt to modify the
                          data at that address.


                          Remove del for email

                          Comment

                          • pete

                            #14
                            Re: understanding pointers

                            sumsin wrote:
                            On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                            digital.dewrote :
                            >sumsin wrote:
                            >>On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                            >>digital.dewro te:
                            >>>sumsin wrote:
                            >>>>Can you please explain the below pointer initializations :
                            >>>>char *cptr = "test"; /* gives no error and warnings */
                            You mean cptr will contain the address of the 1st character of the
                            text literal "test".
                            Yes.

                            /* BEGIN new.c */

                            #include <stdio.h>

                            int main(void)
                            {
                            char *cptr = "test";

                            do {
                            printf("%c", *cptr);
                            } while(*cptr++ != '\0');
                            printf("\n");
                            return 0;
                            }

                            /* END new.c */


                            --
                            pete

                            Comment

                            • sumsin

                              #15
                              Re: understanding pointers

                              On May 29, 4:42 pm, Barry Schwarz <schwa...@dqel. comwrote:
                              On Thu, 29 May 2008 02:03:39 -0700 (PDT), sumsin <sumsin...@gmai l.com>
                              wrote:
                              >
                              >
                              >
                              On May 29, 11:54 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                              digital.dewrote :
                              sumsin wrote:
                              On May 29, 11:12 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
                              digital.dewrote :
                              sumsin wrote:
                              >Can you please explain the below pointer initializations :
                              >
                              >char *cptr = "test"; /* gives no error and warnings */
                              >
                              Because there no error and nothing to warn about.
                              cptr now points to the (read-only) first character of the character
                              array "test"
                              >
                              So what will be the value of cptr? Will it be an address kind of value
                              or the lateral string ("test") itself?
                              >
                              The address of the 1st character (the 1st 't') of the text literal ("test")
                              printf("\ncptr = %p", cptr); /* prints value of cptr */
                              cptr = 0x4005ec
                              >
                              This is the value of the pointer, i.e. the address of the 1st character (the
                              1st 't') of the text literal ("test")but when I try like this:
                              printf("\ncptr = %s", cptr); /* prints value of cptr */
                              cptr = test
                              >
                              Indeed. printf's %s prints the \0 termitated character array that start at
                              cprt.
                              >
                              You mean cptr will contain the address of the 1st character of the
                              text literal "test".
                              Something like:
                              cptr = 0x4005ec (is it ok?)
                              Is it the address of 1st character of the text literal "test"?
                              >
                              Then when I try to dereference(*cp tr) this pointer then why do I go
                              that "Segmentati on fault"?
                              >
                              You need to show how you dereference it. There are lots of wrong ways
                              to attempt to dereference cptr.
                              >
                              If you tried to change the value pointed to, as in
                              *cptr = 'w';
                              then you invoked undefined behavior by attempting to modify a string
                              literal.
                              >
                              If you attempted to print it as if it were a string, as in
                              printf("\ncptr = %s", *cptr);
                              then you invoked undefined behavior by passing the wrong argument type
                              to printf.
                              >
                              A segmentation fault is one of the best kinds of undefined behavior.
                              It quickly eliminates any false hope that your code is correct.
                              >
                              Show us what you did.
                              >
                              Remove del for email
                              I am using:
                              printf("\ncptr = %s", *cptr);

                              Comment

                              Working...