打印指针的指针 [英] Printing pointer to pointer

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

问题描述

它打印3,而我要打印2时,我只递增一次,我不明白其中的道理。也让我知道这是正确的使用指针牛逼指针?我只是做了一个样本code怎么看指针的指针的工作。

 #包括LT&;&stdio.h中GT;
无效的主要(){
INT A1 [] = {1,2,3,4,5,6};
INT A2 [] = {7,8,9,10,11,12};
为int * A3 = A1;
为int * A4 = A2;
INT ** A [2];
一个[0] = A3;
一个[1] = A4;的printf(%d个*(++(* A)));}


解决方案

您宣布为 INT ** ,所以 *一个数组不是指向int的指针,但指针指向int的指针。递增指针增加它指向的数据类型的大小,因此 ++ * A 在前进时的值 A [0] 通过指针的大小。

您实际存储在什么是[0] 为int的指针,而不是一个指针的指针为int。这是错误的,编译器应该警告你这一点。在你的架构似乎一个指针双int的大小,因此增量 ++ * A 增加了两个整数的指针的大小,所以在价值一个[0],如果跨preTED为为int * 而不是 INT ** ,跳过2

要获得预期声明作为数组结果为int *

 为int * a [2];

It prints 3 while I want to print 2. I do not understand the reason when I am incrementing only once. Also let me know if this is correct use of pointers t pointers? I just made a sample code to see how pointers to pointers work.

#include<stdio.h>
void main(){
int a1[] = {1,2,3,4,5,6};
int a2[] = {7,8,9,10,11,12};
int *a3 = a1;
int *a4 = a2;
int **a[2];
a[0] = a3;
a[1] = a4;

printf("%d",*(++(*a)));



}

解决方案

You declared a as an array of int**, so *a is not a pointer to int but a pointer to pointer to int. Incrementing a pointer adds the size of the data type it points to, so ++*a advances the value at at a[0] by the size of a pointer.

What you actually stored in a[0] is a pointer to int, not a pointer to pointer to int. This is wrong and the compiler should have warned you about this. On your architecture it seems that a pointer is double the size of an int, so the increment ++*a adds the size of two ints to the pointer, so the value at a[0], if interpreted as int* instead of int **, skips over the 2.

To get the results you expect declare a as an array of int *.

int *a[2];

这篇关于打印指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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