Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-28-2008, 08:33 PM   #1 (permalink)
complete
Code Monkey
 
complete's Avatar
 
Join Date: Jul 2005
Location: St. Louis
Posts: 76
complete is on a distinguished road
pointer to a pointer

I need to brush up on pointers and I think a good way is to start with this question. If you want to change a pointer in a function, you need to pass a pointer to a pointer. WHy is this?




consider a linked list with the node like this:

typedef struct elementT{
int data;
struct element *next;
}


now, consider this function to insert a new header:


int BadInsert(element *head)
{
element *newElem;
newElem = (element *) malloc(sizeof(element));

if (!newElem)
return 0;

newElem->next = head;

head = newElem;

return 1;
}

This incorrectly updates local copy of head.
Calling code retains the old value for the first tlement
pointer, so it nowpoints at the second element of the list.

The correct way to update the head pointer in C is to pass a pointer
to the head pointer, allowing you to modify the calling function's pointer to the first element,
as shown here:

int Insert(element **head)
{
element *newElem;
newElem = (element *) malloc(sizeof(element));

if (!newElem)
return 0;

newElem->next = *head;

head* = newElem;

return 1;
}


My question is this. How would I call this Insert function?
__________________
46 45 52 4D 49 20 57 41 53 20 4A 45 44 49 2E 10
20 53 41 47 41 4E 20 57 41 53 20 53 49 54 48 2E
complete is offline   Reply With Quote
Old 07-29-2008, 12:28 PM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
Pass by reference:
Code:
Insert(&heads);
Problem is that you don't append but prepend instead. Therefore the pointer to "heads" is not altered.
A different way is:
Code:
element * prepend(element *head)
{
    element *newElem = (element *) malloc(sizeof(element));
    if (!newElem) return head;

    newElem->next = head;

    return newElem;
}

element *elements = (element *) malloc(sizeof(element));
elements = prepend(elements);
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-23-2008, 12:43 PM   #3 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
final exam...make sure you have it mastered!!!

I got this on an interview once!

write a little program method to switch the contents of two pointers
Code:
#include <iostream>
using namespace std;

void switchx(code goes here)
{
code goes here
}
int main()
{

int* p1 = new int();
*p1 = -100;
int* p2 = new int();
*p2 = 100;
cout<<"before "<<*p1<<" ,"<<*p2<<endl;
switchx(&p1, &p2);
cout<<"after "<<*p1<<" ,"<<*p2<<endl;
}
answer on request!
landonmkelsey is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer error ron76 Standard C, C++ 10 07-14-2006 08:00 AM
sizeof() and pointer vs array DJMaze Standard C, C++ 21 04-19-2005 06:44 PM
Array to pointer to pointer frier Standard C, C++ 8 03-25-2005 07:57 PM
Explicit vs. Implicit use of the this Pointer fp_unit Standard C, C++ 2 01-30-2005 04:20 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 06:12 AM


All times are GMT -8. The time now is 05:47 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting