473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stroustrup chapter 5, pointer to pointer to char

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*)

Mar 5 '07 #1
13 3128
On Mar 5, 12:15 pm, "arnuld" <geek.arn...@gm ail.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

Mar 5 '07 #2
On Mar 5, 1:15 pm, "arnuld" <geek.arn...@gm ail.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(s izeof(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(ch ar).

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

Mar 5 '07 #3
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(s izeof(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(ch ar).

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.

Mar 6 '07 #4
On Mar 5, 11:07 pm, "mlimber" <mlim...@gmail. comwrote:

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.c om/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.

Mar 6 '07 #5
On Mar 5, 2:07 pm, "mlimber" <mlim...@gmail. comwrote:
On Mar 5, 12:15 pm, "arnuld" <geek.arn...@gm ail.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 (seehttp://www.parashift.c om/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)(siz e_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.c om/c++-faq-lite/containers.html #faq-34.1.
Yup.
Adrian

Mar 6 '07 #6
On Mar 6, 12:33 am, "arnuld" <geek.arn...@gm ail.comwrote:
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

Mar 6 '07 #7
On Mar 6, 12:33 am, "arnuld" <geek.arn...@gm ail.comwrote:
yep, it made sense to me. i understood these from K&R2 some time ago.
Then why ask?
Adrian

Mar 6 '07 #8
On Tue, 06 Mar 2007 04:14:57 -0800, adrian.hawryluk wrote:
On Mar 5, 2:07 pm, "mlimber" <mlim...@gmail. comwrote:
>On Mar 5, 12:15 pm, "arnuld" <geek.arn...@gm ail.comwrote:
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
Mar 6 '07 #9
On Mar 6, 8:47 am, Lionel B <m...@privacy.n etwrote:
On Tue, 06 Mar 2007 04:14:57 -0800, adrian.hawryluk wrote:
On Mar 5, 2:07 pm, "mlimber" <mlim...@gmail. comwrote:
On Mar 5, 12:15 pm, "arnuld" <geek.arn...@gm ail.comwrote:
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

Mar 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
3137
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
14
2308
by: arnuld | last post by:
i have 2 problems: 1.) in section 4.2 he uses: bool is_open(File*) i want to know why he uses the pointer, instead of these 2: bool is_open(File) or bool is_open(File&)
0
1749
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 // printing the sizes of different types
10
1803
by: arnuld | last post by:
i have created a programme that prints the elements of an array. programme is compiled without ant trouble but i am getting some strange outputs: ------------------- PROGRAMME -------------------------- #include <iostream> void print_array(char ar) { std::cout << "printing elements:";
13
1918
by: arnuld | last post by:
at the very beginning of the chapter, i see some statements i am unable to understand. i know the "Pointer" takes the address of a variable, useful if, in case, we want to manipulate that variable as each Function gets its private copy of arguments: char c; char** ppc; // what is the use of "pointer to pointer" int* a; // why it is necessary to have "an array if pointers"
12
1757
by: arnuld | last post by:
------ PROGRAMME ----------- /* Stroustrup, 5.9, exercises to write some declarations (with initializers) comments explain what we intend to do */ #include<iostream>
16
2361
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define the types: unsigned char
14
2464
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT: Read a sequence of words from the input. use "quit" as the word
1
3517
by: blangela | last post by:
Bjarne Stroustrup has a new text coming out called "Programming: Principles and Practice Using C++" (ISBN: 0321543726), due to be published in December of this year. Some of the features of this new text include: *Its history: The text was developed in the author's introductory programming course at Texas A&M and has been used successfully by hundreds of students.
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.