关闭

main 函数第三个参数

标签: variablescharacterstringpointerscommandprocessing
521人阅读 评论(0) 收藏 举报
This is G o o g l e's cache of http://elearning.embnet.org/file.php/29/ctutorial/Environment-variables.html as retrieved on Aug 8, 2007 04:35:28 GMT.
G o o g l e's cache is the snapshot that we took of the page as we crawled the web.
The page may have changed since that time. Click here for the current page without highlighting.
This cached page may reference images which are no longer available. Click here for the cached text only.
To link to or bookmark this page, use the following url: http://www.google.com/search?q=cache:l4bkWs5Au5UJ:elearning.embnet.org/file.php/29/ctutorial/Environment-variables.html+main+envp&hl=en&ct=clnk&cd=1&gl=us


Google is neither affiliated with the authors of this page nor responsible for its content.
These search terms have been highlighted:  main  envp 

Node:Environment variables, Next:, Previous:Processing command-line options, Up:Putting a program together



Environment variables

Sometimes it is useful to communicate with a program in a semi-permanent way, so that you do not need to specify a command-line option every time you type the command to execute the program. One way to do this is to generate a configuration file, in which you can store data that will be used by the program every time it is run. This approach is typically useful if you have a large amount of data that you want to pass to a program every time it runs, or if you want the program itself to be able to change the data.

However, environment variables provide a more lightweight approach. Environment variables, sometimes called shell variables, are usually set with the export command in the shell. (This section assumes you are using the GNU Bash shell.) Standard environment variables are used for information about your home directory, terminal type, and so on; you can define additional variables for other purposes. The set of all environment variables that have values is collectively known as the environment.

Names of environment variables are case-sensitive, and it is good form to use all upper-case letters when defining a new variable; certainly this is the case for all system-defined environment variables.

The value of an environment variable can be any string that does not contain a null character (since the null character is used to terminate the string value).

Environment variables are stored in a special array that can be read by your main function. Here is the skeleton for a main function that can read environment variables; notice we have added a third parameter to main, called envp, which comes after argc and argv.

#include <stdio.h>

/* To shorten example, not using argp */
int main (int argc, char *argv[], char *envp[])
{

  return 0;
}

Notice that envp is an array of strings, just as argv is. It consists of a list of the environment variables of your shell, in the following format:

NAME=value

Just as you can manually process command-line options from argv, so can you manually process environment variables from envp. However, the simplest way to access the value of an environment variable is with the getenv function, defined in the system header stdlib.h. It takes a single argument, a string containing the name of the variable whose value you wish to discover. It returns that value, or a null pointer if the variable is not defined.

#include <stdio.h>
#include <stdlib.h>

/* To shorten example, not using argp */
int main (int argc, char *argv[], char *envp[])
{
  char *home, *host;

  home = getenv("HOME");
  host = getenv("HOSTNAME");

  printf ("Your home directory is %s on %s./n", home, host);

  return 0;
}

When you run this code, it will print out a line like the following one.

Your home directory is /home/rwhe on linnaeus.

Note: Do not modify strings returned from getenv; they are pointers to data that belongs to the system. If you want to process a value returned from getenv, copy it to another string first with strcpy. (See Strings.) If you want to change an environment variable from within your program (not usually advisable), use the putenv, setenv, and unsetenv functions. See Environment Access, for more information on these functions.


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35099次
    • 积分:779
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:3篇
    • 译文:0篇
    • 评论:3条
    最新评论