C changing the value in initialized read only data

Discussion in 'C Programming' started by mohan, Nov 9, 2003.

  1. mohan

    mohan Guest

    According to page 20 & 21 "Unix Network Proramming" W>Richard Stevens
    the vale of argv and argc are in initialized read-only data. This
    must mean that this is not changable inside the program ( Correct me
    if I am wrong) But when I executed the program the result was
    different

    Program
    ========

    #include <stdio.h>
    int main(int argc, char *argv[])
    {
    printf("the value of argv[0] is %s\n",argv[0]);
    printf("\n\t\t changing the same \n\n");
    argv[0]="test me";
    printf("\n\n\a after change value of argv[0] is %s\n\n",argv[0]);
    }

    output
    ========

    the value of argv[0] is ./mainchng
    mohan, Nov 9, 2003
    #1
    1. Advertising

  2. mohan

    Jack Klein Guest

    On 9 Nov 2003 10:22:22 -0800, (mohan) wrote in
    comp.lang.c:

    > According to page 20 & 21 "Unix Network Proramming" W>Richard Stevens
    > the vale of argv and argc are in initialized read-only data. This
    > must mean that this is not changable inside the program ( Correct me
    > if I am wrong) But when I executed the program the result was
    > different


    The C standard does not define anything called "read-only data". I
    have not read any of Stevens books, but he has a good reputation.
    Perhaps you are taking this out of context.

    > Program
    > ========
    >
    > #include <stdio.h>
    > int main(int argc, char *argv[])
    > {
    > printf("the value of argv[0] is %s\n",argv[0]);
    > printf("\n\t\t changing the same \n\n");
    > argv[0]="test me";
    > printf("\n\n\a after change value of argv[0] is %s\n\n",argv[0]);
    > }
    >
    > output
    > ========
    >
    > the value of argv[0] is ./mainchng


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Nov 9, 2003
    #2
    1. Advertising

  3. mohan

    Richard Bos Guest

    (mohan) wrote:

    > According to page 20 & 21 "Unix Network Proramming" W>Richard Stevens
    > the vale of argv and argc are in initialized read-only data.


    That may be true under UNIX, but it isn't quite true in general.

    From the last public draft of the C99 Standard:

    # -- The parameters argc and argv and the strings pointed to
    # by the argv array shall be modifiable by the program,
    # and retain their last-stored values between program
    # startup and program termination.

    Note that it doesn't say anything about any pointer members of argv;
    only about argv itself, and the strings those members point to. Thus,
    you can modify:

    - argc;
    - argv;
    - argv[n][m], for all sensible values of n and m;

    - but _not_ argv[n], not even for sensible values of n.

    Richard
    Richard Bos, Nov 10, 2003
    #3
    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. keithb
    Replies:
    2
    Views:
    7,989
    keithb
    Jun 7, 2006
  2. mohan
    Replies:
    2
    Views:
    323
    Andreas Kahari
    Nov 9, 2003
  3. Chad
    Replies:
    3
    Views:
    374
    Ben Bacarisse
    Sep 18, 2007
  4. ww
    Replies:
    4
    Views:
    377
    James Kanze
    Oct 26, 2007
  5. Replies:
    11
    Views:
    467
    Phil Carmody
    Apr 21, 2009
Loading...

Share This Page