Stroustrup 5.1 Pointers

A

arnuld

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[15]; // why it is necessary to have "an array if pointers"
instead of array of "int"

int (*fp) (char*); // we can call a function by using its name, then
why "pointer to a function" here.

int* f(char*); // i am unable to comprehend this.

OR

at this newbie "point", i dont need to understand this stuff. In one
of the "comp.lang.c++" archives, i read that these things become
easier and problems like this disappear as soon as one starts building
real life softwares or one does some real-life coding using C++.
 
B

Bo Persson

arnuld wrote:
:: 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"

This is useful (not often :) if your variable is itself a pointer, and you
still want to manipulate it.

::
:: int* a[15]; // why it is necessary to have "an array if pointers"
:: instead of array of "int"

It could be useful. Not very often though.

::
:: int (*fp) (char*); // we can call a function by using its name,
:: then why "pointer to a function" here.

If you have a pointer to a function, you can assign it the address of a
similar function. That lets you change what function you call.

Not used very often either. :)

::
:: int* f(char*); // i am unable to comprehend this.

You have two pointers here, one is a parameter to the function f, the other
is the return value of f.

This is different from int (*fp)(char*), where fp is a pointer, and int is
the return type. Here 'int*' is what the (non-pointer) function f returns.

Don't bother to much. :)

::
:: OR
::
:: at this newbie "point", i dont need to understand this stuff. In one
:: of the "comp.lang.c++" archives, i read that these things become
:: easier and problems like this disappear as soon as one starts
:: building real life softwares or one does some real-life coding using
:: C++.

You probably don't have to know all the details. Using or manipulating raw
pointers is not very common in C++ code. Most of that can be hidden inside
the standard containers.


Bo Persson
 
A

Alf P. Steinbach

* arnuld:
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[15]; // why it is necessary to have "an array if pointers"
instead of array of "int"

int (*fp) (char*); // we can call a function by using its name, then
why "pointer to a function" here.

int* f(char*); // i am unable to comprehend this.

Except the first declaration, these are all examples of pointers.

Asking what they're for is meaningless, just as it's meaningless to ask
what the purpose of 'c' above is.

Perhaps the text has some example code or textual examples that refer to
the above declarations, or perhaps they're just examples.
 
A

Anand Hariharan

at this newbie "point", i dont need to understand this stuff. In one
of the "comp.lang.c++" archives, i read that these things become
easier and problems like this disappear as soon as one starts building
real life softwares or one does some real-life coding using C++.

IMHO, TC++PL serves MUCH better as a reference than as a tutorial.
Depending upon your background, consider either of Glassborow's books
or perhaps Koening & Moo's Accelerated C++.
 
M

mlimber

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[15]; // why it is necessary to have "an array if pointers"
instead of array of "int"

int (*fp) (char*); // we can call a function by using its name, then
why "pointer to a function" here.

int* f(char*); // i am unable to comprehend this.

OR

at this newbie "point", i dont need to understand this stuff. In one
of the "comp.lang.c++" archives, i read that these things become
easier and problems like this disappear as soon as one starts building
real life softwares or one does some real-life coding using C++.

Pointers to pointers (and arrays of pointers) are useful for
multidimensional arrays and for passing pointers "by reference" (so to
speak). For instance, if you had a table of strings, each string
(i.e., a row in your table) would be made up of an array of characters
(i.e., the columns in your table). Now, the strings may not have the
same length, and so it may be too inefficient to require that each row
have the same number of columns. Consider:

char t1[ 10 ][ 10 ]; // Ten strings with up to 9 chars each + null
char *t2[ 10 ]; // Ten strings with a variable, dynamically allocated
// number of characters each
char **t3; // A variable, dynamically allocated number of strings,
each
// with a variable, dynamically allocated number of chars

for( int i=0; i < 10; ++i )
{
t2 = new char[ 10 * (i+1) ]; // different length for each
// Somehow copy some text in there, perhaps from a file
}

// Somehow figure out how many strings you need, perhaps from a file
const int nStrings = 55;
t3 = new char*[ nStrings ]; // 55 rows
for( int i=0; i < nStrings; ++i )
{
t3[ i ] = new char[ 10 * (i+1) ]; // different length for each
// Somehow copy some text in there, perhaps from a file
}

// Use the string tables somehow
// Don't forget to call delete[] somewhere!

Of course, better than this is usually to use std::string and
std::vector, which manage all the memory details for you
automatically. See this FAQ:

http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1

As for passing pointers by reference, you can also use references (cf.
http://www.parashift.com/c++-faq-lite/references.html#faq-8.1):

void Foo( char& c, char*& r, char** p )
{
static const char str = "Hello, World!";
c = str[ 0 ];
r = str; // = &str[ 0 ]
*p = &str[ 7 ];
}

int main()
{
char c = 0;
char *ptr1 = &c;
char *ptr2 = &c;
Foo( c, ptr1, &ptr2 );
cout << c << ' ' << ptr1 << ' ' << ptr2 << '\n';
return 0;
}

Here, Foo takes a reference to a single character, a reference to a
pointer to a character, and a pointer to a pointer to a character. Foo
reassigns them to all or part of its own private string. The reference
and the pointer perform essentially the same function here, but their
syntax is slightly different (n.b., pointers and references are
similar in some respects but not equivalent!). The result is:

H
Hello, World!
World!

Cheers! --M
 
M

mlimber

IMHO, TC++PL serves MUCH better as a reference than as a tutorial.
Depending upon your background, consider either of Glassborow's books
or perhaps Koening & Moo's Accelerated C++.

I agree, but you missed the earlier discussion of the OP's restricted
options.

Cheers! --M
 
J

Jim Langston

arnuld said:
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"

Used for dynamically allocated 2 dimentional arrays, or as a parameter to a
function that wants to change where the variable is pointing to.
int* a[15]; // why it is necessary to have "an array if pointers"
instead of array of "int"

This could be a 2 dimentational array with the 2nd element dynamically
allocated.
int (*fp) (char*); // we can call a function by using its name, then
why "pointer to a function" here.

In C and sometimes C++ it is sometimes useful to call a function through a
pointer. Is a type of rudimentary polymorphism. Also used as parameters
for callback functions.
int* f(char*); // i am unable to comprehend this.

This is declaring a function f that returns a pointer to an int and takes a
char pointer as a parameter.
 
A

arnuld

On Mar 26, 11:57 am, "Anand Hariharan"



I agree, but you missed the earlier discussion of the OP's restricted
options.

Cheers! --M

hey M, you remember me....

nice to hear this :)
 
M

mlimber

i meant why drop "const"? we are not modifying the array, so it needs
to be "const"

Right, but it won't compile with the const in there (you can't assign
a non-const reference or pointer to a const object) and I was trying
to keep it simple. Const-correctness (see the FAQ) will come later.
Needless to say, I didn't actually test that code. :)

Cheers! --M
 
D

dragoncoder

Right, but it won't compile with the const in there (you can't assign
a non-const reference or pointer to a const object) and I was trying
to keep it simple. Const-correctness (see the FAQ) will come later.
Needless to say, I didn't actually test that code. :)

Cheers! --M
But the type of "Hello World!" is still const char* or const char[] so
it should not be a problem, right ?
 
M

mlimber

Right, but it won't compile with the const in there (you can't assign
a non-const reference or pointer to a const object) and I was trying
to keep it simple. Const-correctness (see the FAQ) will come later.
Needless to say, I didn't actually test that code. :)
Cheers! --M

But the type of "Hello World!" is still const char* or const char[] so
it should not be a problem, right ?

No. Try compiling this:

const char str[] = "Hello, World!";
char& c = str[0];

By popular demand (er, confusion), here's a compilable program (still
haven't run it):

#include <iostream>
using namespace std;

void Foo( char& c, char*& r, char** p )
{
static char str[] = "Hello, World!";
c = str[ 0 ];
r = str; // = &str[ 0 ]
*p = &str[ 7 ];
}

int main()
{
char c = 0;
char *ptr1 = &c;
char *ptr2 = &c;
Foo( c, ptr1, &ptr2 );
cout << c << ' ' << ptr1 << ' ' << ptr2 << '\n';
return 0;
}

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top