plz tell me the difference

Discussion in 'C Programming' started by jeniffer, Apr 28, 2006.

  1. jeniffer

    jeniffer Guest

    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
    jeniffer, Apr 28, 2006
    #1
    1. Advertising

  2. "jeniffer" <> wrote in message
    news:...
    > 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
    >


    Questions 1.32 and 6.2 and their answers should help. If not, tell us why
    not.
    http://www.c-faq.com


    Rod Pemberton
    Rod Pemberton, Apr 28, 2006
    #2
    1. Advertising

  3. jeniffer

    AG Guest

    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.

    > char *a="this is a string";

    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
    AG, Apr 28, 2006
    #3
  4. jeniffer

    code break Guest

    "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:
    > 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
    code break, Apr 28, 2006
    #4
  5. jeniffer

    code break Guest

    "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:
    > 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
    code break, Apr 28, 2006
    #5
  6. jeniffer

    Vladimir Oka Guest

    AG opined:

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


    So far so good, albeit somewhat less than precise...

    >> char *a="this is a string";

    > If you wrote printf("char = %d",a); you would see the address
    > where a has been stored


    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.

    > If you wrote printf("char = %c",*a); you would see what value
    > was at that address.


    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>
    Vladimir Oka, Apr 28, 2006
    #6
  7. jeniffer

    Vladimir Oka Guest

    code break opined:

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


    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.

    > char a[]="this is a string";
    >
    > but here you can't change pointer of the variable "a" to other
    > location in the program.


    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.

    > i hope this help you....


    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>
    Vladimir Oka, Apr 28, 2006
    #7
  8. jeniffer

    John Bode Guest

    jeniffer wrote:
    > 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


    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.
    John Bode, Apr 28, 2006
    #8
  9. "AG" <> writes:
    > 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.


    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_Keith) <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.
    Keith Thompson, Apr 28, 2006
    #9
  10. jeniffer

    pete Guest

    Keith Thompson wrote:
    >
    > "AG" <> writes:
    > > 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.

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


    (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
    pete, Apr 28, 2006
    #10
  11. pete <> writes:
    > Keith Thompson wrote:
    >> "AG" <> writes:
    >> > 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.

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

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


    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_Keith) <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.
    Keith Thompson, Apr 28, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. brian

    Size of my Struct? PLZ PLZ reply

    brian, Nov 23, 2004, in forum: C Programming
    Replies:
    7
    Views:
    417
    -berlin.de
    Nov 25, 2004
  2. Replies:
    1
    Views:
    382
  3. Replies:
    2
    Views:
    336
    Nick Keighley
    Nov 24, 2006
  4. Replies:
    1
    Views:
    341
  5. nocturnal
    Replies:
    1
    Views:
    523
    nocturnal
    Jul 10, 2009
Loading...

Share This Page