#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "hello";
/* tmp must allocate one memory store space. */
char *tmp = malloc (20);
char *p;
scanf("%s",tmp);
p = strdup(tmp);
printf("%s, %s\n", s, p);
}
Example2 :
#include <unistd.h>
int main(int argc, char *argv[])
{
int option = -1;
char *addr;
char *port;
while ((option = getopt (argc, argv, "i:p:")) != -1)
{
switch (option)
{
case 'i':
addr = strdup(optarg);
break;
case 'p':
/* This example show optarg already have store space. */
port = strdup(optarg);
break;
default:
/* unrecognised option ... add your error condition */
break;
}
}
printf("port = %s\n",port);
return 0;
}
Show result
$ ./a.out -p happy
port = happy
Reference:
0 意見:
張貼留言