[英]How to access char array using an int pointer?
Hi how to access character array using integer point.嗨,如何使用整数点访问字符数组。
char arr[10] = {'1','2','3','4','5','6','7','8','9','10'};
int *ptr;
How i can print values of 'arr' using pointer ptr?我如何使用指针 ptr 打印“arr”的值?
It is a little unclear what your goal is, but trying to print out a character array with an integer pointer is a bit like trying to get to the second step taking four-steps at a time. 尚不清楚您的目标是什么,但是尝试使用整数指针打印出字符数组有点像试图一步一步走到第二步。 When you tell the compiler that you would like to reference a memory address with an integer pointer, the compiler knows that an integer is
sizeof (int)
bytes (generally 4-bytes on x86/x86_64). 当您告诉编译器您要使用整数指针引用内存地址时,编译器就会知道整数是
sizeof (int)
个字节(在x86 / x86_64上通常为4个字节)。 So attempting to access each element in a character array with an integer pointer and normal pointer arithmetic wouldn't work. 因此,尝试使用整数指针和普通指针算术访问字符数组中的每个元素将不起作用。 (you would be advancing 4-bytes at a time).
(您一次将前进4个字节)。
However printing the character array though an integer
pointer is possible if you use the integer pointer for the starting address of the array and advance the pointer by the number of characters in the array by casting back to char
. 但是,如果将整数指针用作数组的起始地址,并通过回退到
char
来使指针按数组中的字符数前进,则可以通过integer
指针打印字符数组。 While it is doubtful this is your goal, the plain statement of your question seems to suggest it. 尽管这是您的目标值得怀疑,但您的问题的明确表述似乎表明了这一点。 To accomplish this, you could:
为此,您可以:
#include <stdio.h>
int main (void)
{
char arr[] = {'1','2','3','4','5','6','7','8','9'};
int *ptr = (int *)arr;
unsigned int i;
for (i = 0; i < sizeof arr; i++)
printf (" %c", (*(char *)ptr + i));
putchar ('\n');
return 0;
}
Output 输出量
$ ./bin/char_array_int_ptr
1 2 3 4 5 6 7 8 9
Note: your original initialization of your array with a character '10'
was invalid. 注意:最初使用字符
'10'
对数组进行的初始化无效。 If this was an assignment, it is likely intended to expose you to how pointer arithmetic is influenced by type and the ability to cast from and to type char
(without violating strict aliasing rules) 如果这是一项任务,则可能会使您了解指针算术如何受类型影响以及从
char
和从类型char
的能力(不违反严格的别名规则)
If you are just after the integer values you can print out the characters as integers 如果您只是在整数值之后,则可以将字符打印为整数
for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
{
printf( "%d ", arr[i] );
}
Using a pointer of the wrong data type to access anything is undefined behavior, thus making it not something you want to do. 使用错误的数据类型的指针访问任何内容都是未定义的行为,因此使它不是您想要执行的操作。 If you want to cast a char to an integer, you can do that.
如果要将字符转换为整数,则可以这样做。 If you want to print the integer value of a char, you can do that too.
如果要打印char的整数值,也可以这样做。
But using a pointer type integer to access a char array is undefined behavior. 但是,使用指针类型整数访问char数组是未定义的行为。
Simply assign the array name to the pointer (using some casting): 只需将数组名称分配给指针(使用一些强制转换):
int* ptr = (int*)arr;
An array variable is actually just a pointer to the array's first element, so this code is equivalent to: 数组变量实际上只是指向数组第一个元素的指针,因此此代码等效于:
int* ptr = (int*)&arr[0];
As was mentioned in this comments, you can get yourself into some nasty trouble doing this. 正如在此评论中提到的那样,这样做会使您陷入麻烦。 On most modern 32-bit systems,
int
is 4 times as wide as char
, so incrementing ptr
will jump forward 4 elements in arr
, and you'll get 4 chars interpreted as a single int
when you dereference ptr
. 在大多数现代的32位系统上,
int
宽度是char
4倍,因此递增ptr
将使arr
4个元素向前跳转,并且当取消引用ptr
时,您将获得4个char解释为单个int
。 If you're not extremely careful, this will cause segfaults and/or undefined behavior. 如果您不太谨慎,这将导致段错误和/或不确定的行为。 In fact, you'll probably get undefined behavior even if you are careful.
实际上,即使您小心一点,也可能会得到不确定的行为。
That said, this is almost certainly not you want to do. 也就是说,这几乎可以肯定不是您想要做的。 It sounds like you want to interpret the values in
arr
as integers. 听起来您想将
arr
的值解释为整数。 To do that, you first have to understand that char
s in C and C++ are really not any different from int
s (except for their memory size and the range of values they can represent). 为此,您首先必须了解C和C ++中的
char
实际上与int
并无任何区别(除了它们的内存大小和它们可以表示的值范围外)。 '0'
is equivalent to 48
(the ASCII ordinal for the character 0
). '0'
等效于48
(字符0
的ASCII序数)。 So, to interpret a single digit as an integer, do something like this: 因此,要将单个数字解释为整数,请执行以下操作:
int x = arr[i]-'0';
Where i
is whatever index in the array you want to access. i
在哪里是您要访问的数组中的任何索引。 If you want to interpret multiple-digit strings as integers, atoi
is the tool for the job: 如果要将多位数字字符串解释为整数,则
atoi
是该作业的工具:
#include <stdlib.h>
#include <stdio.h>
int main() {
char* s = "12345";
int x = atoi(s);
printf("%d\n", x); // 12345
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.