指针与指针位置之间的差异

时间:2018-09-24 14:53:29

标签: c pointers

此代码为i和&i打印不同的值,并且它们都不等于10。请解释一下这两个数字所表示的含义。

#include<stdio.h>
int main(){
    int p=10;
    int *i=&p;
    printf("%d %d",i,&i);
}

This is what the output look like

2 个答案:

答案 0 :(得分:0)

$(document).ready(function() { $('#btn-search').on('click', function() { $.ajax({ type: "GET", url: "{{myurlapi }}", }).done(function(result) { var table = $('#example').DataTable({ "data": result.data, "columns": [{ "data": "id" }, { "data": "name" }, { "data": null } ], "columnDefs": [{ "searchable": false, "orderable": false, "targets": 0 }, { width: '3%', targets: 0 }, { targets: -1, data: null, defaultContent: '<div class="btn-group"> <button type="button" class=" btn btn-view"><span> <i class="icon-plus"></i></span> </button></div>' }, ], "processing": true, "retrieve": true, "searching": false }); table.clear().draw(); table.rows.add(result).draw(); }); }); $(document).ajaxComplete(function() { $('#example tbody').on('click', '.btn-view', function(e) { alert('something') }); }); }); i,将用于存储integer pointer的地址。在这种情况下,integer variable存储在主存储器的堆栈区域中,当您打印i时,这意味着您要打印存储&i的地址。当您打印i时,它表示您打印i的值(i的值是i的地址,因为您已将p分配给&p通过此行i)。我希望它对您有用。

答案 1 :(得分:0)

这是代码的修改版本,在printfs中带有注释。请注意,我添加了第三个printf来引用int

中的p
#include<stdio.h>
int main(){
    int p=10;
    int *i=&p;
    printf("'i'  = %p is the address of the int stored in variable p\n",(void *)i);
    printf("'&i' = %p is the address of the pointer to an int called i\n",(void *)&i);
    printf("'*i' = %d is the int that is stored at the location in i which points to p\n",*i);
}

'i' = 0x7ffee4e63abc is the address of the int stored in variable p
'&i' = 0x7ffee4e63ab0 is the address of the pointer to an int called i
'*i' = 10 is the int that is stored at the location in i which points to p