473,394 Members | 1,750 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,394 software developers and data experts.

A pointer to pointer (**p) question

34
If I have a pointer say int **pa that stores the address to another pointer int *pb, pa would be storing the address of pb which contains an address to lets say int c = 10. Picking an arbitrary address for pb to be 5009 and c to be 2331. Can someone verify for me if I have the following values correct:

pa = 5009
pb = 2331
*pb = 10

would **pa = 2331? If so, how can I obtain the value of c through pa?

I'm trying to write a function that deletes a record from a linked list by name:
struct record
{
char name[25];
char address[80];
int yearofbirth;
char telno[15];
struct record *next;
};

From what I read in my textbook and online from the net...the list will start with a struct record pointer that stores the address of the first record. So when I start my delete function, I would pass the address of that pointer into the function in order to alter the next field should the first record is the one that needed to be deleted. Therefore the param should be struct record **start. The value in start would be the address to the pointer that stores the address of the first record. Then the value in the address of the record pointer can be obtain through **start. How can I obtain the value whose address is stored in record pointer through start?

Oh well, not so quick question...lol.

EDIT: One way I thought of was to declare another struct record pointer and define it to be **start (value, address to first record) Then should it turns out that the first record contains the name to be deleted, the following code should modify the list:

struct record *nextRecord = ** start
nextRecord = nextRecord.next
clear record whose address is in **start from heap
**start = nextRecord

Is this the correct syntax? Is there another way to do this? Would it just be better to start the list with a struct record instead of a struct record pointer?

Thanks,
jthep
Oct 7 '07 #1
4 1441
jthep
34
I think that another way to delete the first struct record can go as follows too..

struct record *nextRecord = ** start
**start = **start.next
clear record whose address is in nextRecord from heap
Oct 8 '07 #2
mattmao
121 100+
This question is taken from, could be, the Linked List Chapter.

You need to record the address of the head pointer to handle extreme cases, say, what if you want to delete the first element in this Linked List?

Search for any references regarding "C Linked List" and you definitely would see a bunch of code examples show you what is the proper way of doing these modifications.

Generally speaking, a pointer to pointer is the same as a pointer to variable.
It contains the address of a pointer, which is pointing at a variable's address in memory.

you can dereference once to get the address of that pointer to variable and twice to get the content of the variable.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int a = 100;
  6.     int * pty = &a;
  7.     int ** pty2 = & pty;
  8.  
  9.     printf("This is a %d\n", a);
  10.     printf("This is what the pty contains %d\n", *pty);
  11.     printf("This is what the pty2 contains %d\n", **pty2);
  12. }
  13.  
Result:
Expand|Select|Wrap|Line Numbers
  1. bash-3.2$ gcc doublepointer.c
  2. bash-3.2$ ./a.out
  3. This is a 100
  4. This is what the pty contains 100
  5. This is what the pty2 contains 100
  6. bash-3.2$ 
Oct 8 '07 #3
jthep
34
This question is taken from, could be, the Linked List Chapter.

You need to record the address of the head pointer to handle extreme cases, say, what if you want to delete the first element in this Linked List?

Search for any references regarding "C Linked List" and you definitely would see a bunch of code examples show you what is the proper way of doing these modifications.

Generally speaking, a pointer to pointer is the same as a pointer to variable.
It contains the address of a pointer, which is pointing at a variable's address in memory.

you can dereference once to get the address of that pointer to variable and twice to get the content of the variable.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int a = 100;
  6.     int * pty = &a;
  7.     int ** pty2 = & pty;
  8.  
  9.     printf("This is a %d\n", a);
  10.     printf("This is what the pty contains %d\n", *pty);
  11.     printf("This is what the pty2 contains %d\n", **pty2);
  12. }
  13.  
Result:
Expand|Select|Wrap|Line Numbers
  1. bash-3.2$ gcc doublepointer.c
  2. bash-3.2$ ./a.out
  3. This is a 100
  4. This is what the pty contains 100
  5. This is what the pty2 contains 100
  6. bash-3.2$ 
ooooh okay...so now I remember what my professor told me. Taking your example, I can view dereferencing as "cell whose address in pty2 is equal to address of pty1" and that would give the the double dereferencing to be "cell whose address in pty2 is equal to ('cell whose address in pty1 is equal to 100')" which is 100. Thank you.
Oct 8 '07 #4
mattmao
121 100+
ooooh okay...so now I remember what my professor told me. Taking your example, I can view dereferencing as "cell whose address in pty2 is equal to address of pty1" and that would give the the double dereferencing to be "cell whose address in pty2 is equal to ('cell whose address in pty1 is equal to 100')" which is 100. Thank you.
My demo code might not be the proper way of demonstrating the use of pointer to pointer. Anyway if you want to see a professional demonstration upon this issue, find a reference book. I found your saying was a little bit of "not to the point"...

Anyway,

Expand|Select|Wrap|Line Numbers
  1. pty2 = &pty;
  2.              pty = *pty2;
  3.  

Think of pty as a variable rather than a pointer, that might give you a clearer idea.
Oct 8 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.