Tag: error value roc lib char variable definition environ png
3. Environment Table
(1) Each process has a separate environment table (array of character pointers)
(2) The initial environment table inherits from the parent process
(3) Two ways of Access:
①int Main (int argc, char* argv[], char* envp[]); 3rd parameter
②extern char** environ; Global variables
4. environment variable operation function
(1) getenv: Gets the environment variable value
Header file |
#include <stdlib.h> |
Function |
char* getenv (const char* name); |
return value |
A pointer to the value associated with name, if no return null is found |
Function |
Get environment variable Value |
(2) Putenv: Place a string with the shape Name=value in the Environment table
Header file |
#include <stdlib.h> |
Function |
int* putenv (char* str); |
return value |
A pointer to the value associated with name, if no return null is found |
Function |
put a string that is shaped as name=value into the environment table . If name already exists, the original definition is deleted first. |
(3) Setenv: Place a string with the shape Name=value in the Environment table
Header file |
#include <stdlib.h> |
Function |
int* setenv (const char* name, const char* value, int rewrite); |
return value |
Successfully returned 0, error returned non-1 |
Function |
set name to value. if the name already exists in the environment, then if rewrite is 0, its existing definition is not deleted (name is not set to the new value, and there is no error). |
(4) Unsetenv: Delete the specified environment variable
Header file |
#include <stdlib.h> |
Function |
int* unsetenv (const char* name); |
return value |
Successfully returned 0, error returned non-1 |
Function |
Deleting the definition of name is not an error even if the definition does not exist. |
"Programming Experiment" setting/deleting environment variables and displaying environment variable table information
Process_env.c
#include <unistd.h>#include<stdio.h>#include<stdlib.h>//Get the Environment Table mode 1extern Char* * environ;//Global VariablesvoidShow_env (Char**env) { inti =0; Char*Curr; while((Curr = env[i++])! =NULL) {printf ("%s\n", Curr); }}//the 3rd parameter of main is the environment table pointerintMainintargcChar* argv[],Char*envp[]) { //Get Environment table information with global variables//show_env (environ); //Get the Environment Table 2: Use command line 3rd parameter envpshow_env (ENVP); //Setting Environment Variablesprintf"-----------------------------------------\ n"); Putenv ("City=shanghai"); Setenv (" Company","Horizonstudio",1); Show_env (environ); //Delete Environment Variablesprintf"-----------------------------------------\ n"); Unsetenv (" City"); Show_env (environ); return 0;}
5th. Process Environment (3) _ Environment table and environment variables