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

  • Functions as parameters
  • Arrays as parameters

Arrays as parameters

The 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 parameters

For 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
void f(void (*g)(void));  //function pointer

 
 

Note that parentheses around *g is needed. Otherwise, it would specify a function returning void*, instead of a pointer to a function returning void.

Back to arrays

Now, 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);
int main(int c, char *argv[]);
int main(int c, char *argv[]);
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
// allows CPU to pre-load some memory.
int main(int c, char *argv[static ]); // says: argv is a constant pointer pointing to a char*
int main(int c, char *argv[const]);   //you won't be able to change "argv" within the function - it has become a const pointer. // says the same as the previous one
int main(int c, char ** const argv);  //you won't be able to change "argv" within the function - it has become a const pointer.
 

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 Warning

Note 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[];
char **c = &c; // does not work. typedef char array[];
array *pc = &c; // *does* work. // same without typedef. Parens needed, because [...] has
// higher precedence than '*'. Analogous to the function example above.
char (*array)[] = &c;

(转)Should I use char** argv or char* argv[]的更多相关文章

  1. char *argv[] 与 char **argv

    #include<stdio.h> #include<string.h> int main(int argc,char *argv[])//同int main(int argc ...

  2. char **argv 与char *argv[]

    1.char **argv 分析:argv是一个指针变量,argv的指向(*argv)是char *,也就是argv指向的也是一个指针 : *argv的指向(**argv)是char. 2.char ...

  3. C语言执行时报错“表达式必须是可修改的左值,无法从“const char [3]”转换为“char [120]” ”,原因:字符串不能直接赋值

    解决该问题的方法:使用strcpy函数进行字符串拷贝   原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string ...

  4. C++中 char *s 和 char s[] 的区别

    原因 刚好看到给main传递参数,书上(C++Primer)说“ int main(int argc, char *argv[])也可以写成 int main(int argc, char **arg ...

  5. 理解C/C++中const char*、char* const、const char* const、char* const*等等

    先说些题外话,今天学习execve(2)的使用,由于书上代码使用的是C89标准,所以下面这种代码都被我修改了 char* s[] = { "aaa", "bbb" ...

  6. char 与 signed char 和 unsigned char三者之间的关系

    # char 与 signed char 和 unsigned char三者之间的关系 三者都占用 1个字节,即 8 bit signed char取值范围(-128, 127) unsigned c ...

  7. 关于 char 和 unsigned char 的区别

    首先卖个关子: 为什么网络编程中的字符定义一般都为无符号的字符?   char buf[16] = {0}; unsigned char ubuf[16] = { 0 };   上面两个定义的区别是: ...

  8. const char*和const char[]怎么识别?

    #include <iostream> using namespace std; template<typename T> class _ischararray_; templ ...

  9. c语言中 char* 和 unsigned char* 的区别浅析(转)

    原文:https://blog.csdn.net/guotianqing/article/details/77341657 背景最近在项目中遇到了一个编译警告,是因为定义的变量为char[],而在使用 ...

随机推荐

  1. 洛谷 P3395 路障

    P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个方 ...

  2. hdu5347 MZL's chemistry(打表)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's chemistry Time Limit: 2000/1000 MS ...

  3. 关于页面刷新或者调用方法事获取不到元素信息或者出现缺少对象错误的换位思考setTimeout的使用

    这两天客户的需求不能定下来,做闲人好长时间了,不如来整理下最近碰到的一些个小麻烦. 正题: 场景一. 最近在开发的过程中使用到了百度的富客户端文本编辑器(ueditor)---这是一款功能很强大的文本 ...

  4. jquery height、innerHeight、outHeight

    JQuery有很多的height,不总结一下你就要被他搞晕,所以为了保持清醒,汇总在下面 height:height innerHeight:height+padding outerHeight(fa ...

  5. Python即时网络爬虫项目启动说明

    作为酷爱编程的老程序员,实在按耐不下这个冲动,Python真的是太火了,不断撩拨我的心. 我是对Python存有戒备之心的,想当年我基于Drupal做的系统,使用php语言,当语言升级了,推翻了老版本 ...

  6. [C入门 - 游戏编程系列] 贪吃蛇篇(五) - 蛇实现

    因为已经写了食物的实现,所以我不知道到底是该先写世界的实现还是蛇的实现.因为世界就是一个窗口,可以立刻在世界中看到食物的样子,对于大多数人来说,如果写完代码立刻就能看到效果,那就再好不过了.可是,我最 ...

  7. 【转】简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue

    转载地址:http://blog.csdn.net/mn11201117/article/details/8671497 SynchronousQueue SynchronousQueue是无界的,是 ...

  8. Shortest Word Distance 解答

    Question Given a list of words and two words word1 and word2, return the shortest distance between t ...

  9. c++在函数后面加const

    非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...

  10. hdu 5430 Reflect (数学推导题)

    Problem Description We send a light from one point on a mirror material circle,it reflects N times a ...