[英]Access address of pointer in pointer-to-pointer array
4I am trying to get a pointer address from a pointer to a pointer array, however I just get garbage when I run the code below. 4我试图从指向指针数组的指针获取指针地址,但是当我运行下面的代码时,我只会得到垃圾。 How do I retrieve the pointer address of one of the char * from the char ** array?
如何从char **数组中检索char *之一的指针地址? Thanks for checking this out.
感谢您对此进行检查。
Specifically, I want to obtain the address of "wh" via "what" and assign it to "hi". 具体来说,我想通过“ what”获取“ wh”的地址并将其分配给“ hi”。
char * hi;
char * wh;
char ** what;
int main(void) {
char z[4] = "wx\0";
char a[4] = "ab\0";
hi = &z;
wh = &a;
what = (char **) malloc( 25 * sizeof(char));
what[0] = &hi;
what[1] = &wh;
printf("%s | %s\n", hi, wh);
hi = &what[1];
printf("%s | %s\n", hi, wh);
return EXIT_SUCCESS;
}
Corrected code: 更正的代码:
#include <stdio.h>
#include<stdlib.h>
char * hi;
char * wh;
char ** what;
int main(void) {
char z[3] = "wx\0";
char a[3] = "ab\0";
hi = z;
wh = a;
what = malloc(2 * sizeof *what);
what[0] = hi;
what[1] = wh;
printf("%s | %s\n", hi, wh);
hi = what[1];
printf("%s | %s\n", hi, wh);
return 0;
}
The type of hi
and wh
is not the same as of &z
and &a
, even though they should yield the same value. hi
和wh
的类型&z
和&a
,即使它们应产生相同的值。 Also you want what[1]
in hi
not the value of the memory location where it is. 另外,您不希望
hi
what[1]
是它所在的存储位置的值。 Also, in C you do not need to cast the return of malloc. 另外,在C中,您无需强制转换malloc的返回值。 However, in C++ the cast is required although its better to use the
new
operator. 但是,在C ++中,尽管更好地使用
new
运算符,但仍需要强制转换。
hi
and wh
are defined as pointer to char . hi
和wh
被定义为指向char的指针 。 So, change your assignment statements 因此,请更改您的工作分配表
from, 从,
hi = &z; /* Type of &z is pointer-to-array-of-4-char */ wh = &a; /* Type of &a is pointer-to-array-of-4-char */
to, 至,
hi = z; /* Type of z is pointer-to-char. Array name, here, decays into pointer to first element of the array */ wh = a; /* Type of a is pointer-to-char */
what
is defined as type, pointer to pointer to char . what
被定义为类型, 指针指向char。 So, change your malloc
statement 因此,更改您的
malloc
语句
from, 从,
what = (char **) malloc( 25 * sizeof(char)); /* what needs to hold char*'s, you have allocated only for char's */
to, 至,
what = malloc(25 * sizeof *what); /* what is of type char**. *what gives you char* */
Also, change your what
, hi
assignment statements 另外,更改您的
what
, hi
赋值语句
from, 从,
what[0] = &hi; /* Type of what[0] is pointer-to-char; Type of &hi is pointer-to-pointer-to-char */ what[1] = &wh; /* Type of what[1] is pointer-to-char; Type of &wh is pointer-to-pointer-to-char */ ... hi = &what[1]; /* Type of hi is pointer-to-char; Type of &what[1] is pointer-to-pointer-to-char */
to , 至 ,
what[0] = hi; /* Types are same */ what[1] = wh; /* Types are same */ ... hi = what[1]; /* Types are same */
Note: The size of what
is too high than what really required for the posted program. 注:大小
what
比真正需要的发布程序过高。 If you feel, you are going to have the same program, modify 25
to 2
in the malloc
. 如果您有相同的程序,请在
malloc
中将25
修改为2
。
char * hi;
char * wh;
char ** what;
int main(void) {
char z[4] = "wx\0";
char a[4] = "ab\0";
hi = &z;
wh = &a;
First mistake; 第一个错误; the types of the expressions
&z
and &a
are both char (*)[4]
(pointer to 4-element array of char
), not char *
. 表达式
&z
和&a
类型均为char (*)[4]
(指向char
4元素数组的指针),而不是char *
。 To fix this, drop the &
from both: 要解决此问题,请同时删除
&
:
hi = z;
wh = a;
Except when it is the operand of the sizeof
or unary &
operators, or is a string literal being used to initialize an array, an expression of type "N-element array of T
" will be converted ("decay") to an expression of type "pointer to T
", and the value of the expression is the address of the first element in the array. 除非它是
sizeof
或一元&
运算符的操作数,或者是用于初始化数组的字符串文字,否则类型“ N的T
元素数组”的表达式将被转换(“ decay”)为键入“指向T
指针”,表达式的值就是数组中第一个元素的地址。
what = (char **) malloc( 25 * sizeof(char));
Don't cast the result of malloc
; 不要转换
malloc
的结果; it isn't necessary 1 , and depending on the compiler version it can suppress a useful diagnostic. 不必要1 ,并且根据编译器版本,它可以抑制有用的诊断。 You also have a type mismatch.
您也有类型不匹配。 You want to space for 25 pointers to
char
, but you're only allocating enough for 25 plain char
s. 您想为
char
分配25个指针,但是您只分配了25个char
。 Rewrite that as 重写为
what = malloc( sizeof *what * 25 );
The type of the expression what
is char **
; 类型表达的
what
是char **
; thus, the type of the expression *what
is char *
. 因此,表达式
*what
是char *
。 So sizeof *what
will give you the same result as sizeof (char *)
. 所以
sizeof *what
的结果与sizeof (char *)
结果相同。 The above line allocates enough memory to store 25 pointers to char
, and assigns the resulting pointer to what
. 上一行分配了足够的内存来存储25个指向
char
指针,并将结果指针分配给what
。
what[0] = &hi;
what[1] = &wh;
Again, drop the &
; 同样,删除
&
; hi
and wh
are already pointers to char
: hi
和wh
已经是char
指针:
what[0] = hi;
what[1] = wh;
printf("%s | %s\n", hi, wh);
hi = &what[1];
Type mismatch; 类型不匹配;
&what[1]
has type char **
, hi
has type char *
. &what[1]
类型为char **
, hi
类型为char *
。 Again, drop the &
. 同样,删除
&
。
hi = what[1];
hi
and wh
now point to the same thing ( what[1]
== wh
and hi
== what[1]
, so hi
== wh
; that is, both hi
and wh
contain the same pointer value). hi
和wh
现在指向同一事物( what[1]
== wh
和hi
== what[1]
,所以hi
== wh
;也就是说, hi
和wh
包含相同的指针值)。
printf("%s | %s\n", hi, wh);
return EXIT_SUCCESS;
}
malloc
anyway.
malloc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.