plz tell me the difference

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

    plz tell me the difference

    char *a="this is a string";
    char a[]="this is a string";
    first is a pointer while second is an array.Tell me more differences

  • Rod Pemberton

    #2
    Re: plz tell me the difference


    "jeniffer" <zenith.of.perf ection@gmail.co m> wrote in message
    news:1146211433 .676652.125630@ j33g2000cwa.goo glegroups.com.. .[color=blue]
    > char *a="this is a string";
    > char a[]="this is a string";
    > first is a pointer while second is an array.Tell me more differences
    >[/color]

    Questions 1.32 and 6.2 and their answers should help. If not, tell us why
    not.



    Rod Pemberton


    Comment

    • AG

      #3
      Re: plz tell me the difference

      char *a declares a pointer to a string. So, to say char *a = "this is
      a string", puts the address of the first letter of the string into the
      pointer a. So, that first "t" is located somewhere in memory, the
      pointer a contains the address.
      [color=blue]
      > char *a="this is a string";[/color]
      If you wrote printf("char = %d",a); you would see the address
      where a has been stored
      If you wrote printf("char = %c",*a); you would see what value was
      at that address.

      flank

      Comment

      • code break

        #4
        Re: plz tell me the difference


        "char *a="this is a string"
        Here the string literals " this is a string " is stored in data
        segment & you are making pointer variable "a" to point this location
        but "a" can be changed to point to any other location in a program.

        char a[]="this is a string";

        but here you can't change pointer of the variable "a" to other
        location in the program.

        i hope this help you....





        jeniffer wrote:[color=blue]
        > char *a="this is a string";
        > char a[]="this is a string";
        > first is a pointer while second is an array.Tell me more differences[/color]

        Comment

        • code break

          #5
          Re: plz tell me the difference


          "char *a="this is a string"
          Here the string literals " this is a string " is stored in data
          segment & you are making pointer variable "a" to point this location
          but "a" can be changed to point to any other location in a program.

          char a[]="this is a string";

          but here you can't change pointer of the variable "a" to other
          location in the program.

          i hope this help you....





          jeniffer wrote:[color=blue]
          > char *a="this is a string";
          > char a[]="this is a string";
          > first is a pointer while second is an array.Tell me more differences[/color]

          Comment

          • Vladimir Oka

            #6
            Re: plz tell me the difference

            AG opined:
            [color=blue]
            > char *a declares a pointer to a string. So, to say char *a = "this
            > is a string", puts the address of the first letter of the string into
            > the pointer a. So, that first "t" is located somewhere in memory,
            > the pointer a contains the address.[/color]

            So far so good, albeit somewhat less than precise...
            [color=blue][color=green]
            >> char *a="this is a string";[/color]
            > If you wrote printf("char = %d",a); you would see the address
            > where a has been stored[/color]

            No, you wouldn't.

            You'd see (if you're lucky) whatever your implementation thinks a
            pointer interpreted as an `int` looks like. In fact, you get Undefined
            Behaviour, as "%d" tells `printf()` to expect an `int`, when in fact
            you're passing `char *`. UB essentially means: all bets are off, duck
            or grouse!

            Also, since you didn't terminate output with '\n', you may end up
            getting absolutely no output at all.

            You could output a pointer (as represented by your implementation) if
            you used "%p" instead. How, and whether it relates to memory
            addresses, is another matter.
            [color=blue]
            > If you wrote printf("char = %c",*a); you would see what value
            > was at that address.[/color]

            This will work as expected, yes (apart from missing '\n', that is).


            --
            The trouble with a kitten is that
            When it grows up, it's always a cat
            -- Ogden Nash.

            <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

            Comment

            • Vladimir Oka

              #7
              Re: plz tell me the difference

              code break opined:
              [color=blue]
              >
              > "char *a="this is a string"
              > Here the string literals " this is a string " is stored in data
              > segment & you are making pointer variable "a" to point this location
              > but "a" can be changed to point to any other location in a program.[/color]

              C knows not of "data segment", nor does it say where exactly the string
              is stored. It's important to note that you're not allow to modify it.
              [color=blue]
              > char a[]="this is a string";
              >
              > but here you can't change pointer of the variable "a" to other
              > location in the program.[/color]

              Wrong.

              In this case `a` is not a pointer, but an array. It is correct that you
              can't make `a` refer to something else, but that is true of any
              variable you declare. As it is not declared `const` the contents of
              this array can be modified.

              Also, in the line above, the string is used to initialise array `a`
              (including the terminating '\0'). It is not otherwise accessible from
              the program.
              [color=blue]
              > i hope this help you....[/color]

              Actually, no.

              --
              He's dead, Jim
              -- McCoy, "The Devil in the Dark", stardate 3196.1

              <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

              Comment

              • John Bode

                #8
                Re: plz tell me the difference


                jeniffer wrote:[color=blue]
                > char *a="this is a string";
                > char a[]="this is a string";
                > first is a pointer while second is an array.Tell me more differences[/color]

                I'm going to rewrite this a little:

                char *ptr="this is a string";
                char arr[]="this is a string";

                In the above code, we're talking about 3 distinct objects: the string
                literal "this is a string", the pointer ptr, and the array arr. The
                following hypothetical memory map shows the state of each object after
                initialization (best viewed with a fixed-size font):

                Item Address Contents
                ---- ------- --------
                literal 0x0800 "this is a string"
                ptr 0x1000 0x0800
                arr 0x2000 "this is a string"

                The string literal has its own address in memory, and may not be
                writable (i.e., you may not be allowed to write to the bytes between
                0x0800 and 0x0811; assume that you can't).

                The pointer ptr contains the address of the string literal. Since the
                literal may not be writable, you may not write to *ptr or ptr[n];
                however, you can assign a new value to ptr (i.e., ptr=NULL,
                ptr=&arr[0], ptr=malloc(16), etc.), and if this new value points to
                writable memory, you may then also write to *ptr or ptr[n].

                The array arr contains a *copy* of the contents of the string literal.
                The array contents are writable, so you can write to *arr or arr[n];
                however, the array object itself is not writable, so you may not write
                arr=NULL, or arr="this is a string", or arr=p, etc.

                Comment

                • Keith Thompson

                  #9
                  Re: plz tell me the difference

                  "AG" <andrewkgentile @gmail.com> writes:[color=blue]
                  > char *a declares a pointer to a string. So, to say char *a = "this is
                  > a string", puts the address of the first letter of the string into the
                  > pointer a. So, that first "t" is located somewhere in memory, the
                  > pointer a contains the address.[/color]

                  Strictly speaking, char *a declares a pointer to a char, not a pointer
                  to a string. That pointer-to-char can, and commonly does, point to
                  the first character of a string (a sequence of chars terminated by a
                  '\0'), and can be used to access the entire string. Referring to a as
                  a "pointer to a string" is a common verbal shorthand, and a useful
                  one, but it's not literally correct.

                  More generally, it's common to use a pointer-to-FOO to point to the
                  first element of an array of FOO:

                  FOO arr[10];
                  FOO *ptr = arr; /* or, equivalently, FOO *ptr = &arr[0]; */

                  but it's *also* possible to have a pointer to an array, which is
                  distinct from a pointer to the array's first element:

                  FOO (*arr_ptr)[10] = &arr;

                  Calling ptr a "pointer to an array" blurs this distinction.

                  Incidentally, pointers to arrays are rarely useful. It's usually more
                  useful to have a pointer to the first element of an array, and keep
                  track of the length separately rather than hard-wiring a fixed length
                  into an pointer-to-array type.

                  --
                  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

                  • pete

                    #10
                    Re: plz tell me the difference

                    Keith Thompson wrote:[color=blue]
                    >
                    > "AG" <andrewkgentile @gmail.com> writes:[color=green]
                    > > char *a declares a pointer to a string.
                    > > So, to say char *a = "this is a string",
                    > > puts the address of the first letter of the string into the
                    > > pointer a. So, that first "t" is located somewhere in memory, the
                    > > pointer a contains the address.[/color]
                    >
                    > Strictly speaking, char *a declares a pointer to a char, not a pointer
                    > to a string. That pointer-to-char can, and commonly does, point to
                    > the first character of a string (a sequence of chars terminated by a
                    > '\0'), and can be used to access the entire string. Referring to a as
                    > a "pointer to a string" is a common verbal shorthand, and a useful
                    > one, but it's not literally correct.[/color]

                    (a) is a "pointer to a string" according to the definition
                    in the standard for "pointer to a string".

                    I don't think it would be wrong to say that
                    char *a = "this is a string";
                    declares a pointer with a value unequal to NULL.
                    The point being, that there are other aspects
                    to an initialised declaration, besides the type.

                    --
                    pete

                    Comment

                    • Keith Thompson

                      #11
                      Re: plz tell me the difference

                      pete <pfiland@mindsp ring.com> writes:[color=blue]
                      > Keith Thompson wrote:[color=green]
                      >> "AG" <andrewkgentile @gmail.com> writes:[color=darkred]
                      >> > char *a declares a pointer to a string.
                      >> > So, to say char *a = "this is a string",
                      >> > puts the address of the first letter of the string into the
                      >> > pointer a. So, that first "t" is located somewhere in memory, the
                      >> > pointer a contains the address.[/color]
                      >>
                      >> Strictly speaking, char *a declares a pointer to a char, not a pointer
                      >> to a string. That pointer-to-char can, and commonly does, point to
                      >> the first character of a string (a sequence of chars terminated by a
                      >> '\0'), and can be used to access the entire string. Referring to a as
                      >> a "pointer to a string" is a common verbal shorthand, and a useful
                      >> one, but it's not literally correct.[/color]
                      >
                      > (a) is a "pointer to a string" according to the definition
                      > in the standard for "pointer to a string".[/color]

                      You're right. C99 7.1.1 says:

                      A _pointer to a string_ is a pointer to its initial (lowest
                      addressed) character.

                      If you interpret the phrase literally, it's a useful shorthand but not
                      strictly correct. Since the standard provides a definition for it, I
                      concede the point.

                      --
                      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

                      Working...