简体   繁体   English

在指向数组的指针中使用类型转换

[英]Use of typecasting in pointer to an array

int s[4][2] = {1234,56,1212,33,1434,80,1312,78} ;   
int (*p)[2], i, j;    
int *pint;      
for(i=0;i<=3;i++)      
{
    p = s[i];    
    pint = (int*) p;    
    for(j=0;j<=1;j++) 
    {
        printf("%d ",*(pint+j));   
    }

    printf("\n");
}   

Here, p is a pointer to an array of 2 integers and p contains the address of the ith 1-D array. 此处,p是指向2个整数的数组的指针,p包含第i个一维数组的地址。 But why are we typecasting p to pint? 但是为什么我们将p强制转换为品脱呢? And using pint for the rest of our program. 并将品脱用于我们的程序的其余部分。 Why can't we use p only instead of pint? 为什么我们不能仅使用p而不是品脱?

Also, I tried taking p instead of pint, but then it is printing address of 1-D array instead of elements. 另外,我尝试用p代替品脱,但是它是打印一维数组的地址而不是元素。 Why? 为什么?

For starters it would be more correctly to write 对于初学者来说,写起来会更正确

pint = *p;

instead of 代替

pint = (int*) p;    

And this expression statement 而这个表达声明

p = s[i];

is also wrong. 也错了。 s[i] has type int[2] that used in expressions is implicitly converted to an object of type int * . s [i]具有int[2]类型,该类型在表达式中使用,被隐式转换为int *类型的对象。 While the left side operand has type int ( * )[2] . 而左侧操作数的类型为int ( * )[2] So there is an attempt to assign a pointer of type int( * )[2] with an expression of type int * . 因此,有分配类型的指针试图int( * )[2]与式的表达式int * However the types are incompatible. 但是类型不兼容。

You have to write either 你必须写

p = &s[i];

or 要么

p = s + i;

Of course there is no need to introduce a new pointer that to output elements of the array. 当然,无需引入新的指针来输出数组的元素。 However using an additional pointer you can simplify constructions. 但是,使用附加的指针可以简化构造。

Without introducing a new pointer the loop could look like 如果不引入新的指针,则循环看起来像

for ( i = 0; i < 4; i++ )      
{
    p = s + i;    

    for ( j = 0; j < 2; j++ ) 
    {
        printf( "%d ", *( *p + j ) );   
    }

    printf( "\n" );
}   

This quote from the C Standard will be helpful (6.3.2.1 Lvalues, arrays, and function designators) C标准中的该引用将很有帮助(6.3.2.1左值,数组和函数指示符)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 3除非它是sizeof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则类型为“ array of type”的表达式将转换为类型为“ pointer”的表达式键入'',它指向数组对象的初始元素,不是左值。 If the array object has register storage class, the behavior is undefined. 如果数组对象具有寄存器存储类,则该行为是不确定的。

Thus array s having type int[4][2] used in expressions except as an operand of the sizeof or & operations is converted to pointer to its first element of type int ( * )[2] 因此,在表达式中使用的类型为int[4][2]数组s用作sizeof&操作的操作数除外)将转换为指向类型为int ( * )[2]第一个元素的指针

If to apply operator * to the pointer the type of the result object will be int[2] that is an array. 如果将运算符*应用于指针,则结果对象的类型将为int[2] ,即数组。 Again in expressions it in turn is converted to pointer to its first element of type int * . 再次在表达式中将其转换为指向类型为int *第一个元素的指针。

So expression *p has type int * . 因此表达式*p类型为int * If it would be used in the sizeof operator then it had type int[2] . 如果将其用于sizeof运算符,则其类型为int[2]

One Dimension arrays: 一维数组:


int a[2] ; 

is equiavalent to: 等价于:

int *a ;
a = malloc(sizeof(int) * 2);

Two Dimension arrays: 二维数组:


int a[4][2] ;

is equivalent to : 等效于:

int **a ;
a = malloc(sizeof(int *)*4);
for ( i = 0 ; i < 2 ; ++i){
*(a+i) = malloc(sizeof(int)*2);
} 

How can it relate to your example: 它如何与您的示例相关:


int (*p)[2]; 

type of p : int ** p的类型:int **

EDIT:This is wrong 编辑:这是错误的


p is of type (*)[2] p是(*)[2]类型

int s[4][2] ;

type of s : int ** s的类型:int **

EDIT:s is type of (*)[2] not ** 编辑:s是(*)[2]而非**的类型


type of s[i] : int * s [i]的类型:int *
type of s[i][j] : int s [i] [j]的类型:int

Credit goes to the commentators, thanks. 感谢评论者,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM