Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer initialization

Status
Not open for further replies.

chachi

Programmer
Joined
May 4, 2002
Messages
8
Location
US
In the following code, why is the line highlighted in red set to null?

Code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

struct listNode {
	char data;
	struct listNode *nextPtr;
};

typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;

void insert(LISTNODEPTR *, char);
char delete_me(LISTNODEPTR *, char);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);

int main(int argc, char* argv[])
{
LISTNODEPTR startPtr = NULL;
Code:
	int choice;
	char item;

	instructions(); /*display the menu*/
	printf(&quot;? &quot;);
	scanf(&quot;%d&quot;, &choice);

	while (choice != 3) {
		switch (choice) {
		case 1:
			printf(&quot;Enter a character &quot;);
			scanf(&quot;\n%c&quot;, &item);
			insert(&startPtr, item);
			printList(startPtr);
			break;
		case 2:
			if (!isEmpty(startPtr)) {
				printf(&quot;Enter character to be deleted: &quot;);
				scanf(&quot;\n%c&quot;, &item);
				if (delete_me(&startPtr, item)) {
					printf(&quot;%c deleted.\n&quot;, item);
					printList(startPtr);
				}
				else
					printf(&quot;%c not found. \n\n&quot;, item);
			}
			else
				printf(&quot;List is empty.\n\n&quot;);
			break;
		default:
			printf(&quot;Invalid choice.\n\n&quot;);
			instructions();
			break;
		}
		printf(&quot;? &quot;);
		scanf(&quot;%d&quot;, &choice);
	}
	printf(&quot;End of run.\n&quot;);

	return 0;
}

/*Print the instructions*/
void instructions(void)
{
	printf(&quot;Enter your choice:\n&quot;
		&quot;   1 to insert an element into the list.\n&quot;
		&quot;   2 to delete an element from the list.\n&quot;
		&quot;   3 to end.\n&quot;);
}

/*Insert a new value into the list in sorted order*/
void insert(LISTNODEPTR *sPtr, char value)
{
	LISTNODEPTR newPtr, previousPtr, currentPtr;
	newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));
	if (newPtr != NULL) {
		newPtr->data = value;
		newPtr->nextPtr = NULL;

		previousPtr = NULL;
		currentPtr = *sPtr;
		
		while (currentPtr != NULL && value > currentPtr->data) {
			previousPtr = currentPtr;  /*walk to...*/
			currentPtr = currentPtr->nextPtr;/*...next node*/
		}

		if (previousPtr == NULL) {
			newPtr->nextPtr = *sPtr;
			*sPtr = newPtr;
		}
		else {
			previousPtr->nextPtr = newPtr;
			newPtr->nextPtr = currentPtr;
		}
	}
	else
		printf(&quot;%c not inserted. No memory available.\n&quot;, value);
}
 
It is usually good programming strategy to initialize a pointer to NULL. That way you can check if it has a value (if ptr != NULL) before you delete it because deleting an uninitialized pointer will crash a program.
 
just like initializing a local variable at the time of declaration so that it does not contain some junk value , likewise it is good programming practise to initialize pointers , and the correct / safe initial value a pointer could have is NULL . does it clear your doubt or is there something else you have in mind
 
Gotcha. Thanks for the input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top