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. hadoop容灾能力测试

    实验简单来讲就是 1. put 一个600M文件,分散3个replica x 9个block 共18个blocks到4个datanode 2. 我关掉了两个datanode,使得大部分的block只在 ...

  2. 空值排序(oracle/sqlserver)

    oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 改变空值办法: (1)用nvl函数或decode函数将null转换为一特定值 替 ...

  3. [Mugeda HTML5技术教程之18]如何在Android应用中使用Mugeda动画内容

    1.简介 本文主要介绍如何在Android应用程序中使用Mugeda动画.Mgeda动画是标准HTML5格式的动画,在Android应用程序中可以使用WebView来加载Mugeda动画.动画内容本身 ...

  4. thinkPHP中省市级联下拉列表

    公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...

  5. 多路查找树之2-3-4树和B树 - 数据结构和算法82

    多路查找树之2-3-4树和B树 让编程改变世界 Change the world by program 由2-3树到2-3-4树 ...... 省略,具体请看视频讲解 ...... B树 一个m阶的B ...

  6. 用C实现字符串分割并返回所有子串

    #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h> ...

  7. Linux/UNIX环境下Oracle数据库多实例开机启动脚本(转)

    操作系统平台:RHEL 5Shell环境:BashOracle:10g2 功能描述:开机时自动切换到oracle用户下,启动oracle的多个实例.并记录数据库的启动情况到自定义的日志文件中. #!/ ...

  8. heritrix 3.2.0 下载

    由于archive.org屏蔽,编译完成版本 http://builds.archive.org/maven2/org/archive/heritrix/heritrix/3.2.0/ 无法下载. 现 ...

  9. OpenWrt openssl library

    Please install the openssl library (with development headers) sudo apt-get install libssl; rite fail ...

  10. [置顶] java ant 配置及构建项目

      Ant是一种基于Java的构建工具.Ant文件是配置构建目标过程的XML文件,也称为Ant脚本.                     (因为对这个不是很了解,所以用词方面可能于个人的理解有偏差 ...