pointer question

B

Bill Cunningham

I want to take a value stored in an int use a pointer to move it to
another int variable using a int **. Would this be the right or a usable
technique? I know how of course to use a pointer to an int to move a value
to an empty int and have sharing memory with pointers done but I want to use
a int **. And move the value /from/ one int to another.

int main(){
int a,b;
a=32;
int *pi;
pi=&a;
b=*pi;
}

The above is simple but I've never used pointers to pointers.

Bill
 
I

Ian Collins

Bill said:
I want to take a value stored in an int use a pointer to move it to
another int variable using a int **. Would this be the right or a usable
technique? I know how of course to use a pointer to an int to move a value
to an empty int and have sharing memory with pointers done but I want to use
a int **. And move the value /from/ one int to another.

int main(){
int a,b;
a=32;
int *pi;
pi=&a;
b=*pi;
}

The above is simple but I've never used pointers to pointers.

Well you still haven't....
 
B

Bill Cunningham

Ian said:
Well you still haven't....

Are you implying that there is error in my code above or that I'm not
using pointers to pointers. I know that that's what I'm inquiring about.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Are you implying that there is error in my code above or that I'm not
using pointers to pointers. I know that that's what I'm inquiring about.

You're not using pointers to pointers. You mentioned int** in your
question, but you don't use it in your code.
 
B

Barry Schwarz

I want to take a value stored in an int use a pointer to move it to
another int variable using a int **. Would this be the right or a usable

Given your abysmal record of confusion about the C language, why would
you want to perform such a simple operation is such an obfuscated
manner?
technique? I know how of course to use a pointer to an int to move a value
to an empty int and have sharing memory with pointers done but I want to use

What does the phrase "have sharing memory with pointers done" above
mean?
a int **. And move the value /from/ one int to another.

Do you want to move the value or copy it? If you really meant move,
what fills the vacuum in the source int after the value is moved to
the target int?
int main(){
int a,b;
a=32;
int *pi;
pi=&a;
b=*pi;
}

The above is simple but I've never used pointers to pointers.

Since you stated in the previous paragraph you want to use int**,
where in this code did you define an object or value of that type?
 
B

Bill Cunningham

pete said:
Bill said:
Ian said:
Well you still haven't....
[snip]

I'm not using pointers to pointers.

/* BEGIN new.c */

#include <stdio.h>

int
main(void)
{
int *p[2];
int **ppi = p;
int a = 32;
int b = 0;

printf("a is %d\n", a);
printf("b is %d\n", b);
putchar('\n');
p[0] = &a;
p[1] = &b;
**(ppi + 1) = **ppi;
printf("a is %d\n", a);
printf("b is %d\n", b);
return 0;
}

/* END new.c */

Thanks Pete for your example. I will study it and use it. I have been
reading about linked lists lately and I want to get down pointers to
pointers before trying something so complicated. For me anyway.

Bill
 
S

Shao Miller

I want to take a value stored in an int use a pointer to move it to
another int variable using a int **. Would this be the right or a usable
technique? I know how of course to use a pointer to an int to move a value
to an empty int and have sharing memory with pointers done but I want to use
a int **. And move the value /from/ one int to another.

int main(){
int a,b;
a=32;
int *pi;
pi=&a;
int **ppi;
ppi=&pi;
b=**ppi;
return 0;
}

The above is simple but I've never used pointers to pointers.

Enjoy.
 
B

Bill Cunningham

pete said:
You're welcome.


If from your posts
I've gotten the correct impression of what klonopin is,
then I think that (pointers to pointers)
is going to be very difficult for you.

Yes it is. I've been working on it for awhile now.

Bill
 
B

Barry Schwarz

On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"

snip
Thanks Pete for your example. I will study it and use it. I have been
reading about linked lists lately and I want to get down pointers to
pointers before trying something so complicated. For me anyway.

Why do you think you need pointers to pointers to use linked lists?

A linked list consists of a string of nodes which are usually
implemented as structures. Each structure will contain at least one
pointer which points to the next structure in the list.

Where would an int** fit in this design?
 
S

Shao Miller

On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"

snip


Why do you think you need pointers to pointers to use linked lists?

A linked list consists of a string of nodes which are usually
implemented as structures. Each structure will contain at least one
pointer which points to the next structure in the list.

Where would an int** fit in this design?

Another chap and I were discussing tracking list heads and a null
sentinel value versus list tails and having a circular list.

My code for circular: http://ideone.com/aNGBA
His code for null sentinel: http://ideone.com/nVSdt

(His code has a typo, somewhere, but I can't remember quite where.)

Anyway, he made extensive use of multiple levels of indirection, as is
evident.
 
B

Bill Cunningham

Barry said:
On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"

snip


Why do you think you need pointers to pointers to use linked lists?

A linked list consists of a string of nodes which are usually
implemented as structures. Each structure will contain at least one
pointer which points to the next structure in the list.

Where would an int** fit in this design?

the int ** was just being used in an example that was not a linked list
example. After a struct describing a node has been written some kind of
"push" function has to push nodes into the chain of nodes. That's where my
tutorial mentions pointer to pointers.

Bill
 
B

Bill Cunningham

Barry said:
On Sun, 17 Mar 2013 17:00:51 -0400, "Bill Cunningham"

snip


Why do you think you need pointers to pointers to use linked lists?

A linked list consists of a string of nodes which are usually
implemented as structures. Each structure will contain at least one
pointer which points to the next structure in the list.

Where would an int** fit in this design?

The .pdf on this page is what I'm going by. It seems to be the simplest.
http://cslibrary.stanford.edu/103/
 
B

Barry Schwarz

The .pdf on this page is what I'm going by. It seems to be the simplest.
http://cslibrary.stanford.edu/103/

The tutorial loses credibility as soon as it states that a pointer
stores a reference, especially since the term has a specific meaning
in C++ which the tutorial claims to be relevant for.

While that tutorial does describe how a function can alter a variable
in the calling function by receiving a pointer to that variable, the
tutorial's claim that this is the traditional way to write a push
function seems suspect. Instead of having the push function return
void, the tradition I'm familiar with would have it return a pointer
to the head of the list and eliminate the need for a pointer to
pointer. This eliminates both the need for a pointer to pointer and
the unnecessary indirection within the push function at the cost of
using the push function in an assignment statement. In other words,
instead of calling push with
push(&head, data);
I would call it with
head = push(head,data);
This provides the additional flexibility of storing the return value
in a pointer different than the one passed as an argument.
 
K

Keith Thompson

Barry Schwarz said:
On Tue, 19 Mar 2013 13:32:41 -0400, "Bill Cunningham"


The tutorial loses credibility as soon as it states that a pointer
stores a reference, especially since the term has a specific meaning
in C++ which the tutorial claims to be relevant for.
[...]

I haven't looked at the tutorial, but N1570 6.2.5p20 says:

A pointer type describes an object whose value provides a
reference to an entity of the referenced type.
 
B

Bill Cunningham

pete said:
Using a return value makes the most sense to me too.

The only time that I use a pointer parameter
to achieve a side effect in the calling function,
is when the return value is already being used for something else.

The code in the tutorial
never checks the return value of an allocation,
for a null pointer.

Does anyone have any other suggestions for a good C tutorial on this. I
have invested so much time in C if I can't eventually grasp it the thing to
look at would be C++. But this isn't a C issue I don't believe so much as a
algorithm issue in C.

Bill
 
B

Bill Cunningham

Shao said:
A tutorial on linked lists or on multiple levels of indirection with
pointers? I agree with you if you are saying that linked lists can be
implemented with C, but are not intrinsic to C.

That's pretty much what I'm saying.

Bill
 
B

Barry Schwarz

On Wed, 20 Mar 2013 16:01:11 -0400, "Bill Cunningham"

snip
Does anyone have any other suggestions for a good C tutorial on this. I
have invested so much time in C if I can't eventually grasp it the thing to
look at would be C++. But this isn't a C issue I don't believe so much as a
algorithm issue in C.

If you are having trouble with the C concepts, C++ will not make
things any easier.
 
Ö

Öö Tiib

Does anyone have any other suggestions for a good C tutorial on this. I
have invested so much time in C if I can't eventually grasp it the thing to
look at would be C++. But this isn't a C issue I don't believe so much as a
algorithm issue in C.

C++ is often considered "monstrously complex" by fans of other languages
because it adds a lot of features to C. With "a lot" I mean *A LOT*.
Classes, virtual functions, operator overloading, multiple
inheritance, exceptions, templates, lambda functions etc. C++ standard
library is also quite large so it takes some time to learn to use it.

Most C programs actually compile on C++ compilers with very little changes
(if any) so most of the complexity of C is legal in C++.

You may want to consider some scripting language like Python, that lot
of people say is easy to use.
 
B

blmblm

[ snip ]
Does anyone have any other suggestions for a good C tutorial on this. I
have invested so much time in C if I can't eventually grasp it the thing to
look at would be C++. But this isn't a C issue I don't believe so much as a
algorithm issue in C.

I agree with the others who have said that if you're having trouble
with C it's unlikely that moving to C++ will help -- it's a much
bigger and more complicated language, with pretty much all the warts
of C, so -- how do you think it would be better?

I'm inclined to agree, though, that the problems you're having are
not necessarily language-specific -- C is probably one of the more
difficult languages to start with, but based on your history of
posts here I'm inclined to think that the problems are not so much
with the specifics of C as with, oh, "algorithmic thinking" maybe.
Possibly it's a result of the medication you're taking. Whether
switching to a different language would help -- I guess I'm not
optimistic, but I'd be glad to be proved wrong.

One thing I'm curious about, though:

A while back you posted to comp.lang.fortran asking about
introductory and/or tutorial material. I'd be interested in hearing
about how/whether you followed up on that. ?
 
B

Bill Cunningham

(e-mail address removed) wrote:

[snip]
A while back you posted to comp.lang.fortran asking about
introductory and/or tutorial material. I'd be interested in hearing
about how/whether you followed up on that. ?

I figured I've come this far so move on. And I think you are right. If I
knew fortran I still wouldn't know much to *do* with it or how to do
anything with it. I know what I'd like to do but lack the knowledge. I
mentioned C++ because it is much higher level mainly.

Bill
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top