Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup chapter 5, pointer to pointer to char

Reply
Thread Tools

Stroustrup chapter 5, pointer to pointer to char

 
 
arnuld
Guest
Posts: n/a
 
      03-05-2007
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*)

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 12:15 pm, "arnuld" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
adrian.hawryluk@gmail.com
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 1:15 pm, "arnuld" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-06-2007
> On Mar 5, 11:10 pm, (E-Mail Removed) 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.

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-06-2007
> On Mar 5, 11:07 pm, "mlimber" <(E-Mail Removed)> wrote:


> 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 (seehttp://www.parashift.com/c++-faq-lite/freestore-mgmt.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).



Hmmm.... a sofwtare-development issue, literally out of my head but i
do ot ask for clarification. i think this fog will clear-up when i
will start developing real-life softwares.


> > 2. pointer to function:

>
> > int (*fp) (char*);

>
> These are useful for callbacks, generic programming (e.g. with
> std::for_each), etc. See also "functors".


ok

>
>
> > 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.



thanks for the simple and easy explanation of last 2 points




> 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.


i know, i am only trying to understand them.

 
Reply With Quote
 
adrian.hawryluk@gmail.com
Guest
Posts: n/a
 
      03-06-2007
On Mar 5, 2:07 pm, "mlimber" <(E-Mail Removed)> wrote:
> On Mar 5, 12:15 pm, "arnuld" <(E-Mail Removed)> wrote:
>
> > 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 (seehttp://www.parashift.com/c++-faq-lite/freestore-mgmt.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).


Yup.

> > 2. pointer to function:

>
> > int (*fp) (char*);

>
> These are useful for callbacks, generic programming (e.g. with
> std::for_each), etc. See also "functors".


Noooooooooo! This is a function pointer. Functors are related to
objects as in:

class A {
int stateVar;
public: int operator()(int x, int y);
};

That is a functor, it allows an object to act like a function, and
since it has it's own state, it could be used in place of this model
of a function:

int A(int x, int y)
{
static int stateVar;

....
}
which has only one instance. The stateVar(s) are optional in
functors, of course.


A function pointer is like this:

void* (*myMalloc)(size_t) = malloc;

You can now use myMalloc to call malloc indirectly, you can also
change what myMalloc points at during runtime unless you state it like
this:

void* (* const myMalloc)(size_t) = malloc;

Which means that the function pointer is constant and non-changeable.

> > 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. Seehttp://www.parashift.com/c++-faq-lite/containers.html#faq-34.1.


Yup.


Adrian

 
Reply With Quote
 
adrian.hawryluk@gmail.com
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 12:33 am, "arnuld" <(E-Mail Removed)> wrote:
> > > 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 significance is for late binding. This is also known as a
callback and is used primarily in event programming. In C++ (although
hidden in the compilers internals) it is used for virtual functions,
which if you think about it, is like event programming. You have an
object and you need to tell the object that something has happened,
i.e. an event has occurred. However, at compile time, a piece of code
may not know what function to call (because it is a generic piece of
code), but it would know at runtime. qsort() is a classic example and
can be seen at http://www.cplusplus.com/reference/c...lib/qsort.html.

I've wrote a significant part of a system in C using pseudo-objects
and callbacks to write OO code. It greatly reduces copy coding.


Adrian

 
Reply With Quote
 
adrian.hawryluk@gmail.com
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 12:33 am, "arnuld" <(E-Mail Removed)> wrote:
> yep, it made sense to me. i understood these from K&R2 some time ago.


Then why ask?


Adrian

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      03-06-2007
On Tue, 06 Mar 2007 04:14:57 -0800, adrian.hawryluk wrote:

> On Mar 5, 2:07 pm, "mlimber" <(E-Mail Removed)> wrote:
>> On Mar 5, 12:15 pm, "arnuld" <(E-Mail Removed)> wrote:
>>
>> > i see the use of pointers, from K&R2 but what is the use of:


[snip]

>> > 2. pointer to function:

>>
>> > int (*fp) (char*);

>>
>> These are useful for callbacks, generic programming (e.g. with
>> std::for_each), etc. See also "functors".

>
> Noooooooooo! This is a function pointer. Functors are related to
> objects as in:
>
> class A {
> int stateVar;
> public: int operator()(int x, int y);
> };
>
> That is a functor, it allows an object to act like a function, and
> since it has it's own state, it could be used in place of this model
> of a function:


mlimber didn't say "This is a function pointer", s/he said "See also
functors" - but didn't say why! Perhaps there's a point to be made along
the lines that functors are frequently used in C++ to provide more elegant
solutions to callbacks, generic programming, etc. than can be achieved
with function pointers, or to "wrap" function pointers to hide ugly
implementation details or provide generic interfaces.

Just my tuppence worth.

[snip]

--
Lionel B
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 8:47 am, Lionel B <(E-Mail Removed)> wrote:
> On Tue, 06 Mar 2007 04:14:57 -0800, adrian.hawryluk wrote:
> > On Mar 5, 2:07 pm, "mlimber" <(E-Mail Removed)> wrote:
> >> On Mar 5, 12:15 pm, "arnuld" <(E-Mail Removed)> wrote:

>
> >> > i see the use of pointers, from K&R2 but what is the use of:

>
> >> > 2. pointer to function:

>
> >> > int (*fp) (char*);

>
> >> These are useful for callbacks, generic programming (e.g. with
> >> std::for_each), etc. See also "functors".

>
> > Noooooooooo! This is a function pointer. Functors are related to
> > objects as in:

>
> > class A {
> > int stateVar;
> > public: int operator()(int x, int y);
> > };

>
> > That is a functor, it allows an object to act like a function, and
> > since it has it's own state, it could be used in place of this model
> > of a function:

>
> mlimber didn't say "This is a function pointer", s/he said "See also
> functors" - but didn't say why! Perhaps there's a point to be made along
> the lines that functors are frequently used in C++ to provide more elegant
> solutions to callbacks, generic programming, etc. than can be achieved
> with function pointers, or to "wrap" function pointers to hide ugly
> implementation details or provide generic interfaces.
>
> Just my tuppence worth.


Precisely.

Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroustrup Chapter 4, Exercise 3 - displaying the sizes of char, int etc. arnuld C++ 0 03-05-2007 03:18 PM
Stroustrup Chapter 4, Exercise 2 arnuld C++ 0 03-05-2007 02:34 PM
Stroustrup: chapter 4 arnuld C++ 13 03-05-2007 07:41 AM
Stroustrup "desk calculator" chapter 6 arnuld C++ 14 11-11-2006 04:14 AM
Stroustrup chapter 3 - 3.6 arnuld C++ 4 11-05-2006 11:25 AM



Advertisments