[英]Why i can't compile without declare a matrix like const
My doubt is: why in this code: 我的疑问是:为什么在此代码中:
/*Asignacion de valores en arreglos bidimensionales*/
#include <stdio.h>
/*Prototipos de funciones*/
void imprimir_arreglo( const int a[2][3] );
/*Inicia la ejecucion del programa*/
int main()
{
int arreglo1[2][3] = { { 1, 2, 3 },
{ 4, 5, 6 } };
int arreglo2[2][3] = { 1, 2, 3, 4, 5 };
int arreglo3[2][3] = { { 1, 2 }, { 4 } };
printf( "Los valores en el arreglo 1 de 2 filas y 3 columnas son:\n" );
imprimir_arreglo( arreglo1 );
printf( "Los valores en el arreglo 2 de 2 filas y 3 columnas son:\n" );
imprimir_arreglo( arreglo2 );
printf( "Los valores en el arreglo 3 de 2 filas y 3 columnas son:\n" );
imprimir_arreglo( arreglo3 );
return 0;
} /*Fin de main*/
/*Definiciones de funciones*/
void imprimir_arreglo( const int a[2][3] )
{
int i; /*Contador filas*/
int j; /*Contador columnas*/
for (i = 0; i <=1; i++)
{
for (j = 0; j <= 2; j++)
{
printf( "%d ", a[i][j] );
}
printf( "\n" );
}
} /*Fin de funcion imprime_arreglo*/
I can't compile without declaring the matrix variables like const, and in a vector i can... Why this behavior occurs? 我不能在不声明像const这样的矩阵变量的情况下进行编译,并且在向量中我可以...为什么会出现这种现象? Sorry if my english its bad, i'm speak spanish.
对不起,如果我的英语不好,我会说西班牙语。 I'll be very thankful with your answers.
非常感谢您的回答。
Remove const from 从中删除const
void imprimir_arreglo( const int a[2][3] );
and 和
void imprimir_arreglo( const int a[2][3] )
{
and your code will work. 这样您的代码就会起作用。
There is a real mess about this subject. 这个问题真是一团糟。 You should not use constant modifier for indirect pointers, like
const int**
, as there could be a mess, like: 您不应该将常量修饰符用于间接指针,例如
const int**
,因为这样可能会造成混乱,例如:
Is it an int **
that the values cannot be modified? 不能修改值是否是
int **
?
Or, is it a pointer (or even array) for const int *
? 还是它是
const int *
的指针(甚至数组)?
There is a topic about it on C-faq . 在C-faq上有一个关于它的话题 。
Example: 例:
const int a = 10;
int *b;
const int **c = &b; /* should not be possible, gcc throw warning only */
*c = &a;
*b = 11; /* changing the value of `a`! */
printf("%d\n", a);
It shouldn't allow to change a
's value, gcc
does allow, and clang
runs with warning but doesn't change the value. 它不应该允许改变
a
的值, gcc
确实允许,和clang
与预警运行,但不会改变价值。
Therefore, I'm not sure why the compilers (tried with gcc
and clang
) complain (with warnings, but works) about const T[][x]
, as it is not exactly the same as above. 因此,我不确定为什么编译器(尝试使用
gcc
和clang
)抱怨(带有警告,但可以)关于const T[][x]
,因为它与上面的并不完全相同 。 But, in general, I may say that this kind of problem is solved on different ways depending on your compiler (as gcc
and clang
), so never use const T[][x]
! 但是,总的来说,我可能会说这种问题是根据您的编译器(如
gcc
和clang
)以不同的方式解决的,所以永远不要使用const T[][x]
!
The best alternative, in my opinion, is to use a direct pointer: 我认为最好的替代方法是使用直接指针:
void imprimir_arreglo( const int *a, int nrows, int ncols )
{
int i; /*Contador filas*/
int j; /*Contador columnas*/
for (i = 0; i < nrows; i++)
{
for (j = 0; j < ncols; j++)
{
printf( "%d ", *(a + i * ncols + j) );
}
printf( "\n" );
}
}
And to call: 并致电:
imprimir_arreglo( arreglo1[0], 2, 3 );
This way, your function is more dynamic and more portable. 这样,您的功能将更加动态和可移植。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.