Hi,
I have the following code that passes a pointer of a structure so that the function can print the content of the structure, but for some reason I am not able to get the correct value. I printed out the address that's passing in to the function (which points to the structure) and it is correct, but when I print out the address and content for the member, I am getting something totally wierd. Could someone look at my code and tell me what I am doing wrong? thanx!
Hung-Hsun
Output
0012FE34
0012FE34
0012FE38
0012FE9C
0012FF00
0012FF64
0012FF6C
Hash ---- Name ---- Type ---- Home ---- Local ---- Next
97 - a - ?ÿÿ - 4•@ - 0012FF00 - 0012FFC0
********************
0012FE34
0012FE34
0012FE38
0012FE9C
0012FF00
0012FF00
0012FFC0
Code
#include <stdio.h>
#include <string.h>
struct Entry_Struct
{
int Hash_Value; // Hash value of entry
char Name[100]; // Name of variable
char Data_Type[100];
char Home_Node[100];
struct Entry_Struct *Local_Pointer;
int Lock;
struct Entry_Struct *Next; // Pointer to the next variable with the same Hash value
};
const DIRECTORY_SIZE = 1000;
static struct Entry_Struct *Directory[1000]; // Number should match the directory size constant
void Dir_Init ()
{
for (int i = 0; i < DIRECTORY_SIZE; i++)
{
Directory = NULL;
}
}
int Is_Empty (int Hash_In)
{
if (Directory[Hash_In] == NULL)
{
return 1;
}
else
{
return 0;
}
}
int Hash (char Name_In[])
{
int Output = 0; // Store the output of hash value
int Input_Length = strlen(Name_In); // Length of the Input string
for (int i = 0; i < Input_Length; i++)
{
Output = Output*128 + (int) Name_In;
}
if (Input_Length > 0)
{
return Output % DIRECTORY_SIZE;
}
else
{
return -1;
}
}
void Dir_Insert (char Name_In[], char Type_In[], char Home_In[])
{
int Hash_Value = Hash (Name_In);
struct Entry_Struct New_Entry;
// Setting up the fields for the new entry
New_Entry.Hash_Value = Hash_Value;
strcpy (New_Entry.Name, Name_In);
strcpy (New_Entry.Data_Type, Type_In);
strcpy (New_Entry.Home_Node, Home_In);
New_Entry.Local_Pointer = NULL;
printf ("%p\n", &New_Entry);
printf ("%p\n", &New_Entry.Hash_Value);
printf ("%p\n", &New_Entry.Name);
printf ("%p\n", &New_Entry.Data_Type);
printf ("%p\n", &New_Entry.Home_Node);
printf ("%p\n", &New_Entry.Local_Pointer);
printf ("%p\n", &New_Entry.Next);
/*
printf ("%p\n", &New_Entry);
printf ("%d\n", Hash_Value);
printf ("%s\n", New_Entry.Name);
printf ("%s\n", New_Entry.Data_Type);
printf ("%s\n", New_Entry.Home_Node);
printf ("%p\n", New_Entry.Local_Pointer);
printf ("********************\n"
;
*/
if (Is_Empty(Hash_Value))
{
New_Entry.Next = NULL;
}
else
{
New_Entry.Next = Directory[Hash_Value];
}
Directory[Hash_Value] = &New_Entry;
}
void Print_Entry (struct Entry_Struct *Entry_In)
{
printf ("Hash ---- Name ---- Type ---- Home ---- Local ---- Next\n"
;
printf ("%d - ", Entry_In->Hash_Value);
printf ("%s - ", Entry_In->Name);
printf ("%s - ", Entry_In->Data_Type);
printf ("%s - ", Entry_In->Home_Node);
printf("%p - ", Entry_In->Local_Pointer);
printf("%p\n", Entry_In->Next);
printf ("********************\n"
;
printf ("%p\n", Entry_In);
printf ("%p\n", Entry_In->Hash_Value);
printf ("%p\n", Entry_In->Name);
printf ("%p\n", Entry_In->Data_Type);
printf ("%p\n", Entry_In->Home_Node);
printf ("%p\n", Entry_In->Local_Pointer);
printf ("%p\n", Entry_In->Next);
}
void Dir_Print ()
{
for (int i = 0; i < DIRECTORY_SIZE; i++)
{
if (!Is_Empty(i))
{
Print_Entry(Directory);
}
}
}
int main(void)
{
Dir_Init ();
Dir_Insert ("a", "int", "NODE X"
;
// Dir_Insert ("z", "int", "NODE X"
;
Print_Entry (Directory[97]);
// Dir_Print();
}
I have the following code that passes a pointer of a structure so that the function can print the content of the structure, but for some reason I am not able to get the correct value. I printed out the address that's passing in to the function (which points to the structure) and it is correct, but when I print out the address and content for the member, I am getting something totally wierd. Could someone look at my code and tell me what I am doing wrong? thanx!
Hung-Hsun
Output
0012FE34
0012FE34
0012FE38
0012FE9C
0012FF00
0012FF64
0012FF6C
Hash ---- Name ---- Type ---- Home ---- Local ---- Next
97 - a - ?ÿÿ - 4•@ - 0012FF00 - 0012FFC0
********************
0012FE34
0012FE34
0012FE38
0012FE9C
0012FF00
0012FF00
0012FFC0
Code
#include <stdio.h>
#include <string.h>
struct Entry_Struct
{
int Hash_Value; // Hash value of entry
char Name[100]; // Name of variable
char Data_Type[100];
char Home_Node[100];
struct Entry_Struct *Local_Pointer;
int Lock;
struct Entry_Struct *Next; // Pointer to the next variable with the same Hash value
};
const DIRECTORY_SIZE = 1000;
static struct Entry_Struct *Directory[1000]; // Number should match the directory size constant
void Dir_Init ()
{
for (int i = 0; i < DIRECTORY_SIZE; i++)
{
Directory = NULL;
}
}
int Is_Empty (int Hash_In)
{
if (Directory[Hash_In] == NULL)
{
return 1;
}
else
{
return 0;
}
}
int Hash (char Name_In[])
{
int Output = 0; // Store the output of hash value
int Input_Length = strlen(Name_In); // Length of the Input string
for (int i = 0; i < Input_Length; i++)
{
Output = Output*128 + (int) Name_In;
}
if (Input_Length > 0)
{
return Output % DIRECTORY_SIZE;
}
else
{
return -1;
}
}
void Dir_Insert (char Name_In[], char Type_In[], char Home_In[])
{
int Hash_Value = Hash (Name_In);
struct Entry_Struct New_Entry;
// Setting up the fields for the new entry
New_Entry.Hash_Value = Hash_Value;
strcpy (New_Entry.Name, Name_In);
strcpy (New_Entry.Data_Type, Type_In);
strcpy (New_Entry.Home_Node, Home_In);
New_Entry.Local_Pointer = NULL;
printf ("%p\n", &New_Entry);
printf ("%p\n", &New_Entry.Hash_Value);
printf ("%p\n", &New_Entry.Name);
printf ("%p\n", &New_Entry.Data_Type);
printf ("%p\n", &New_Entry.Home_Node);
printf ("%p\n", &New_Entry.Local_Pointer);
printf ("%p\n", &New_Entry.Next);
/*
printf ("%p\n", &New_Entry);
printf ("%d\n", Hash_Value);
printf ("%s\n", New_Entry.Name);
printf ("%s\n", New_Entry.Data_Type);
printf ("%s\n", New_Entry.Home_Node);
printf ("%p\n", New_Entry.Local_Pointer);
printf ("********************\n"
*/
if (Is_Empty(Hash_Value))
{
New_Entry.Next = NULL;
}
else
{
New_Entry.Next = Directory[Hash_Value];
}
Directory[Hash_Value] = &New_Entry;
}
void Print_Entry (struct Entry_Struct *Entry_In)
{
printf ("Hash ---- Name ---- Type ---- Home ---- Local ---- Next\n"
printf ("%d - ", Entry_In->Hash_Value);
printf ("%s - ", Entry_In->Name);
printf ("%s - ", Entry_In->Data_Type);
printf ("%s - ", Entry_In->Home_Node);
printf("%p - ", Entry_In->Local_Pointer);
printf("%p\n", Entry_In->Next);
printf ("********************\n"
printf ("%p\n", Entry_In);
printf ("%p\n", Entry_In->Hash_Value);
printf ("%p\n", Entry_In->Name);
printf ("%p\n", Entry_In->Data_Type);
printf ("%p\n", Entry_In->Home_Node);
printf ("%p\n", Entry_In->Local_Pointer);
printf ("%p\n", Entry_In->Next);
}
void Dir_Print ()
{
for (int i = 0; i < DIRECTORY_SIZE; i++)
{
if (!Is_Empty(i))
{
Print_Entry(Directory);
}
}
}
int main(void)
{
Dir_Init ();
Dir_Insert ("a", "int", "NODE X"
// Dir_Insert ("z", "int", "NODE X"
Print_Entry (Directory[97]);
// Dir_Print();
}