简体   繁体   English

C char数组指针混乱

[英]C char array pointer confusion

I have written following c code:我写了以下 c 代码:

#include <stdio.h>
#include <stdlib.h>

void func1(char *arr){
    printf("%d\n",arr[0]);
    printf("%d\n",arr[1]);
    return;
}

int main () {
    char a[6] = "hello";
    printf("%p\n",a);
    printf("%p\n",&a);
    func1(a);
    return 0;
}

when I executed this code I am getting following output当我执行此代码时,我正在关注 output

0x7fff5a7323e2
0x7fff5a7323e2
104
101

Following are my doubts:以下是我的疑惑:

  1. Why is value of arr[1] less than arr[0] , and what are these values?为什么arr[1]的值小于arr[0] ,这些值是什么?
  2. Suppose we are given 0 to 1073741823 is the valid memory range and we have to check if array passed to func1 is in valid range then how to check that.假设给定 0 到 1073741823 是有效的 memory 范围,我们必须检查传递给 func1 的数组是否在有效范围内,然后如何检查。

1. Why is value of arr[1] less than arr[0] , and what are these values? 1. 为什么arr[1]的值小于arr[0] ,这些值是什么?

printf("%d\n",arr[0]) and printf("%d\n",arr[1]) will print the decimal values of the characters in those positions, since your character encoding is ASCII the decimal value of e ( arr[1] ) is 101 and the decimal value h ( arr[0] ) is 104, as you can check in the linked table. printf("%d\n",arr[0])printf("%d\n",arr[1])将打印这些位置字符的十进制值,因为您的字符编码是 ASCII十进制值e ( arr[1] ) 的值为 101,十进制值h ( arr[0] ) 为 104,您可以在链接表中查看。


2. Suppose we are given 0 to 1073741823 is the valid memory range and we have to check if array passed to func1 is in valid range then how to check that. 2. 假设给定 0 到 1073741823 是有效的 memory 范围,我们必须检查传递给 func1 的数组是否在有效范围内,然后如何检查。

In this case, since it's a string, you know it will have a sentinel null byte at the end of the array, so you can use:在这种情况下,因为它是一个字符串,你知道它在数组末尾会有一个标记 null 字节,所以你可以使用:

// initially arr will be pointing to the first element of the array
while(*arr != '\0'){ // will evaluate to false when the string ends
     
    //do stuff...
    arr++;

}

There are other ways of doing it, for example, pass the size of the array as second argument:还有其他方法可以做到这一点,例如,将数组的大小作为第二个参数传递:

void func1(char *arr, int size){...}

and the call:和电话:

func1(a, sizeof a / sizeof *a);

This is more useful when your array is not a null terminated array, otherwise you can always use sentinels to signal the end of your arrays, the downside is that in most cases you'll need to add them yourself.当您的数组不是null 终止数组时,这更有用,否则您始终可以使用哨兵来指示 arrays 的结尾,缺点是在大多数情况下您需要自己添加它们。


Note that the behavior of printf("%p\n", a) is undefined, you should have printf("%p\n", (void*)a) .请注意, printf("%p\n", a)的行为未定义,您应该有printf("%p\n", (void*)a)

What you need is to understand formatted string for printf .您需要了解printf的格式化字符串。 printf("%d\n",arr[0]); expected to print an integer to the output. But in C, character can be treated as integer based on its coded value in memory. For example, 104 represents "h" according to ASCII coding.期望打印一个integer到output。但是在C中,字符可以根据其在memory中的编码值处理为integer。例如,104根据ASCII编码表示“h”。 And 101 represents "e".而101代表“e”。 The memory allocated for your array is managed by system, you not suppose to check if they are in valid range or not.为您的阵列分配的 memory 由系统管理,您不应该检查它们是否在有效范围内。 But you can always treat them as integer and use simple compare operators.但您始终可以将它们视为 integer 并使用简单的比较运算符。

For the second part, you can convert hexadecimal address to decimal and compare it with the provided range.对于第二部分,您可以将十六进制地址转换为十进制,并将其与提供的范围进行比较。

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

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