简体   繁体   English

“ unsigned int * ptr”和“ signed int * ptr”有什么区别?

[英]what is the difference between “unsigned int *ptr” and signed int *ptr"?

I know that integer pointer will dereference 4 bytes, then what signed and unsigned do with pointers ? 我知道整数指针将取消引用4个字节,然后对指针进行有符号和无符号处理? what is the meaning of "unsigned int *ptr" and "signed int *ptr" “ unsigned int * ptr”和“ signed int * ptr”是什么意思

Type of pointer suggests what kind of variables address it can keep. 指针类型表明它可以保留什么样的变量地址。 So, unsigned int *ptr should keep address of unsigned int and signed int *ptr should keep address of signed int 因此, unsigned int *ptr应该保留unsigned int地址,而signed int *ptr应该保留signed int地址。

Please look at following piece of code, 请看下面的代码,

  int main()
  {
    unsigned int * ptr1;
    signed int * ptr2;

    unsigned int i;
    signed int s;

    ptr1 = &s;
    ptr2 = &i;

  }

It gives me following errors in Visual Studio, 它给我以下Visual Studio中的错误,

Error 1 error C2440: '=' : cannot convert from 'int *' to 'unsigned int *' [...] 错误1错误C2440:'=':无法从'int *'转换为'unsigned int *'[...]

Error 2 error C2440: '=' : cannot convert from 'unsigned int *' to 'int *' [...] 错误2错误C2440:'=':无法从'unsigned int *'转换为'int *'[...]

When you have 当你有

unsigned int* ptr;

the value of ptr is assumed to hold an unsigned int . 假定ptr的值包含一个unsigned int Similarly, when you have 同样,当你有

signed int* ptr;

the value of ptr is assumed to hold an signed int . 假定ptr的值包含一个有signed int

Here's a simple experiment: 这是一个简单的实验:

#include <stdio.h>

int main()
{
   signed int i = -10;
   signed int* p1 = &i;
   unsigned int* p2 = (unsigned int*)p1;

   printf("Value of p1: 0x%p\n", p1);
   printf("Value of p2: 0x%p\n", p2);

   printf("Value of *p1: %d\n", *p1);
   printf("Value of *p2: %u\n", *p2);
}

A sample output of that program: 该程序的示例输出:

Value of p1: 0x0x7fff1b52bd6c
Value of p2: 0x0x7fff1b52bd6c
Value of *p1: -10
Value of *p2: 4294967286

Even though the numeric value of the address held by p1 and p2 are same, when dereferenced, they evaluate to vastly different values. 即使p1p2所保存的地址的数值相同,但在取消引用时,它们的取值也大不相同。

A pointer, in C/C++, is nothing more than a memory location; 在C / C ++中,指针不过是一个内存位置。 their sizes are the same for a given architecture. 对于给定的体系结构,它们的大小是相同的。 The type of pointer such as void *, int *, unsigned int *, etc... tells the compiler how to handle pointer math. 指针的类型(例如void *,int *,unsigned int *等)告诉编译器如何处理指针数学。

Visualizing it this way always helps me... 以这种方式可视化始终对我有帮助...

char *ptrMyString = "test";
int *ptrMyINTS[] = {1,2,3,4}

The memory for each of the arrays above looks like this: 上面每个数组的内存如下所示:

Offsets        0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 
ptrMyString = [t] [e] [s] [t] [0]
ptrMyINTS   = [0] [0] [0] [1] [0] [0] [0] [2] [0] [0] [0] [3] [0] [0] [0] [4]

sizeof(ptrMyString) should be equal to sizeof(ptrMyINTS) since they are just memory pointers. sizeof(ptrMyString)应该等于sizeof(ptrMyINTS),因为它们只是内存指针。 However, ptrMyString++ will increment by 1 where ptrMYINTS will increment by 4. 但是,ptrMyString ++将增加1,而ptrMYINTS将增加4。

Now I'm over simplifying a lot of things and ignoring stuff like virtual memory but the basic idea is, for example, if both pointers start at 0x00000000 (32bit address) then the pointer's value for each offset would be: 现在,我已经简化了很多事情,并忽略了诸如虚拟内存之类的东西,但是基本思想是,例如,如果两个指针都从0x00000000(32位地址)开始,则每个偏移量的指针值将为:

ptrMyString+0 = 0x00000000 = t
ptrMyString+1 = 0x00000001 = e
ptrMyString+2 = 0x00000002 = s
ptrMyString+3 = 0x00000003 = t

ptrMyINTS+0 = 0x00000000 = 1
ptrMyINTS+1 = 0x00000004 = 2
ptrMyINTS+2 = 0x00000008 = 3
ptrMyINTS+3 = 0x0000000C = 4

Notice the size is the same for all locations but the amount we increment by, 1 and 4, comes from the type of pointers, char * and int * respectively. 注意,所有位置的大小都相同,但我们增加的数量1和4分别来自指针的类型char *和int *。 I made the assumption that an int is 4 bytes here because it is on 32 bit X86 and ARM architectures. 我假设这里的int为4字节,因为它位于32位X86和ARM体系结构上。

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

相关问题 `ptr = (int *) realloc(ptr,sizeof(int));` 和 `ptr = (int *) realloc(ptr,sizeof(int)*m);` 有什么区别? - What is the difference between `ptr = (int *) realloc(ptr,sizeof(int));` and `ptr = (int *) realloc(ptr,sizeof(int)*m);`? 有符号整数和无符号整数有什么区别 - What is the difference between signed and unsigned int C中的unsigned int和signed int有什么区别? - What is a difference between unsigned int and signed int in C? const int* ptr[] 和 const int (*ptr)[] 的区别 - Difference between const int* ptr[] and const int (*ptr)[] 将 arrays 传递给 function。 arguments int *ptr 与 int ptr[ ] 有什么区别 - Pass arrays to a function. What is the difference between arguments int *ptr vs int ptr[ ] 以下两种说法有什么区别? ptr = malloc (400); ptr = malloc (100 * sizeof(int)) - What are the difference between the following two statements? ptr = malloc (400); ptr = malloc (100 * sizeof(int)) unsigned和signed int指针之间的区别 - Difference between unsigned and signed int pointer 如果 [ "int* ptr"] ptr 是指针,"int a=5" 与 scanf("%d",ptr) 的关系是什么? - what is relation of "int a=5" with scanf("%d",ptr) if [ "int* ptr"] ptr is pointer? unsigned int和int之间的区别 - Difference between unsigned int and int short signed int和signed int之间有什么区别 - what is the difference between short signed int and signed int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM