![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 181
| understanding double pointers in functions and linked lists When I use a single point of indirection and pass the value in a pointer variable it is being passed call by value. When I use a double pointer I get to use the address operator (&) to pass the pointer which tells the compiler it is to be passed call by reference. If this is the case, it sure wasn't mentioned in the book! Does a primitive type only get passed call by reference when it has the address operator input in the function argument? Code: #include <stdio.h>
#include <stdlib.h>
struct listNode {
char data;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
/* Does this mean any structure declared using ListNodePtr
will automatically be a pointer without using the * cast? */
//void insert( ListNodePtr *, char );
void insert( ListNode **sPtr, char value );
char delete( ListNodePtr *, char );
int isEmpty( ListNodePtr );
void printList( ListNodePtr );
void instructions( void );
int main()
{
ListNode *startPtr = NULL;
int choice;
char item;
instructions();
printf( "? " );
scanf( "%d", &choice );
while( choice != 3 ) {
switch( choice ) {
case 1:
printf( "Enter a character: " );
scanf( "\n%c", &item );
/* startPtr is a pointer. What does it mean to use the address operator on a pointer? */
insert( &startPtr, item );
printList( startPtr );
break;
case 2:
if ( !isEmpty( startPtr ) ) {
printf( "Enter character to be deleted: " );
scanf( "\n%c", &item );
if ( delete( &startPtr, item ) ) {
printf( "%c deleted.\n", item );
printList( startPtr );
}
else
printf( "%c not found.\n\n", item );
}
else
printf( "List is empty.\n\n" );
break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
}
printf( "? " );
scanf( "%d", &choice );
}
printf( "%c\n", startPtr->data );
printf( "End of run.\n" );
return 0;
}
void insert( ListNode **sPtr, char value )
{
ListNode *newPtr, *previousPtr, *currentPtr;
newPtr = malloc( sizeof( ListNode ) );
if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->nextPtr = NULL;
currentPtr = *sPtr;
previousPtr = NULL;
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
}
void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the lsit.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n" );
}
/*
void insert( ListNode *sPtr, char value )
{
ListNodePtr newPtr, previousPtr, currentPtr;
newPtr = malloc( sizeof( ListNode ) );
if ( newPtr != NULL ) {
// data is not a pointer, why is the -> used to access it then?
newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = sPtr;
// Is currentPtr pointing to the value at sPtr or the address of sPtr?
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = sPtr;
sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else
printf( "%c not inserted. No memory available.\n", value );
}
*/
char delete( ListNodePtr *sPtr, char value )
{
ListNodePtr previousPtr, currentPtr, tempPtr;
/* Does this mean the value of a struct name is the memory address? */
/* Could I use a non-pointer struct and change the mem address it points to? */
if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr = ( *sPtr )->nextPtr;
free( tempPtr );
return value;
}
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;
while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
return value;
}
}
return '\0';
}
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}
void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL )
printf( "List is empty.\n\n" );
else {
printf( "The list is:\n" );
while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
}
|
| yougene is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,836
| Everything is always passed by value. You are passing the address of startPtr by value. Of course, that means that since you have an address, you can write to that address, so you can change the contents of startPtr. |
| tabstop is online now | |
| | #3 |
| Registered User Join Date: May 2006
Posts: 181
| Thanks that clears some questions up. |
| yougene is offline | |
| | #4 |
| Registered User Join Date: May 2006
Posts: 181
| Is the name of a structure without any accessor operators a pointer to the structure? |
| yougene is offline | |
| | #5 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #6 |
| Registered User Join Date: May 2006
Posts: 181
| Ahh I see. But if I declare a pointer to a struct and point it to an existing struct I don't have to use the (*) operator either. Code: struct blah *apointer; struct blah structure; apointer = &structure; //no * required here apointer.member; |
| yougene is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,836
| Except apointer.member is syntactically invalid. You have to use apointer->member instead. |
| tabstop is online now | |
| | #8 |
| Registered User Join Date: May 2006
Posts: 181
| Don't you also use -> if member is a pointer and "apointer" isn't? The use of -> is where my question originally arose from. |
| yougene is offline | |
| | #9 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,338
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #10 |
| Registered User Join Date: May 2006
Posts: 181
| Gotchya thanks |
| yougene is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linked Lists 101 | The Brain | C++ Programming | 5 | 07-24-2004 04:32 PM |
| Passing pointers between functions | heygirls_uk | C Programming | 5 | 01-09-2004 06:58 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| One more question about linked lists? | shaeng | C Programming | 2 | 06-24-2003 07:36 AM |
| Linked lists | sballew | C Programming | 6 | 10-24-2001 08:52 PM |