C changing the value in initialized read only data

M

mohan

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
 
J

Jack Klein

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
 
R

Richard Bos

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top