取消引用指针指向整数数组 [英] dereferencing pointer to integer array

查看:124
本文介绍了取消引用指针指向整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向整数数组10.应该怎样取消引用此指针给我?

例如:

 #包括LT&;&stdio.h中GT;主要()
{
    INT变种[10] = {1,2,3,4,5,6,7,8,9,10};
    INT(* ptr的)[10] =安培;无功;    的printf(值=%U%U \\ n,* PTR,PTR); //两个打印2359104.不应该* PTR打印1?
}


解决方案

什么取消引用是指向数组的指针。因此,取消引用给你的阵列。传递一个数组对printf(或任何功能)通过第一元件的地址。

您告诉的printf ,你传递一个 unsigned int类型%U ),但实际上什么传递是为int * 。您所看到的数字是PTED成unsigned int数组间$ P $的第一个元素的地址。

当然,这是不确定的行为。如果您想打印一个地址,你必须使用%P 并传递一个无效*

I have a pointer to integer array of 10. What should dereferencing this pointer give me?

Eg:

#include<stdio.h>

main()
{
    int var[10] = {1,2,3,4,5,6,7,8,9,10};
    int (*ptr) [10] = &var;

    printf("value = %u %u\n",*ptr,ptr);  //both print 2359104. Shouldn't *ptr print 1?


}

解决方案

What you dereference is a pointer to an array. Thus, dereferencing gives you the array. Passing an array to printf (or to any function) passes the address of the first element.

You tell printf that you pass it an unsigned int (%u), but actually what is passed is an int*. The numbers you see are the addresses of the first element of the array interpreted as an unsigned int.

Of course, this is undefined behavior. If you want to print an address, you have to use %p and pass a void*.

这篇关于取消引用指针指向整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆