(转)Should I use char** argv or char* argv[]
| 
 | 
 As you are just learning C, i recommend you to really try to understand the differences between arrays and pointers first instead of the common things. In the area of parameters and arrays, there are a few confusing rules that should be clear before going on. First, what you declare in a parameter list is treated special. There are such situations where things don't make sense as a function parameter in C. These are 
 Arrays as parametersThe second maybe is not immediately clear. But it becomes clear when you consider that the size of an array dimension is part of the type in C (and an array whose dimension size isn't given has an incomplete type). So, if you would create a function that takes an array by-value (receives a copy), then it could do so only for one size! In addition, arrays can become large, and C tries to be as fast as possible. In C, for these reasons, array-values are not existent. If you want to get the value of an array, what you get instead is a pointer to the first element of that array. And herein actually already lies the solution. Instead of drawing an array parameter invalid up-front, a C compiler will transform the type of the respective parameter to be a pointer. Remember this, it's very important. The parameter won't be an array, but instead it will be a pointer to the respective element type. Now, if you try to pass an array, what is passed instead is a pointer to the arrays' first element. Excursion: Functions as parametersFor completion, and because i think this will help you better understand the matter, let's look what the state of affairs is when you try to have a function as a parameter. Indeed, first it won't make any sense. How can a parameter be a function? Huh, we want a variable at that place, of course! So what the compiler does when that happens is, again, to transform the function into a function pointer. Trying to pass a function will pass a pointer to that respective function instead. So, the following are the same (analogous to the array example): void f(void g(void)); //function 
Note that parentheses around  Back to arraysNow, i said at the beginning that arrays can have an incomplete type - which happens if you don't give a size yet. Since we already figured that an array parameter is not existant but instead any array parameter is a pointer, the array's size doesn't matter. That means, the compiler will translate all of the following, and all are the same thing: int main(int c, char **argv); 
Of course, it doesn't make much sense to be able to put any size in it, and it's just thrown away. For that reason, C99 came up with a new meaning for those numbers, and allows other things to appear between the brackets: // says: argv is a non-null pointer pointing to at least 5 char*'s 
The last two lines say that you won't be able to change "argv" within the function - it has become a const pointer. Only few C compilers support those C99 features though. But these features make it clear that the "array" isn't actually one. It's a pointer. A word of WarningNote that all i said above is true only when you have got an array as a parameter of a function. If you work with local arrays, an array won't be a pointer. It will behave as a pointer, because as explained earlier an array will be converted to a pointer when its value is read. But it should not be confused with pointers. One classic example is the following: char c[];  | 
(转)Should I use char** argv or char* argv[]的更多相关文章
- char *argv[] 与 char **argv
		
#include<stdio.h> #include<string.h> int main(int argc,char *argv[])//同int main(int argc ...
 - char **argv  与char *argv[]
		
1.char **argv 分析:argv是一个指针变量,argv的指向(*argv)是char *,也就是argv指向的也是一个指针 : *argv的指向(**argv)是char. 2.char ...
 - C语言执行时报错“表达式必须是可修改的左值,无法从“const char [3]”转换为“char [120]” ”,原因:字符串不能直接赋值
		
解决该问题的方法:使用strcpy函数进行字符串拷贝 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string ...
 - C++中 char *s 和 char s[] 的区别
		
原因 刚好看到给main传递参数,书上(C++Primer)说“ int main(int argc, char *argv[])也可以写成 int main(int argc, char **arg ...
 - 理解C/C++中const char*、char* const、const char* const、char* const*等等
		
先说些题外话,今天学习execve(2)的使用,由于书上代码使用的是C89标准,所以下面这种代码都被我修改了 char* s[] = { "aaa", "bbb" ...
 - char 与 signed char 和 unsigned char三者之间的关系
		
# char 与 signed char 和 unsigned char三者之间的关系 三者都占用 1个字节,即 8 bit signed char取值范围(-128, 127) unsigned c ...
 - 关于 char  和 unsigned  char 的区别
		
首先卖个关子: 为什么网络编程中的字符定义一般都为无符号的字符? char buf[16] = {0}; unsigned char ubuf[16] = { 0 }; 上面两个定义的区别是: ...
 - const char*和const char[]怎么识别?
		
#include <iostream> using namespace std; template<typename T> class _ischararray_; templ ...
 - c语言中 char* 和 unsigned char* 的区别浅析(转)
		
原文:https://blog.csdn.net/guotianqing/article/details/77341657 背景最近在项目中遇到了一个编译警告,是因为定义的变量为char[],而在使用 ...
 
随机推荐
- hdu1372  BFS求最短路径长度
			
C - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
 - js中的function
			
Math方法详解 Math.sqrt(x) 计算X开平方 Math.sqrt(x,y) 计算xy Math.round(x) 计算x 四舍五入的值 getBoundingClientRe ...
 - Fun with dynamicobject dynamic and the settings table
			
What came before In my previous post I discussed ways of making the settings table using Generics to ...
 - Android-1
			
@String 支持多语言 layout中的text文本中String都尽量定义在String.xml中,便于多语言管理. <resources> <string name=&quo ...
 - final关键字的作用
			
final 可以修饰类.方法.变量. (1):final修饰的变量是一个常量,只能被赋值一次. 常量一般使用final声明,如: public static final String CHINA = ...
 - MYSQL连接字符串参数详细解析(大全参考)
			
Connector/Net Connection String Options Reference Database=dbname;Data Source=192.168.1.1;Port=3306; ...
 - (转)fastcgi协议的简单实现
			
FastCgi不仅可以用于webserver与PHP的交互,也可用于任何两个应用之间的交互,PHPer用的比较多的应该就是用于两个子系统之间的交互. 比如A系统和B系统分部独立的部署在两台机器上,其之 ...
 - Java学习系列(一)Java的运行机制、JDK的安装配置及常用命令详解
			
俗话说:“十五的月亮十六圆”.那学习是不是也是如此呢?如果把月亮看成是我们的愿望,那十五便是我们所处的“高原期”,坚持迈过这个坎,我相信你的愿望终究会现实的.记得马云曾说:今天很残酷,明天更残酷,后天 ...
 - c++ 06
			
一.下标操作符 A a (...); cout << a[3] << endl; cout << a.operator[] (3) << endl; c ...
 - SQL查询最近三个月的数据(查询最近几天,几年等等)
			
定义和用法 :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId OrderPayD ...