- 论坛徽章:
- 0
|
unix下环境变量environ得问题
这个地方一向没搞清楚,只知道栈上有存放参数表和环境变量表的空间。自己理解,这也是一个进程的用户空间的一部分,象getenv之类的函数,返回的只是一串字符的第一个字符的地址。(自己想的不知道对不对呵呵)
试了一下,
- 1 #include <stdio.h>;
- 2
- 3 int main()
- 4 {
- 5 int i;
- 6 for(i = 0;i<argc;i++)
- 7 printf("%s\n",argv[i]);
- 8
- 9 return 0;
- 10 }
复制代码
编译时提示:
- aa.c: In function `main':
- aa.c:6: `argc' undeclared (first use in this function)
- aa.c:6: (Each undeclared identifier is reported only once
- aa.c:6: for each function it appears in.)
- aa.c:7: `argv' undeclared (first use in this function)
复制代码
改成extern声明:
- 1 #include <stdio.h>;
- 2
- 3 int main()
- 4 {
- 5 extern int argc;
- 6 extern char *argv[];
- 7 int i;
- 8 for(i = 0;i<argc;i++)
- 9 printf("%s\n",argv[i]);
- 10
- 11 return 0;
- 12 }
复制代码
编译提示:- /tmp/ccylZphI.o(.text+0x1c): In function `main':
- : undefined reference to `argc'
- /tmp/ccylZphI.o(.text+0x2d): In function `main':
- : undefined reference to `argv'
- collect2: ld returned 1 exit status
复制代码
看来还是要改main的原型啊。别的方法不会了,朋友们说一下吧,谢了呵呵 |
|