473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Pointers to pointers

What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman

Feb 18 '07 #1
8 1682
In article <11**********************@s48g2000cws.googlegroups .com>,
cman <ti****@gmail.comwrote:
>What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman
If you want to modify an object passed in the argument list of a
function from within the function, you must pass a pointer to the object
as the argument. So, if you want to modify a pointer, you should pass a
pointer to the pointer as the function argument.

Feb 18 '07 #2
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?
This is just a special case of "What do pointers to (Things)
accomplish?" The answer is that they provide a way to access
(Things) without needing to know their names -- indeed, without
them even needing to have names at all. Sometimes the (Things)
are ints, sometimes they are doubles, sometimes they are structs.
And sometimes, sometimes, you may want "anonymous" access to a
(Thing) that is itself a pointer.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 18 '07 #3
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?
Smells like homework. What are your thoughts on the matter? What have
you found about pointers to pointers in your text?


Brian
Feb 18 '07 #4
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman
This sounds like a homework question :-). But I'll help you by giving hints.

Can you write a function which takes two dimensional char arrays and prints
it contents ? How will you pass two dimensional array to this function ?

Can you study how passing command line input to a program works in C ?
something like

int main(int argc, char **argv)

On a advanced topic, can you write a function which inserts a node at the
head of link list ? How will you pass head pointer to this function ?

Cheers,
Tejas Kokje
Feb 18 '07 #5
cman skrev:
What do pointers to pointers accomplish?
Nothing really.
How does having a pointer to
a pointer help a design or aglorithm?
Well, it doesn't.
Where are they normally used?
Since C lacks call by reference, when you want a function to modify a
pointer you have to pass it a pointer to the pointer.
August
Feb 18 '07 #6
Tejas Kokje <bi*************@gmail.comwrites:
cman wrote:
>What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

cman

This sounds like a homework question :-). But I'll help you by giving hints.

Can you write a function which takes two dimensional char arrays and prints
it contents ?
[...]

Not really. A two dimensional array is an array of arrays. C doesn't
provide a good way of passing such a beast to a function, even using
pointers, unless the second dimension is fixed. You can pass a
pointer to the first element and do the indexing calculations yourself
(though it could be argued that that invokes undefined behavior).

What you can do is create something that *acts like* a two-dimensional
array. One obvious way to do this is to create an array of pointers,
where each pointer points to the first element of a row of the
"array". This is actually more flexible than a two-dimensional array,
in that it can be "ragged" (not all the rows have to be of the same
length) -- but it's more difficult because you have to manage the
memory allocation for each row *and* for the array of pointers. The
argv parameter to main() is an example of this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 18 '07 #7
Tejas Kokje wrote:
cman wrote:
>What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?

This sounds like a homework question :-). But I'll help you by giving
hints.
.... snip ...
>
On a advanced topic, can you write a function which inserts a node at
the head of link list ? How will you pass head pointer to this
function ?
I will avoid the confusion of double stars with:

link *inserthead(link* root, whatever data);

and the usage is:

root = inserthead(root, data);

and the code for inserthead probably looks like:

link *inserthead(link *root, whatever data);

link *tmp;

if (tmp = malloc(sizeof *tmp) {
tmp->next = root;
/* the following may require further mallocation */
/* depending on the definition of link. Also */
/* other fields in *tmp may need initialization */
tmp->data = data;
root = tmp;
else {
/* optional - announce lack of memory */
}
return root;
}

If you use NULL to signal error, then you want to treat inserthead
in the same manner as realloc, by receiving its return value in a
temporary, and copying that to root only if it is non-NULL.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 19 '07 #8
On Feb 18, 3:56 pm, CBFalconer <cbfalco...@yahoo.comwrote:
Tejas Kokje wrote:
cman wrote:
What do pointers to pointers accomplish? How does having a pointer to
a pointer help a design or aglorithm? Where are they normally used?
This sounds like a homework question :-). But I'll help you by giving
hints.

... snip ...
On a advanced topic, can you write a function which inserts a node at
the head of link list ? How will you pass head pointer to this
function ?

I will avoid the confusion of double stars with:

link *inserthead(link* root, whatever data);

and the usage is:

root = inserthead(root, data);

and the code for inserthead probably looks like:

link *inserthead(link *root, whatever data);

link *tmp;

if (tmp = malloc(sizeof *tmp) {
tmp->next = root;
/* the following may require further mallocation */
/* depending on the definition of link. Also */
/* other fields in *tmp may need initialization */
tmp->data = data;
root = tmp;
else {
/* optional - announce lack of memory */
}
return root;
}

If you use NULL to signal error, then you want to treat inserthead
in the same manner as realloc, by receiving its return value in a
temporary, and copying that to root only if it is non-NULL.
Point noted. But I was not asking OP to find out ways to avoid using
double pointers. In fact, OP was interested in learning about where
pointers to pointers can be useful and I as just suggesting him a
case.

Tejas Kokje

Feb 19 '07 #9

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.