Memset and optarg Problems

Source: Internet
Author: User
Memset and optarg-general Linux technology-Linux programming and kernel information. For more information, see below. # Include
# Include
# Include
# Include

Int main (int argc, char ** argv)
{
Int opt;

Char * s [] = {"./code", "-a", "hello", "-o", "para "};

If (argc! = 5)
{
Argc = 5;
Argv = s;
}
Printf ("% d \ n", argc );

While (opt = getopt (argc, argv, "o: a: s :"))! = EOF ){
Switch (opt ){
Case 'O ':
Printf ("o % s \ n", optarg );
Break;
Case 'A ':
Printf ("a % s \ n", optarg );
Int len = strlen (optarg );
Printf ("len is % d \ n", len );
Printf ("optarg is % s \ n", optarg );
Memset (optarg, 'x', len );
Break;
Case's ':
Printf ("s % s \ n", optarg );
Break;
Default:
Break;
}
}

Return 0;
}


Generate code.exe if you enter./code-a dsfds-o para from the command line
The code is normal, but if only the./code is entered, Segmentation fault (core dumped) is triggered)

Why are there problems with the latter? How can this problem be solved? Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.