你能告诉我为什么输出不一样吗,

#include <stdio.h>  int main(int ac, char **av, char **env) {     printf("Address of the array of environment variables: %p\n", (void *)env);         printf("Address of the first environment variable: %p\n", (void *)(env[0]));       return 0; } 

env和env [0]不是指向相同的地址。

输出:

Address of the array of environment variables: 0x7ffcd47f6288 Address of the first environment variable: 0x7ffcd47f7312 

===============>>#1 票数:1 已采纳

它们不是同一件事。 env是指向字符串的指针数组,该数组位于内存中的某个位置。 env[0]是第一个字符串的地址,就像env[1]是第二个字符串的地址一样,它不同于存储这些地址的数组的地址。

  ask by md.jamal translate from so

===============>>#2 票数:0

两者应该相同,您需要正确打印地址,尝试以下代码,看看吗?

#include <stdio.h>  int main(int ac, char **av, char *env[]) {     int i;     printf("Address of the array of environment variables: %p\n", env);     printf("Address of the first environment variable: %p\n", &env[0]);     // env is array of pointers to char *     // env[0] , env[1] , env[3] and so on and each has its own address which will be different     // which is like , char *envStrings[3] = { "evn1", "env2", "env3" }  3 - Just for example     char *testEnv[3] = { "evn1", "env2", "env3" };     printf("Address of testEnv = %p \n", (void *)testEnv);     for ( i =0; i < 3; i++ ) {          printf("testEnv[%d] = %s , address is %p \n", i, testEnv[i], &testEnv[i]);     }     printf("address of the env = %p \n", env);     for (i = 0; env[i] != NULL; i++)         printf("\n%s and address is %p", env[i], &env[i]);     return 0; } 

输出:

Address of the array of environment variables: 0x7ffdf1d639b8 --> env Address of the first environment variable: 0x7ffdf1d639b8 Address of testEnv = 0x7ffdf1d638a0 testEnv[0] = evn1 , address is 0x7ffdf1d638a0 testEnv[1] = env2 , address is 0x7ffdf1d638a8 testEnv[2] = env3 , address is 0x7ffdf1d638b0 address of the env = 0x7ffdf1d639b8  XDG_SESSION_ID=36 and address is 0x7ffdf1d639b8 --> first element ...... XDG_RUNTIME_DIR=/run/user/1000 and address is 0x7ffdf1d63a70 _=./test and address is 0x7ffdf1d63a78 OLDPWD=/home/user and address is 0x7ffdf1d63a80 

  ask by md.jamal translate from so

本文未有回复,本站智能推荐: