简体   繁体   English

int * a和char * a之间的区别?

[英]Difference between int*a and char *a?

What is the difference between char *a and int *a as both work on Code Blocks for storing character pointer eg. char *aint *a之间有什么区别,因为两者都在用于存储字符指针的代码块上工作,例如。

char *c, k = '$';
int *j;

j = &k;
printf("%c\n%p\n%c\n", k, j, *j);

c = &k;
printf("%c\n%p\n%c", k, c, *c);

Activate diagnostics and don't ignore them ( -Wall -Wextra -pedantic-errors ). 激活诊断程序,不要忽略它们( -Wall -Wextra -pedantic-errors )。
The compiler should tell you that you are doing something disallowed. 编译器应告诉您您正在执行不允许的操作。

See here on coliru: http://coliru.stacked-crooked.com/a/31acb5b670254167 参见此处关于coliru的信息: http ://coliru.stacked-crooked.com/a/31acb5b670254167

main.cpp:7:7: error: incompatible pointer types assigning to 'int *' from 'char *' [-Werror,-Wincompatible-pointer-types]
    j = &k;
      ^ ~~

Answering your question, a char is an integer-type of lower rank than int (meaning potentially (and in practice nearly guaranteed) smaller size and value-range), and thus pointers to either are different types too. 回答您的问题时, char是一个比int低的整数类型(它可能(实际上在实际上可以保证)更小的大小和值范围),并且它的指针也是不同的类型。
Using a pointer of wrong type to access an object (with few exceptions) is UB. UB使用错误类型的指针访问对象(少数例外)。

Interpreting a character object as an integer 将字符对象解释为整数

printf("%c\n%p\n%c\n", k, j, *j);

or storing the address of a char into an int pointer 或将char的地址存储到int指针中

j = &k;

is undefined behavour. 是不确定的行为。

In your case you got the same result by chance. 在您的情况下,您偶然会得到相同的结果。 The code is incorrect and may as well print anything. 代码不正确,可能还会打印任何内容。

char *a

a is a pointer to something. a是指向某物的指针。 That something is a char 那是一个char

int *b

b is a pointer to something. b是指向某物的指针。 That something is an int 那是一个int

Both a and b are pointers , they only store memory addresses to other things, which is why it is possible (but definitely not reccommended; warning by default, error with -wError ) to store the address of an int in a char * . ab都是指针 ,它们仅存储指向其他对象的内存地址,这就是为什么有可能(但绝对不建议;默认情况下warning-wError error )将int的地址存储在char *
Dereferencing it is undefined behaviour and " anything could happen " which is why the warning/error is there in the first place. 取消引用是未定义的行为,并且“ 可能发生任何事情 ”,这就是为什么警告/错误首先出现的原因。

It may work with your current machine and compiler. 可能适用于您当前的机器和编译器。 It isn't guaranteed to though, and literally anything could break it. 虽然不能保证,但实际上任何事情都会破坏它。 Don't do it 不要做

In your case it will give the same value because sizeof(int)>size(char) . 在您的情况下,它将给出相同的值,因为sizeof(int)>size(char) If you really want to see the difference between char* and int* . 如果您真的想看看char*int*之间的区别。 Lets do this: 我们开工吧:

Assume: char is of 1 byte , int is 4 byte and addresses are also of 4 byte . 假设: char1 byteint4 byte ,地址也为4 byte To observe the difference between char* and int* . 观察char*int*之间的区别。

int k=1024;
char* charptr= &k;
int* intptr=&k;

printf("%02x \n\n", *charptr); // this will simply print `00`
printf("%02x \n\n", *intptr); // this will simply print `400`
int i=0;
for(; i<4 ; i++)
  printf("%02x ", *charptr++);  // this will print `00 04 00 00`

NOTE: It is a little endian machine. 注意:这是一个小端的机器。 First print displays the content of first byte only therefore we see 00 as output. 第一次打印仅显示第一个字节的内容,因此我们将00作为输出。 Third print statements clears everything. 第三个打印语句清除所有内容。

Hope this will help to understand the difference here. 希望这将有助于理解此处的区别。

What is the difference between a char pointer and an int pointer ? char指针和int指针有什么区别?

Without considering what your code, the answer is simple Char pointer points to a memory address which holds a char value and an int pointer points to one with int value. 不用考虑代码是什么,答案很简单Char指针指向一个存储有char值的内存地址,而int指针指向一个具有int值的地址。 This is and remains the difference between them. 这是并且仍然是它们之间的区别。 However, when you force the compiler to do something what they haven't been specified to, you either get an error or an unspecified behavior. 但是,当您强制编译器执行未指定的操作时,您会得到错误或未指定的行为。

So, what's up with your code ? 那么,您的代码怎么了?

That has already been explained well in answers but the basic thing is that char is stored as an ascii value and thus in your case an int pointer could point to a char. 答案中已经对此进行了很好的解释,但是最基本的是char是作为ascii值存储的,因此在您的情况下,int指针可以指向char。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM