(转)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[],而在使用 ...
随机推荐
- Mysql学习(慕课学习笔记6)修改数据表(上)
添加单列 Alter table tb1_name ADD[column] col_name column_definition [first | after col_name] 列名称 ...
- 新花生壳+tomcat(内网映射,无需设置路由器)建站攻略
说明: 1.适用于内网用户(局域网,校园网,或者公司网等无法更改路由器映射的情况) 2.一共花了8块钱…………心疼.不过如果大家有钱的话,8块钱,少吃一顿麻辣烫就好了~总之,这个适用于测试网站,小访问 ...
- 02—从Cocos2DX视角看游戏组成
Cocos2DX引擎按照层次逻辑将游戏元素细分为:导演CCDirector.场景CCScene.图层CCLayer.精灵CCSprite.在上面篇中我们已经知道除导演CCDirector外都继承了CC ...
- android与PC互传文件 adb push
1- df 命令查看文件系统的磁盘空间占用情况 shell@android:/ # adb shell df 2- 找到data文件夹, 上传 adb push /data/Sogou Input_ ...
- docker --- 初识
Docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机).ba ...
- myeclipse中java文件头注释格式设置
转载:http://www.blogjava.net/yxhxj2006/archive/2014/01/14/408940.html myeclipse中java文件头注释格式设置 windows ...
- cf B. Vasily the Bear and Fly
http://codeforces.com/contest/336/problem/B #include <cstdio> #include <cstring> #includ ...
- poj3294 --Life Forms
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12483 Accepted: 3501 Descr ...
- linux xargs 使用
xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题[1]. 例如,下面的命令: rm `find /path -type ...
- SuperSpider——打造功能强大的爬虫利器
SuperSpider——打造功能强大的爬虫利器 1.爬虫的介绍 图1-1 爬虫(spider) 网络爬虫(web spider)是一个自动的通过网络抓取互联网 上的网页的程序,在当今互联网 中 ...