[英]Incompatible types in assignment char ** to char *
I have the following bit of code: 我有以下代码:
As a global variable: 作为全局变量:
char *orderFiles[10];
And then my main method: 然后是我的主要方法:
int main(int argc, char *argv[])
{
orderFiles = argv;
}
However it keeps giving me an error. 但是,它总是给我一个错误。 What am I doing wrong?
我究竟做错了什么?
It's giving you an error because char *x[10]
gives you an array of ten char pointers which is non-modifiable. 这给您带来了错误,因为
char *x[10]
为您提供了十个不可修改的char指针数组。 In other words, you cannot assign to x
, nor change it in any way. 换句话说,您不能分配给
x
,也不能以任何方式对其进行更改。 The equivalent changeable version would be char **orderFiles
- you can assign argv
to that just fine. 等效的可变版本为
char **orderFiles
您可以将argv
分配给它。
As an aside, you could transfer individual arguments to your array thus: 顺便说一句,您可以将各个参数传递给数组,从而:
for (i = 0; i <= argc && i < sizeof(orderFiles)/(sizeof(*orderFiles); i++)
orderFiles[i] = argv[i];
but that seems rather convoluted. 但这似乎令人费解。 It will either fill up
orderFiles
with the first N
arguments or partially fill it, making the next one NULL. 它将使用前
N
参数填充orderFiles
或部分填充它,使下一个NULL。
If your intent is simply to stash away the arguments into a global so that you can reference them anywhere, you should do something like: 如果您只是想将参数存放在全局变量中以便可以在任何地方引用它们,则应执行以下操作:
#include <stdio.h>
char **orderFiles;
int orderCount;
static void someFn (void) {
int i;
printf ("Count = %d\n", orderCount);
for (i = 0; i < orderCount; i++)
printf ("%3d: [%s]\n", i, orderFiles[i]);
// or, without orderCount:
// for (i = 0; orderFiles[i] != NULL; i++)
// printf ("%3d: [%s]\n", i, orderFiles[i]);
// printf ("Count was %d\n", i);
}
int main (int argc, char **argv) {
orderCount = argc;
orderFiles = argv;
someFn();
return 0;
}
That code saves the arguments into globals so they can be accessed in a different function. 该代码将参数保存到全局变量中,以便可以在其他函数中对其进行访问。
You should save both arguments to main
if you want to use argc
as well although, technically, it's not necessary since argv[argc]
is guaranteed to be NULL for hosted environments - you could use that to detect the end of the argument array. 你应该保存两个参数来
main
,如果你想使用argc
以及虽然在技术上,它不是必要的,因为argv[argc]
保证是空的托管环境-你可以用它来检测参数数组的结尾。
orderFiles is a const char **
, you can't modify it (the array pointer itself). orderFiles是一个
const char **
,您不能修改它(数组指针本身)。
You could try assigning the array members (ie orderFiles[0] = argv[0] and so on). 您可以尝试分配数组成员(即orderFiles [0] = argv [0]等)。
The problem is that there is a difference between arrays initialized without a length, and those initialized with one. 问题在于没有长度初始化的数组与以一个长度初始化的数组之间存在差异。 Remove the 10 from the global variables declaration, and then it should work
从全局变量声明中删除10,然后它应该可以工作
The reason for this is that argv is really just a char* * , but orderFiles is an array of 10 char*. 原因是argv实际上只是一个char * * ,但是orderFiles是一个10 char *的数组。
There is an implicit char**
for orderFiles
yes, but it's constant because you initialized it to a link time allocated block of memory by specifying the size [10]
. orderFiles
yes有一个隐式char**
,但是它是常量,因为您通过指定大小[10]
将其初始化为分配给链接时间的内存块。 You should create a non-constant char**
or maybe memcpy
from argv
to your array. 您应该从
argv
创建非常数char**
或memcpy
到数组。
Like they all said two different data types. 就像他们都说了两种不同的数据类型。 In other terms think of it this way: argv is an array of c-strings, and your orderFiles is declared as a single c-string.
换句话说,您可以这样考虑:argv是一个c字符串数组,您的orderFiles被声明为单个c字符串。
So how to assign orderFiles depends on what you're trying to do. 因此,如何分配orderFiles取决于您要执行的操作。 I typically iterate through argv to get the arguments passed to the application.
我通常通过argv进行迭代,以将参数传递给应用程序。 Note that argv[0] is the application name.
注意argv [0]是应用程序名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.