简体   繁体   English

数组在通过引用传递给函数之前和之后显示不同的地址

[英]Array shows different addresses, before and after being passed to a function by reference

I was curious to know why the addresses of the same array ie array b in my case is changing before and after it has been passed to the function manipulation(), as shown in the output picture. 我很想知道为什么相同数组(即我的数组b)的地址在传递给函数handler()之前和之后都会发生变化,如输出图片所示。 在此处输入图片说明 Please share your thoughts and help me figure this out. 请分享您的想法,并帮助我解决这个问题。 Thank you! 谢谢!

#include <stdio.h>
void manipulation(int *pa,int *pb){   
    int i;
    for(i=0;i<10;i++){
        *(pb+i)=*(pa+i);
        printf("%d\t  %04x\n",*pb+i,&pb+i);
    }
}

int main(){
    int a[10],b[10];
    int i;

    int *point;
    point = &b[0];
    printf("Enter the array elements\n");
    for(i=0;i<10;i++){
        scanf("%d\n",&a[i]);    
    }

    for(i=0;i<10;i++){
        printf("%04x\n",&point+i);
    }

    manipulation(&a[0],&b[0]);
    return 0;
}

because you print ( address of your pointer) + i: 因为您打印(指针的地址 )+ i:

printf("%04x\n",&point+i);
printf("%d\t  %04x\n",*pb+i,&pb+i);

you want to print the pointer + i: 您要打印指针+ i:

printf("%04x\n",point+i);
printf("%d\t  %04x\n",*pb+i,pb+i);

Also you have a bug in there: 另外,您还有一个错误:

printf("%d\t  %04x\n",*pb+i,pb+i);

*pb+i is interpreted as (*pb) + i that is the first value of the array plus i, or simply pb[0]+i . *pb+i被解释为(*pb) + i ,它是数组加i的第一个值,或者简称为pb[0]+i

you seem to get the right answer because your array is 1 2 3 ... 您似乎得到了正确的答案,因为您的数组是1 2 3 ...

you probably want this: 您可能想要这样:

printf("%d\t  %04x\n",*(pb+i),pb+i);

or simply: 或者简单地:

printf("%d\t  %04x\n",pb[i],pb+i);

Yeah, the real problems is comparing the address of point to the address of pb . 是的,真正的问题是将point的地址与pb的地址进行比较。

Try using point + i (or &point[i] ) and pb + i (or &pb[i] ). 尝试使用point + i (或&point[i] )和pb + i (或&pb[i] )。 I'm sure you will get the expected answer. 我相信您会得到预期的答案。


In case you are curious, &point + i is the location in memory of the local variable point plus the value of i . 如果您感到好奇,则&point + i是局部变量point在内存中的point加上i的值。 &pb + i is the location in memory of the parameter pb plus the value of i . &pb + i是参数pbi的值在内存中的位置。 They are different variables, so they have different locations in memory. 它们是不同的变量,因此它们在内存中具有不同的位置。

point stores the address of array a. 点存储数组a的地址。 &point prints the address of point and adding 4 through out the loop. &point打印点的地址,并在整个循环中加4。 same is the case with &pb. &pb也是如此。 28fee8 is the place where point is stored and 28fed4 is the place where pb is stored. 28fee8是存储点的位置,而28fed4是存储pb的位置。

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

相关问题 传递给线程后保持引用有效 - Keeping reference alive after being passed to thread 为什么不通过引用传递数组? - Why isn't the array being passed by reference? 为什么为传递给函数的指针获取不同的内存地址? - Why Do I Get Different Memory Addresses For Pointer Passed To Function? 如何使 function 参数在 C++ 中单行传递之前和之后执行某些操作 - How to make a function argument do something before and after being passed in one-liner in C++ 使用由函数传递的数组。 - Using an Array being passed by a function. 另一个结构体内部的结构体数组的成员变量没有通过引用传递 - Member variables of an array of structs that is inside of another struct are not being passed by reference 如果它是一个函数争论并且正在通过引用传递值,我应该删除一个指针吗? - Should I delete a pointer if it is a function arguemente and is being passed values by reference? 在传递给func之后,Struct具有不同的大小 - Struct has different size after being passed to func C ++:指针在传递后包含不同的地址 - C++: Pointer contains different address after being passed 为什么指针即使在作为引用传递给函数之后也不会改变? - Why a pointer is unmutated even after passed to a function as reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM