C 使用指针从方法中获取字符数组值。
我一直在努力实现以下目标: 我的目标是使用main()中的指针访问在方法()中创建的元素 我想打印缓冲区值的每个元素,就像方法()使用ptr创建的一样。有没有关于我如何做这件事的建议 我不确定我是否误解了什么,但我认为ptr指向缓冲区的地址。因此,取消引用会给我缓冲区[0]C 使用指针从方法中获取字符数组值。,c,pointers,C,Pointers,我一直在努力实现以下目标: 我的目标是使用main()中的指针访问在方法()中创建的元素 我想打印缓冲区值的每个元素,就像方法()使用ptr创建的一样。有没有关于我如何做这件事的建议 我不确定我是否误解了什么,但我认为ptr指向缓冲区的地址。因此,取消引用会给我缓冲区[0] 谢谢。这是您的代码的固定和注释版本。在评论中询问是否有smth。你不明白 #include <stdio.h> #include <stdlib.h> // takes in address of
谢谢。这是您的代码的固定和注释版本。在评论中询问是否有smth。你不明白
#include <stdio.h>
#include <stdlib.h>
// takes in address of pointer
//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose
int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);
//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);
buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:
*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;
//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)
printf("%x\n", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%p\n", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%p\n", *input); // this prints address of buffer
return 0;
}
int main(){
unsigned char *ptr;
method(&ptr);
printf("%p\n", ptr); // this prints address of buffer
//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %x\t in decimal: %d\n", i, buf_elem, buf_elem);
}
// Don't forget to free the memory. //<<< changed
free(ptr);
}
#包括
#包括
//接收指针的地址
//十六进制:0xab大于有符号字符的最大值。
//如果不指定unsigned,大多数Comiler默认为signed char。
//因此,您需要对所选的值使用unsigned
int方法(unsigned char**input){/&buffer
是指向变量的指针,而不是它所指向的位置(这将是普通的buffer
)。缓冲区的类型是char**
,而*input
的类型是char*
。此外,如果要使用printf
打印指针,您首先需要将其强制转换为void*
,然后使用%p”
格式。如果您在答案中添加更改的内容以及更改的原因,将更有帮助。
#include <stdio.h>
#include <stdlib.h>
// takes in address of pointer
//Hex: 0xab is larger than the max value of a signed char.
//Most comilers default to signed char if you don't specify unsigned.
//So you need to use unsigned for the values you chose
int method(unsigned char** input) { //<<< changed
unsigned char *buffer = malloc(sizeof(char)*10);
//Check for malloc success <<< added
if(!buffer)
exit(EXIT_FAILURE);
buffer[0] = 0x12;
buffer[1] = 0x34;
buffer[2] = 0xab;
//I recommend not to mix array notation and pointer notation on the same object.
//Alternatively, you could write:
*buffer = 0x12;
*(buffer + 1) = 0x34;
*(buffer + 2) = 0xab;
//buffer already contains the address of your "array".
//You don't want the address of that address
*input = buffer; //<<< changed (removed &)
printf("%x\n", *buffer); // this prints 0x12
//Not casting &buffer will likely work (with compiler warnings
//But it is better to conform. Either use (char *) or (void *)
//<<< added the cast for printf()
printf("%p\n", (char *)&buffer); // this prints address of buffer example: 0x7fffbd98bf78
printf("%p\n", *input); // this prints address of buffer
return 0;
}
int main(){
unsigned char *ptr;
method(&ptr);
printf("%p\n", ptr); // this prints address of buffer
//this does not seem to print out buffer[0]
for(int i = 0; i < 3; i++){
//<<< changed to obtain content of buffer via ptr for loop.
unsigned char buf_elem = *(ptr + i);
printf("buffer[%d] in hex: %x\t in decimal: %d\n", i, buf_elem, buf_elem);
}
// Don't forget to free the memory. //<<< changed
free(ptr);
}