I have noticed that pointers are 4 bytes while ints are 2 bytes. When I make a declaration such as:
我注意到指针是4个字节,而整数是2个字节。当我做出如下声明:
int * myGreatArray[50];
am I declaring an array of 50 pointers to ints? I thought I was declaring a pointer to an array of integers, which is functionally the same as
我是否正在宣布一个由50个指针组成的数组?我以为我正在声明一个指向整数数组的指针,它在功能上与它相同
int ** myGreatArray = malloc(50 * sizeof(int *));
However, I was informed by this tutorial that such a declaration is actually creating an array of pointers to ints, which seems more inefficient than just simply storing the ints themselves.
但是,本教程告诉我,这样的声明实际上是在创建一个指向int的指针数组,这似乎比仅仅存储int本身更低效。
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm
Can anyone clarify this for me?
谁能为我澄清一下这个?
5 个解决方案
#1
3
It is a question of precedence of operators.
这是运营商的优先权问题。
int * myGreatArray[50]; // array of 50 pointers to int
int (*ptArray)[50]; // pointer to array of 50 ints
#2
2
int * myGreatArray[50];
is indeed creating an array of pointers to int
. If you wanted a pointer to an array of int
, it would be written as
确实在创建一个指向int的指针数组。如果你想要一个指向int数组的指针,它将被写为
int (*myGreatArray)[50];
However, I was informed by this tutorial that such a declaration is actually creating an array of pointers to ints, which seems more inefficient than just simply storing the ints themselves.
但是,本教程告诉我,这样的声明实际上是在创建一个指向int的指针数组,这似乎比仅仅存储int本身更低效。
Depends on what you are trying to do. It's useful for creating "jagged" 2D arrays, where each "row" can be a different length:
取决于你想要做什么。它对于创建“锯齿状”2D数组很有用,其中每个“行”可以是不同的长度:
for ( size_t i = 0; i < 50; i++ )
myGreatArray[i] = malloc( sizeof *myGreatArray[i] * some_length(i) );
where some_length(i)
represents the number of elements for a particular "row". It can also be used to point to existing arrays:
其中some_length(i)表示特定“行”的元素数。它还可以用于指向现有数组:
int foo[] = {1, 2, 3};
int bar[] = {4, 5, 6, 7, 8};
...
int *myGreatArray[] = {foo, bar, ...};
#3
1
When you are writing this declaration:
在撰写此声明时:
int * myGreatArray[50];
its the same as writing -
它和写作一样 -
int ** myGreatArray = malloc(50 * sizeof(int *));
and you will get an array of 50 pointers to int, while if you will use this line:
你会得到一个50个指向int的数组,如果你将使用这一行:
int myGreatArray[50];
or:
要么:
int* myGreatArray = (int*)malloc(sizeof(int)*50)
you will get an array of 50 int variables. I hope that my comment was helpful for you :D and if you still have questions ask and i will answer you ASAP. Have a great day ;)
你将得到一个包含50个int变量的数组。我希望我的评论对你有帮助:D如果你还有问题,我会尽快回答你。祝你有美好的一天 ;)
#4
1
Declaring an array:
声明一个数组:
int * myGreatArray[50];
This is an array that stores 50 pointers to int
. Be aware that it does not allocate the storage for those integers, just for the storage of the pointers.
这是一个存储50个int指针的数组。请注意,它不会为这些整数分配存储空间,仅用于存储指针。
int arr[50]; //array of 50 integers
int * parr = arr; /*pointer to an int, which may be
the beginning of an array*/
Passing to a function:
传递给一个功能:
This is exactly what I was hoping I'd discover, so when passing an array into a function, is it more efficient to pass a pointer to an array as opposed to an array of pointers? I'd think yes. – Michael Hackman
这正是我希望发现的,所以当将数组传递给函数时,将指针传递给数组而不是指针数组是否更有效?我想是的。 - 迈克尔哈克曼
The two function definitions:
两个函数定义:
void doStuffToArray(int ** array, size_t len)
{
//dostuff
}
and
和
void doStuffToArray(int * array[], size_t len)
{
//dostuff
}
are functionally identical. When you pass an array, the function actually receives a pointer to the array.
在功能上是相同的。传递数组时,该函数实际接收指向数组的指针。
To call the functions, you can pass the array (devolves to pointer to the beginning of the array, (recommended) or a pointer to the beginning of the array (not recommended for full arrays, but is useful to pass pointers to sections of arrays):
要调用这些函数,您可以传递数组(转换为指向数组开头的指针,(推荐)或指向数组开头的指针(不推荐用于完整数组,但对于将指针传递给数组的部分很有用) ):
int arr[10] = {};
doStuffToArray(arr, sizeof(arr)/sizeof(arr[0])); //functionally identical
doStuffToArray(&arr[0], sizeof(arr)/sizeof(arr[0])); //functionally identical
When passing an array of pointers, there are two function definitions that can be used, e.g. argv is an array of pointers to char arrays:
当传递指针数组时,有两个可以使用的函数定义,例如, argv是一个指向char数组的指针数组:
int main(int argc, char * argv[]){return 0;} //functionally identical
int main(int argc, char ** argv ){return 0;} //functionally identical
My advice is to use the array notation (with the []
) as this is a declaration of intent, instead of the equivalent but more ambiguous pointer notation.
我的建议是使用数组表示法(使用[]),因为这是一个意图声明,而不是等效但更模糊的指针表示法。
If you know how big the array is, then argv
could have been defined as an 'array of arrays' char argv[][]
which would be great, but can't be done. When defining a function, only the first array dimension can be undefined, any further dimensions have to be defined. If you know how big it is though, there is nothing to stop you from creating a function:
如果你知道数组有多大,那么argv可以被定义为'数组数组'char argv [] [],这将是很好的,但是无法完成。定义函数时,只能定义第一个数组维,必须定义任何其他维。如果你知道它有多大,那么没有什么可以阻止你创建一个函数:
void doStuffToMyArray( int array[][10]){
/*...*/
}
#5
0
In fact in this call
实际上在这个电话中
malloc(50 * sizeof(int));
there is allocated neither array.:) There is allocated an extent of memory. You can interpret it in various ways.
没有分配数组。:)分配了一定程度的内存。您可以通过各种方式解释它。
For example you can write
例如,你可以写
int *myGreatArray = malloc(50 * sizeof(int));
char *myGreatArray = malloc(50 * sizeof(int));
or even like
甚至喜欢
long *myGreatArray = malloc(50 * sizeof(int));
provided that sizeof( long )
is equal to 2 * sizeof( int )
.
假设sizeof(long)等于2 * sizeof(int)。
or like
或者喜欢
int ( *myGreatArray )[50] = malloc(50 * sizeof(int));
The only requirement is that there was allocated enough memory for the object(s) you are going to store there.
唯一的要求是为你要存储的对象分配了足够的内存。
If you want to allocate dynamically an extent of memory for an array similar to this declaration
如果要为类似于此声明的数组动态分配内存范围
int * myGreatArray[50];
then you should write
那你应该写
int ** myGreatArray = malloc(50 * sizeof(int *));
^^^^^^ ^^^^^
If you want to allocate dynamically an extent of memory for an array similar to this declaration
如果要为类似于此声明的数组动态分配内存范围
int myGreatArray[50];
that is an array of 50 objects of type int
then you should write
这是一个包含int类型的50个对象的数组,那么你应该写
int * myGreatArray = malloc(50 * sizeof(int));
^^^^^ ^^^^^
#1
3
It is a question of precedence of operators.
这是运营商的优先权问题。
int * myGreatArray[50]; // array of 50 pointers to int
int (*ptArray)[50]; // pointer to array of 50 ints
#2
2
int * myGreatArray[50];
is indeed creating an array of pointers to int
. If you wanted a pointer to an array of int
, it would be written as
确实在创建一个指向int的指针数组。如果你想要一个指向int数组的指针,它将被写为
int (*myGreatArray)[50];
However, I was informed by this tutorial that such a declaration is actually creating an array of pointers to ints, which seems more inefficient than just simply storing the ints themselves.
但是,本教程告诉我,这样的声明实际上是在创建一个指向int的指针数组,这似乎比仅仅存储int本身更低效。
Depends on what you are trying to do. It's useful for creating "jagged" 2D arrays, where each "row" can be a different length:
取决于你想要做什么。它对于创建“锯齿状”2D数组很有用,其中每个“行”可以是不同的长度:
for ( size_t i = 0; i < 50; i++ )
myGreatArray[i] = malloc( sizeof *myGreatArray[i] * some_length(i) );
where some_length(i)
represents the number of elements for a particular "row". It can also be used to point to existing arrays:
其中some_length(i)表示特定“行”的元素数。它还可以用于指向现有数组:
int foo[] = {1, 2, 3};
int bar[] = {4, 5, 6, 7, 8};
...
int *myGreatArray[] = {foo, bar, ...};
#3
1
When you are writing this declaration:
在撰写此声明时:
int * myGreatArray[50];
its the same as writing -
它和写作一样 -
int ** myGreatArray = malloc(50 * sizeof(int *));
and you will get an array of 50 pointers to int, while if you will use this line:
你会得到一个50个指向int的数组,如果你将使用这一行:
int myGreatArray[50];
or:
要么:
int* myGreatArray = (int*)malloc(sizeof(int)*50)
you will get an array of 50 int variables. I hope that my comment was helpful for you :D and if you still have questions ask and i will answer you ASAP. Have a great day ;)
你将得到一个包含50个int变量的数组。我希望我的评论对你有帮助:D如果你还有问题,我会尽快回答你。祝你有美好的一天 ;)
#4
1
Declaring an array:
声明一个数组:
int * myGreatArray[50];
This is an array that stores 50 pointers to int
. Be aware that it does not allocate the storage for those integers, just for the storage of the pointers.
这是一个存储50个int指针的数组。请注意,它不会为这些整数分配存储空间,仅用于存储指针。
int arr[50]; //array of 50 integers
int * parr = arr; /*pointer to an int, which may be
the beginning of an array*/
Passing to a function:
传递给一个功能:
This is exactly what I was hoping I'd discover, so when passing an array into a function, is it more efficient to pass a pointer to an array as opposed to an array of pointers? I'd think yes. – Michael Hackman
这正是我希望发现的,所以当将数组传递给函数时,将指针传递给数组而不是指针数组是否更有效?我想是的。 - 迈克尔哈克曼
The two function definitions:
两个函数定义:
void doStuffToArray(int ** array, size_t len)
{
//dostuff
}
and
和
void doStuffToArray(int * array[], size_t len)
{
//dostuff
}
are functionally identical. When you pass an array, the function actually receives a pointer to the array.
在功能上是相同的。传递数组时,该函数实际接收指向数组的指针。
To call the functions, you can pass the array (devolves to pointer to the beginning of the array, (recommended) or a pointer to the beginning of the array (not recommended for full arrays, but is useful to pass pointers to sections of arrays):
要调用这些函数,您可以传递数组(转换为指向数组开头的指针,(推荐)或指向数组开头的指针(不推荐用于完整数组,但对于将指针传递给数组的部分很有用) ):
int arr[10] = {};
doStuffToArray(arr, sizeof(arr)/sizeof(arr[0])); //functionally identical
doStuffToArray(&arr[0], sizeof(arr)/sizeof(arr[0])); //functionally identical
When passing an array of pointers, there are two function definitions that can be used, e.g. argv is an array of pointers to char arrays:
当传递指针数组时,有两个可以使用的函数定义,例如, argv是一个指向char数组的指针数组:
int main(int argc, char * argv[]){return 0;} //functionally identical
int main(int argc, char ** argv ){return 0;} //functionally identical
My advice is to use the array notation (with the []
) as this is a declaration of intent, instead of the equivalent but more ambiguous pointer notation.
我的建议是使用数组表示法(使用[]),因为这是一个意图声明,而不是等效但更模糊的指针表示法。
If you know how big the array is, then argv
could have been defined as an 'array of arrays' char argv[][]
which would be great, but can't be done. When defining a function, only the first array dimension can be undefined, any further dimensions have to be defined. If you know how big it is though, there is nothing to stop you from creating a function:
如果你知道数组有多大,那么argv可以被定义为'数组数组'char argv [] [],这将是很好的,但是无法完成。定义函数时,只能定义第一个数组维,必须定义任何其他维。如果你知道它有多大,那么没有什么可以阻止你创建一个函数:
void doStuffToMyArray( int array[][10]){
/*...*/
}
#5
0
In fact in this call
实际上在这个电话中
malloc(50 * sizeof(int));
there is allocated neither array.:) There is allocated an extent of memory. You can interpret it in various ways.
没有分配数组。:)分配了一定程度的内存。您可以通过各种方式解释它。
For example you can write
例如,你可以写
int *myGreatArray = malloc(50 * sizeof(int));
char *myGreatArray = malloc(50 * sizeof(int));
or even like
甚至喜欢
long *myGreatArray = malloc(50 * sizeof(int));
provided that sizeof( long )
is equal to 2 * sizeof( int )
.
假设sizeof(long)等于2 * sizeof(int)。
or like
或者喜欢
int ( *myGreatArray )[50] = malloc(50 * sizeof(int));
The only requirement is that there was allocated enough memory for the object(s) you are going to store there.
唯一的要求是为你要存储的对象分配了足够的内存。
If you want to allocate dynamically an extent of memory for an array similar to this declaration
如果要为类似于此声明的数组动态分配内存范围
int * myGreatArray[50];
then you should write
那你应该写
int ** myGreatArray = malloc(50 * sizeof(int *));
^^^^^^ ^^^^^
If you want to allocate dynamically an extent of memory for an array similar to this declaration
如果要为类似于此声明的数组动态分配内存范围
int myGreatArray[50];
that is an array of 50 objects of type int
then you should write
这是一个包含int类型的50个对象的数组,那么你应该写
int * myGreatArray = malloc(50 * sizeof(int));
^^^^^ ^^^^^