看一道海康威视的笔试题,题目很简单:
题目是多选题。
我觉得大部分同学看到这个题目的时候,应该觉得它是一个送分题。
main函数如果提供参数的话,有两个参数,一个是argc,一个是argv,其中,argc表示命令行参数的个数,argv是个指针数组,每个指针指向一个参数。
#include int main(int argc, char *argv[]){ for (int i = 0; i 0)) { struct audit_ifaces *afct = glro(dl_audit); struct link_map *head = gl(dl_ns)[lm_id_base]._ns_loaded; for (unsigned int cnt = 0; cnt preinit != null) afct->preinit (&link_map_audit_state (head, cnt)->cookie); afct = afct->next; } }#endif#ifdef shared if (__glibc_unlikely (glro(dl_debug_mask) & dl_debug_impcalls)) glro(dl_debug_printf) (transferring control: %s, argv[0]);#endif#ifndef shared _dl_debug_initialize (0, lm_id_base);#endif#ifdef have_cleanup_jmp_buf /* memory for the cancellation buffer. */ struct pthread_unwind_buf unwind_buf; int not_first_call; not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf); if (__glibc_likely (! not_first_call)) { struct pthread *self = thread_self; /* store old info. */ unwind_buf.priv.data.prev = thread_getmem (self, cleanup_jmp_buf); unwind_buf.priv.data.cleanup = thread_getmem (self, cleanup); /* store the new cleanup handler info. */ thread_setmem (self, cleanup_jmp_buf, &unwind_buf); /* run the program. */ result = main (argc, argv, __environ main_auxvec_param); } else { /* remove the thread-local data. */# ifdef shared pthfct_call (ptr__nptl_deallocate_tsd, ());# else extern void __nptl_deallocate_tsd (void) __attribute ((weak)); __nptl_deallocate_tsd ();# endif /* one less thread. decrement the counter. if it is zero we terminate the entire process. */ result = 0;# ifdef shared unsigned int *ptr = __libc_pthread_functions.ptr_nthreads;# ifdef ptr_demangle ptr_demangle (ptr);# endif# else extern unsigned int __nptl_nthreads __attribute ((weak)); unsigned int *const ptr = &__nptl_nthreads;# endif if (! atomic_decrement_and_test (ptr)) /* not much left to do but to exit the thread, not the process. */ __exit_thread (); }#else /* nothing fancy, just call the function. */ result = main (argc, argv, __environ main_auxvec_param);#endif exit (result);}
果然,libc_start_main函数的第一个参数,就是待会要调用的main函数,从声明可以看出,这个函数确实有三个参数,第一个是int类型,后面两个都是char **类型。static int libc_start_main (int (*main) (int, char **, char ** main_auxvec_decl), int argc, char **argv, elfw(auxv_t) *auxvec, __typeof (main) init, void (*fini) (void), void (*rtld_fini) (void), void *stack_end){ //....}
而且程序最后调用main函数的时候,也确实是传了三个参数。 result = main (argc, argv, __environ main_auxvec_param);
第三个参数指向的是环境变量,在代码里面确实有函数对它做初始化。
envp类型和argv一样,都是指针数组,每个指针指向一个环境变量,并且最后以null结尾。
要是写代码的话,可以通过循环来输出各个字符串:
#include int main(int argc, char *argv[], char *envp[]){ int i = 0; while (envp[i]) { printf(%s, envp[i++]); } return 0;}运行的结果就是这样的。跟我们用env命令看到的基本一样。
苹果下一代iphone弃用NFC技术,欧美倒退2年?!
齿轮减速电机模块的设计与材料选择
5G发牌一周年之际高通全球副总裁侯明娟 5G是行业机会
三相电机如何测量好坏 万用表怎样测三相电机的好坏
UL 62368-1认证:确保工业电脑产品安全的关键
main函数的三个参数
独家整理!8个超好用的UI资源网!
视频监控数据如何带来价值
光伏逆变器:待开发的“千亿金矿”
室内空气质量监测系统主要应用在哪些领域?
鑫金晖 ▏PCB丝印“黄金孔”现象解决方案成果分享
盘点5G手机及全球前20大技术贡献公司
虹科干货| 虹科物联网HMI/网关结合JMobile软件实现趋势功能
STR6020电源电路原理图与电路组成
ds1302与单片机的连接,51单片机操作ds1302流程展示
油烟污染在线监控系统设计方案
看图识电容
多功能程控闪光集成电路应用电路
炫彩音质 音色绽放 JBL携手陈意涵Estelle发布新品PULSE4
pcb线路板生产有哪些步骤