Stroustrup第5章,指向char的指针 [英] Stroustrup chapter 5, pointer to pointer to char
问题描述
我看到使用指针,来自K& R2,但有什么用途:
1.指针指针:
char c;
char ** ppc;
2.指向函数的指针:
int(* fp )(char *);
3.返回指针的函数:
int * f(char *)
3月5日,下午12:15,arnuld < geek.arn ... @ gmail.comwrote:
i看到指针的使用,来自K& R2但有什么用的:
1.指针指针:
char c;
char ** ppc;
指向char的指针指向char,通过它,你可以修改char的
。一个指向指针的指针指向一个指针,并通过它
你可以修改指针。它最常用于动态分配二维数组(参见
http://www.parashift.com/c++-faq-lit...html#faq-16.16 )
和传递给需要修改
指针的函数的指针(例如,将一个节点添加到链接的
列表末尾的函数可能
附加后需要修改指向列表末尾的指针。
>
2 。指向函数的指针:
int(* fp)(char *);
这些对于回调,泛型编程很有用(例如使用
std :: for_each)等。另请参阅functors。
>
3.函数返回指针:
int * f(char *)
它可能会根据
输入字符串,动态分配的整数或数组返回指向全局查找表的指针整数。
注意指针是有用的,但是你应该尽可能避免使用它们。
支持危险性更小的结构。参见
http:// www .parashift.com / c ++ - faq-lit .... html#faq-34.1 。
干杯! --M
3月5日下午1点15分,arnuld < geek.arn ... @ gmail.comwrote:
i看到指针的使用,来自K& R2但有什么用的:
1.指针指针:
char c;
char ** ppc;
指向指针的方法是通过两个指针来获得最终类型的
。这通常用于2D阵列。最初在
C中,2D数组表示为指向
类型指针数组的指针。
int const X = ...;
int const Y = ...;
int x;
char ** a = (char **)malloc(sizeof(char *)* X);
for(x = 0; x< X; ++ x){
a [x] =(char *)malloc(sizeof(char)* Y);
}
现在你可以访问一个2D数组:a [i] [j]
但是,Y并不一定需要保持静态。对于
实例:
char * strings [] = {" hello",there,out,there, in,la-
la,land等。如上所述,
将被解密为char **,但每个字符串是
长度不一样。
>
C和C ++也有一个不同的数组,可以在
堆栈上或静态分配。不需要指针,
编译器在堆栈上分配一个X * Y数组,并根据x + y * Y * sizeof计算
偏移量(char )。
char strings [] [20] = {" hello"," there"," out"," there"," in"," la-
la",land };
将有7个元素,每个长度为20个字符,即使每个
字符串不是20个字符。
>
2.指向函数的指针:
int(* fp)(char *);
指向函数的指针是间接调用函数的一种方式。
在这种情况下你的fp可以赋值的函数将是一个
函数,它具有返回类型并且将char *作为其唯一的
参数。
示例:
int myFn(char * str){
返回printf("%s",str);
}
int(* fp)(char *)= myFn;
int main()
{
myFn(" hello");
fp(" hello"); //做同样的事情,因为调用相同的函数。
}
这是虚拟函数在C ++中完成的向量表
函数指针。
3.函数返回指针:
int * f(char *)
这会返回一个指向int的指针。所以也许f可能想要传递一个由调用者修改的内容,或者如果按值传递给调用者,那么它将是昂贵的时间。(在在这种情况下,
int通常与指针的大小相同,所以没有真正的区别是这里制作的
),或者f可能返回一个指向int数组的指针。
这对你有意义吗?
Adrian
3月5日,晚上11点10分,adrian.hawry ... @ gmail.com写道:
指向指针的方式是指向两个指针的方式得到
的最终类型。这通常用于2D阵列。最初在
C中,2D数组表示为指向
类型指针数组的指针。
int const X = ...;
int const Y = ...;
int x;
char ** a = (char **)malloc(sizeof(char *)* X);
for(x = 0; x< X; ++ x){
a [x] =(char *)malloc(sizeof(char)* Y);
}
现在你可以访问一个2D数组了: a [i] [j]
然而,Y并不一定需要保持静态大小。对于
实例:
char * strings [] = {" hello",there,out,there, in,la-
la,land等。如上所述,
将被解密为char **,但每个字符串是
长度不一样。
>
C和C ++也有一个不同的数组,可以在
堆栈上或静态分配。不需要指针,
编译器在堆栈上分配一个X * Y数组,并根据x + y * Y * sizeof计算
偏移量(char )。
char strings [] [20] = {" hello"," there"," out"," there"," in"," la-
la",land };
将有7个元素,每个长度为20个字符,即使每个
字符串不是20个字符。
。我猜,原因是,当他开发一些软件时,他们只会理解他们。
对吗?
< blockquote class =post_quotes>
2.指向函数的指针:
int(* fp) (字符*);
指向函数的指针是一种间接调用函数的方法。
这是唯一的原因吗?
间接通话,它有什么意义?
在这种情况下你的fp可以赋值的函数是一个
函数,它有一个返回类型并且只接受一个char *
参数。
3.函数返回一个指针:
int * f(char *)
这将返回指向int的指针。所以也许f可能想要传递一个由调用者修改的内容,或者如果按值传递给调用者,那么它将是昂贵的时间。(在在这种情况下,
int通常与指针的大小相同,所以没有真正的区别是这里制作的
),或者f可能返回一个指向int数组的指针。
这对你有意义吗?
是的,这对我来说很有意义。我不久前从K& R2了解这些。
i see the use of pointers, from K&R2 but what is the use of:
1. "pointer to pointer":
char c;
char** ppc;
2. pointer to function:
int (*fp) (char*);
3. function returning a pointer:
int* f(char*)
On Mar 5, 12:15 pm, "arnuld" <geek.arn...@gmail.comwrote:i see the use of pointers, from K&R2 but what is the use of:
1. "pointer to pointer":
char c;
char** ppc;A pointer to a char points to a char, and through it, you can modify
the char. A pointer to a pointer points to a pointer, and through it
you can modify the pointer. It is most often used for dynamically
allocated two-dimensional arrays (see
http://www.parashift.com/c++-faq-lit...html#faq-16.16)
and pointers that are passed to functions that need to modify the
pointer (e.g., a function that adds a node to the end of the linked
list may need to modify the pointer to the end of the list after
appending).
>
2. pointer to function:
int (*fp) (char*);These are useful for callbacks, generic programming (e.g. with
std::for_each), etc. See also "functors".
>
3. function returning a pointer:
int* f(char*)It might be returning a pointer into a global lookup table based on an
input string, a dynamically allocated integer, or array of integers.
Note that pointers are useful, but you should avoid them when possible
in favor of less dangerous constructs. See
http://www.parashift.com/c++-faq-lit....html#faq-34.1.
Cheers! --M
On Mar 5, 1:15 pm, "arnuld" <geek.arn...@gmail.comwrote:i see the use of pointers, from K&R2 but what is the use of:
1. "pointer to pointer":
char c;
char** ppc;Pointer to pointer is a way of going through two pointers to get to
the final type. This is most often used in 2D arrays. Originally in
C, 2D arrays are represented as a pointer to an array of pointers to a
type.
int const X = ...;
int const Y = ...;
int x;
char ** a = (char**)malloc(sizeof(char*)*X);
for(x=0; x<X; ++x) {
a[x] = (char*)malloc(sizeof(char)*Y);
}
Now you can access a as a 2D array: a[i][j]
However, Y doesn''t necessarly need to be static in size. For
instance:
char * strings[] = { "hello", "there", "out", "there", "in", "la-
la", "land" };
will be deciphered as a char** as described above, but each string is
not the same length.
C and C++ has also a different array that can be allocated on the
stack or staticly. No going though pointers are required, the
compiler allocates an X*Y array on the stack and will calculate the
offset based on x+y*Y*sizeof(char).
char strings[][20] = { "hello", "there", "out", "there", "in", "la-
la", "land" };
will be have 7 elements each 20 chars in length, even though each
string is not 20 chars.
>
2. pointer to function:
int (*fp) (char*);A pointer to a function is a way of indirectly calling a function.
The function that your fp can be assigned to in this case would be a
function that has an in return type and takes a char* as its only
parameter.
Example:
int myFn(char* str) {
return printf("%s", str);
}
int (*fp)(char*) = myFn;
int main()
{
myFn("hello");
fp("hello"); // does the same thing because calls the same function.
}
This is how virtual functions are done in C++ a vector table of
function pointers.
3. function returning a pointer:
int* f(char*)This returns a pointer to an int. So perhaps f may want to pass
something that is to be modified by the caller, or it would be
expensive time-wise if passed by value to the caller (in this case,
int is usually the same size as a pointer so no real difference is
made here), or perhaps f is returning a pointer to an array of ints.
Does this make sense to you?
Adrian
On Mar 5, 11:10 pm, adrian.hawry...@gmail.com wrote:
Pointer to pointer is a way of going through two pointers to get to
the final type. This is most often used in 2D arrays. Originally in
C, 2D arrays are represented as a pointer to an array of pointers to a
type.
int const X = ...;
int const Y = ...;
int x;
char ** a = (char**)malloc(sizeof(char*)*X);
for(x=0; x<X; ++x) {
a[x] = (char*)malloc(sizeof(char)*Y);
}
Now you can access a as a 2D array: a[i][j]
However, Y doesn''t necessarly need to be static in size. For
instance:
char * strings[] = { "hello", "there", "out", "there", "in", "la-
la", "land" };
will be deciphered as a char** as described above, but each string is
not the same length.
C and C++ has also a different array that can be allocated on the
stack or staticly. No going though pointers are required, the
compiler allocates an X*Y array on the stack and will calculate the
offset based on x+y*Y*sizeof(char).
char strings[][20] = { "hello", "there", "out", "there", "in", "la-
la", "land" };
will be have 7 elements each 20 chars in length, even though each
string is not 20 chars.
out of my head. i guess, the reason is, onw ill understand them only
when he will develop some softwares.
right ?
2. pointer to function:
int (*fp) (char*);
A pointer to a function is a way of indirectly calling a function.
Is that the only reason ?
indirect call, what is its significance ?
The function that your fp can be assigned to in this case would be a
function that has an in return type and takes a char* as its only
parameter.
3. function returning a pointer:
int* f(char*)
This returns a pointer to an int. So perhaps f may want to pass
something that is to be modified by the caller, or it would be
expensive time-wise if passed by value to the caller (in this case,
int is usually the same size as a pointer so no real difference is
made here), or perhaps f is returning a pointer to an array of ints.
Does this make sense to you?yep, it made sense to me. i understood these from K&R2 some time ago.
这篇关于Stroustrup第5章,指向char的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!