C++ - char** argv vs. char* argv[]

本文详细解释了在C/C++编程中,当使用函数参数时,char**argv与char*argv[]的用法区别,包括它们的内存分配方式、如何传递参数以及在函数调用时的注意事项。了解这些差异对于编写高效且无错误的代码至关重要。
摘要由CSDN通过智能技术生成

What is the difference between char** argv and char* argv[]?int main(int argc, char** argv) andint main(int argc, char* argv[])?

t's a language rule. When you declare or define a function with the syntaxX foo(Y a[]), then it actually becomes X foo(Y *a). What looks like an array argument to a function is really a pointer. Sinceargv is declared as an array (of pointers), it becomes a pointer (to pointers).

All same except for the following:

  • Sizeof will give different results for both
  • Also second one may not be reassigned to new memory area since it's an array
  • With second one you can use only those indexes which arevalid. It's unspecified by C/C++ if you try to use an array index beyondarray length. However with char** you can use any index from 0 to ...
  • Second form can only be used as formal parameters to a function. While first can even be used to declare variables within a stack.

char    *argv[]    :    argv 是一个指针数组,他的元素个数是argc,存放的是指向每一个参数的指针


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值