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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with array of structures

Status
Not open for further replies.

hunghsun

Programmer
Aug 25, 2002
31
0
0
US
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 (&quot;%p\n&quot;, &New_Entry);
printf (&quot;%p\n&quot;, &New_Entry.Hash_Value);
printf (&quot;%p\n&quot;, &New_Entry.Name);
printf (&quot;%p\n&quot;, &New_Entry.Data_Type);
printf (&quot;%p\n&quot;, &New_Entry.Home_Node);
printf (&quot;%p\n&quot;, &New_Entry.Local_Pointer);
printf (&quot;%p\n&quot;, &New_Entry.Next);
/*
printf (&quot;%p\n&quot;, &New_Entry);
printf (&quot;%d\n&quot;, Hash_Value);
printf (&quot;%s\n&quot;, New_Entry.Name);
printf (&quot;%s\n&quot;, New_Entry.Data_Type);
printf (&quot;%s\n&quot;, New_Entry.Home_Node);
printf (&quot;%p\n&quot;, New_Entry.Local_Pointer);
printf (&quot;********************\n&quot;);
*/

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 (&quot;Hash ---- Name ---- Type ---- Home ---- Local ---- Next\n&quot;);
printf (&quot;%d - &quot;, Entry_In->Hash_Value);
printf (&quot;%s - &quot;, Entry_In->Name);
printf (&quot;%s - &quot;, Entry_In->Data_Type);
printf (&quot;%s - &quot;, Entry_In->Home_Node);
printf(&quot;%p - &quot;, Entry_In->Local_Pointer);
printf(&quot;%p\n&quot;, Entry_In->Next);

printf (&quot;********************\n&quot;);
printf (&quot;%p\n&quot;, Entry_In);
printf (&quot;%p\n&quot;, Entry_In->Hash_Value);
printf (&quot;%p\n&quot;, Entry_In->Name);
printf (&quot;%p\n&quot;, Entry_In->Data_Type);
printf (&quot;%p\n&quot;, Entry_In->Home_Node);
printf (&quot;%p\n&quot;, Entry_In->Local_Pointer);
printf (&quot;%p\n&quot;, 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 (&quot;a&quot;, &quot;int&quot;, &quot;NODE X&quot;);
// Dir_Insert (&quot;z&quot;, &quot;int&quot;, &quot;NODE X&quot;);
Print_Entry (Directory[97]);
// Dir_Print();
}
 
There are a lot of problems with your program.
One being the italics.
You need to post inside
Code:
markers
or turn off TGML.

Instead of going through line by line, I'll
just post a very simple example that basically
works.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MAXSZ 1000

typedef struct
    {
        int Hash_Value;                                                   
        char Name[100];                                                    
        char Data_Type[100];
        char Home_Node[100];
        struct Entry_Struct *Local_Pointer;
        int Lock;
        struct Entry_Struct *Next;                                        
    } PRIMDATA;

PRIMDATA *directory[1000];

void printPts(PRIMDATA **);
void initElement(PRIMDATA *);
void printNode(PRIMDATA *);
 
int main(void) {

         printPts(directory);

return 0;
}


void printPts(PRIMDATA **data) {
int n = 0;

        while (n < MAXSZ) {
              printf(&quot;Element %d at address %p, data size = %d\n&quot;, n, &data[n], sizeof(**data));
              data[n] = malloc(sizeof(PRIMDATA));
              printf(&quot;Memory allocated for element at %p\n&quot;, data[n]);
              initElement(data[n]);
              printNode(data[n]);
              ++n;
        }
} 

             

void initElement(PRIMDATA *ptr) {

               ptr->Hash_Value = (1 + rand() % MAXSZ);
               printf(&quot;Assigned hv to ptr: %d\n&quot;,ptr->Hash_Value);
               strcpy(ptr->Name,&quot;dingo&quot;);
               strcpy(ptr->Data_Type,&quot;stuff&quot;);
               strcpy(ptr->Home_Node,&quot;otherstuff&quot;);
               ptr->Lock = 0;
               ptr->Local_Pointer = NULL;
               ptr->Next = NULL;
}         
 

void printNode(PRIMDATA *aptr) {

     printf(&quot;Hash val %d, Name %s, Data Type %s, Lock %d, Next %p\n\n&quot;, aptr->Hash_Value,
           aptr->Name,aptr->Data_Type,aptr->Lock,aptr->Next);
}

You should take a look at some of the pointer tutorials
out on the web, you are not tracking on them right now.
 
Just a general problem solving point. Simplify or isolate the problem down to the smallest implementation possible when researching a problem rather than deal with layers of existing code and large data structures that are unrelated to the problem set.

&quot;But, that's just my opinion... i could be wrong.&quot;
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top