Jump to content
  • Advertisement
Sign in to follow this  
Ekim_Gram

Problem with pointers

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Today I started going through the "Pointers Structures and Linked Lists" series and I got up to the third article. I went through it and everything seemed all fine and dandy until I tried using the NewAddress function that was defined in the article. Here's my source code:
#include <iostream>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

#define SUCCESS 0
#define FAILURE 1

typedef struct strAddress
{
    char Name[30];
    char Address[180];
    char PhoneNo[10];
    struct strAddress *Next;
} Address_t, *AddressPtr_t;

// Function allocates memory for a new Address to be added to the list
int NewAddress(AddressPtr_t Head, char Name[30], char Address[180], char PhoneNo[10])
{
	AddressPtr_t Temp, Current = Head;

	// Go to the end of the list
	if (Current)
		while (Current->Next)
			Current = Current->Next;

	// Allocate a new block of memory for this address
	Temp = (AddressPtr_t)malloc(sizeof(Address_t));

	// Ensure that the allocation passed
	if (!Temp) return (FAILURE);

	// Add the block to the end of the list
	Temp->Next = NULL;

	if (!Current)
	{
		// If this is the first element, then avoid assertion
		Current = Temp;
	}
	else
	{
		// Add to the end of the list
		Current->Next = Temp;
	}

	// Update the values of the new block
	strcpy(Temp->Name, Name);
	strcpy(Temp->Address, Address);
	strcpy(Temp->PhoneNo, PhoneNo);
	
	return (SUCCESS);
}

int CleanUp(AddressPtr_t Head)
{
	AddressPtr_t Current = Head;
	while (Current)
	{
		Head = Current;
		Current = Current->Next;
		free(Head);
	}
	return (SUCCESS);
}

int main()
{
	AddressPtr_t Address;
	NewAddress(Address, "John Smith", "123 Fake Street", "5551234");
	return 0;
}

With that, it crashes. So I decided to do a little bit of testing and came up with this:
int main()
{
  AddressPtr_t Address;
  Address->Next = NULL;
  return 0;
}
After running the program in Debug mode, I've (VC.NET) come to the conclusion that the line Address->Next = NULL; is what's causing the problem, and that very line is in the NewAddress function. How can I go about and fix this?

Share this post


Link to post
Share on other sites
Advertisement
You need to allocate memory first. As the name suggests, AddressPtr_t is just a pointer to memory.
You need to do


int main()
{
AddressPtr_t address = (AdressPtr_t) malloc ( sizeof(Address_t) );
address->Next = NULL;

CleanUp ( address );

return 0;
}


[edit]
BrainFart:

int main()
{
AddressPtr_t Address = NULL; // <-- YOU MUST INITALISE TH POINTER FIRST!
NewAddress(Address, "John Smith", "123 Fake Street", "5551234");
Cleanup ( Address ); // Don't forget that.
return 0;
}

[/edit]

Share this post


Link to post
Share on other sites
Quote:
Original post by Ekim_Gram
I see. Thank you for your help kind sir.

You're welcome - no need to be so formal [smile].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!