简体   繁体   English

指向int和char的指针(数组)

[英]pointer to an int and a char (array)

I don't understand how pointers to chars differ from pointers to ints for example: 我不明白char的指针与int的指针有何不同:

int a = 10;
int *b = &a;
printf("%d\n", *b); // print 10
char* d = "Hello";
printf("%d\n", (int) *d); // print 72, ASCII of H

Here what is "Hello"? 这是什么“你好”? Is every character an address to an int and thus "Hello" an array of addresses? 每个字符都是整数的地址,因此“ Hello”是地址数组吗?

C is not strictly type-safe, especially the printf function which accepts anything as input (the ... syntax), how it's interpreted is handled by the format string: %d says "treat the Nth argument as a decimal integer". C并不是严格类型安全的,尤其是printf函数,它接受任何内容作为输入( ...语法),其解释方式由格式字符串处理: %d说“将第N个参数作为十进制整数进行处理”。

A char* is a different type to int* . char*int*是不同的类型。 Another way they differ is how they behave when you increment a char* compared to an int* : 它们不同的另一种方式是,与int*相比,增加char*时它们的行为:

char* c1 = "foo";
char* c2 = c1 + 1;
// c2 is c1 + 1, as sizeof(char) == 1

int* i1 = &123;
int* i2 = i1 + 1;
// i2 is i1 + 4 as sizeof(int) == 4, assuming your system uses 4-byte integers

Well if you consider they are all holding addresses ...then 好吧,如果您认为他们都是住址...那么

What is the difference? 有什么区别?

Difference is suppose I have 10 bytes of memory 区别是假设我有10个字节的内存

---+---+---+---+---+---+---+---+---+---+
|  |   |   |   |   |   |   |   |   |   |  
---+---+---+---+---+---+---+---+---+---+
1    2   3   4   5   6   7   8   9    10

Now suppose a char is of size 1 bye and int of 4 byte. 现在假设一个char的大小为1再见,并且int为4字节。

Now if someone asks you to give the integer starting from address 1 what you will do? 现在,如果有人要求您提供从地址1开始的整数,您会怎么做?

You know that size of int is 4 bytes so you get it and show it. 您知道int的大小为4个字节,因此可以获取并显示它。

Similar for char you just get 1 locations value and show it. 与char类似,您仅获得1个位置值并显示它。

Now a pointer similarly needs to know how much it should consider when it is asked to ( dereferenced)... 现在,指针同样需要知道在被要求(取消引用)时应该考虑多少……

That's where int* and char* differ. 那就是int*char*不同的地方。 Otherwise both of them hold addresse. 否则,他们两个都住址。 And using a char* you can get all byes of an integer but that is overhead on part of user. 使用char*可以获取整数的所有char* ,但这对部分用户而言是开销。

Formally.... 正式...

The pointer to type basically tells the pointer how much it should move in memory from it's current location and how much is bytes staring from it's pointing address needs to be considered when dereferrenced. 类型指针基本上告诉指针从当前位置应该在内存中移动多少,并且在取消引用时需要考虑从其指向地址开始的字节数。

Doubt-1 why don't we need &"Hello"? 怀疑1为什么我们不需要&“ Hello”?

char *d= "Hello" will place "Hello" in the read-only parts of the memory, and making da pointer to that makes any writing operation on this memory illegal. char *d= "Hello"会将char *d= "Hello"放置在内存的只读部分中,而使da指向该指针将使对该内存的任何写操作都非法。

ALSO IN C "hello" has type char[]. 在C中,“ hello”也具有char []类型。 And an array can be converted to address of the first element automatically 并且数组可以自动转换为第一个元素的地址

Firstly, your usage of char* is a bad idea since, whilst the string literal is a 'char[6]', you can't legally modify it, so you would do well to use a const char* instead. 首先,使用char*是一个坏主意,因为尽管字符串文字是'char [6]',但是您不能合法地对其进行修改,因此,最好使用const char*

That aside, d will hold the memory address that points to the character H. On most platforms, a char is 1 byte long - thus, d++ will likely increment the memory address by 1 byte. 除此之外, d将保存指向字符H的内存地址。在大多数平台上,char是1字节长-因此, d++可能会将内存地址增加1字节。

On the other hand, let's say that an int is 4 bytes. 另一方面,假设int为4个字节。 if d was an int* then d++ would increment the memory address by 4. 如果dint*d++将内存地址增加4。

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

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