Solved

error:integer from pointer

Posted on 2004-05-01
75
395 Views
Last Modified: 2010-04-15
I am trying to implement a hash table where each hashtable element is a pointer to a linked list of strings. Here is what my code looks like
#define MAX_DISTRICTS 31
#define MAX_CHARACTERS 25
struct node
{
    char *name[MAX_CHARACTERS];
    struct node *next;
};

void insertName(int,char);
main()
{
        char sname[MAX_CHARACTERS];
         scanf("%s",sname);
         insertName(hashkey,sname);

}


insertName(int key, char iname)
{
           struct node *q,*r,*temp;
           temp=q=distArray[key];
           
           r=malloc(sizeof(struct node));
           r->name=iname;
           //rest of the code tries to insert the name in correct position in the list sorted lexicographicaly
}

I am getting lot of errors and started learning c just few days ago . I get this error:
passing arg 2 of `insertSchool' makes integer from pointer without a cast
and also some other errors.

What am I supposed to do now?How do I pass sname to the insertName method to insert it into the string?
Some one please help. Thanks.
0
Comment
Question by:b_vishwajit
  • 41
  • 26
  • 7
  • +1
75 Comments
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969586
variable <b>distArray</b> stores hashTable elements which are ints
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969615
>>passing arg 2 of `insertName' makes integer from pointer without a cast
sorry about the typo.
0
 
LVL 12

Assisted Solution

by:PCableGuy
PCableGuy earned 20 total points
ID: 10969640
I can see one problem:

scanf("%s",sname);

sname is a string, but insertName(int, char)
is expecting a char as the second argument.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969668
Hmm. That was so silly. But string are char arrays right.
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 10969677
Yes, a string in C is an array of characters ending in a "null character."

Try char* instead of char
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969689
I already tried it before posting the question but in vain.
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 10969695
Did the compiler give the same error?
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 10969726
Another question:

Was this intended to be an array of strings?
char *name[MAX_CHARACTERS];
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969736
>>Did the compiler give the same error?
Yes
>>Was this intended to be an array of strings?
char *name[MAX_CHARACTERS];
<<
That was a mistake. I guess it should be char name[MAX_CHARACTERS]; which will be the data of the linked list i.e. a string.
A linked list of strings.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969741
PCableGuy how long do you plan to stay on EE?
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 10969747
Not sure....

Next hour or so...
If I figure out something concrete, I'll post the code.

Deal?
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969784
PCableGuy thanks. I have worked it out. Just used your suggestion. It is working but a new problem has shown up.
When I insert first element into the list it is inserted properly without any problems. But when I try to to insert the next element I get this error by the OS (which is MS -XP).

Error details:
schools.exe has encountered a problem  a problem and needs to close.
For more information about this error, click here.


When i click on the link another window pops up whcih says:
Error signature_____________________________________________
AppName:schools.exe                 AppVer:0.0.0.0       ModName:msvcrt.dll
ModVer:7.0.2600.1106               offset:000336d0

Its probably a pointer error. I am trying to track it down.
Thanks.

0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 10969794
Admittedly, I'm not a C expert.

If I had any chance of helping you, you would probably have to post all your code....

Hang in there...
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 10969897
Does it happen "immediately after you hit the enter key" on keyboard?
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10969981
>>Does it happen "immediately after you hit the enter key" on keyboard?
Yes thats right.First time it works properly and then second time it gives me immediately after scanning the input string. Its probably a problem with the insertName method. I took a long break and continuing now. CableGuy by helping others is how become an expert atleast on EE thats how it works:). Hope someone having good experience with c pointers will drop by and join us.

I dont want to post my whole source code and make this discussion annoying. I will post it when I feel it is realy necessary.Thanks.

---Peace vishwa.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970033
From the code posted:

          struct node *q,*r,*temp;
          temp=q=distArray[key];

and distarray is an int array,why are you assigning an int value to a structure pointer?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970060
Also,

>r=malloc(sizeof(struct node));
>r->name=iname;

You should cast the return value of malloc to (struct node *);
r=(struct node *)malloc(sizeof(struct node));

if name and iname are char arrays,r->iname=iname wont work.
use strcpy(r->name,iname);

0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970113
>>variable <b>distArray</b> stores hashTable elements which are ints
I gave wrong information. distArray is actualy an array of pointers of type node.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970115
struct node *distArray[MAX_SIZE];//MAX_SIZE-> maximum number of elements in the hash table.
Hope that makes it clear.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970140
>temp=q=distArray[key];
You read the current value of the hash table for the key value,then what do you do with it.

>a linked list of strings.
if u use char *name[MAX_CHARACTERS],then its an array of pointers,you cannot modify the contents of the strings.as it might be placed in read only memory.

0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970178
ankuratvb,
i am not using malloc anymore to insert data into the node. I am using strcpy(r->name,scname); to insert the data. It is working but  when i execute the program i can insert the first string and some times second string. But at some stage it  crashes (which is uncertain. sometimes it crashes while i try to insert the second string.)Am I doing it properly, what I mean is am I using the right way to insert data into the node.I can paste my code if you want to see it.Thanks.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970182
>>if u use char *name[MAX_CHARACTERS],then its an array of pointers,you cannot modify the contents of the strings.as it might be placed in read only memory

I said it before:
>>Was this intended to be an array of strings?
char *name[MAX_CHARACTERS];
<<
That was a mistake. I guess it should be char name[MAX_CHARACTERS]; which will be the data of the linked list i.e. a string.
A linked list of strings.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970183
so i just declared it to be a char array.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970190
Ok I am posting my  insertSchool method to make things clear:

void insertSchool(int index,char* scname)
{
    struct node *q,*r,*temp;
   
    temp=q=distArray[index];
   
    r=malloc(sizeof(struct node));
    strcpy(r->name,scname);
   
    /*if list is empty*/
    if(q==NULL||strcmp(q->name,scname)>0)
    {
        q=r;
        q->next=temp;
        distArray[index]=q;
    }
    else
    {
        /*traverse the list*/
        while(temp!=NULL)
        {
                if((strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
                    ||temp->next==NULL)
                {
                                r->next=temp->next;
                                temp->next=r;
                                return;
                }
               
                temp=temp->next;
        }
        r->next=NULL;
        temp->next=r;
    }
}

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970195
A char array is only 1 string.
A linked list of strings is a list of strings i.e. multiple char arrays in a Linked list.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970202
Thats exactly what you've done. :~)
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970219
Ok the error I am getting is segmentation fault. I get this error when I try to insert more than one element in the list. I guess it is pointer casting problem. But i see no mistakes with my insertSchool method. As I told before I have just started learning C. Please help..............Thanks.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970237
I didnot get you ankuratvb?????So just using a single char array is the problem. Is that what you are tryin to say?
or am I too dumb to understand what you are tryin to say:). Pardon me actualy I have no patience right now to think calmly about this (cause i used java before.). I will think about it and get back to you.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970247
Hi,

your problem is that distArray[] is not initialized.
do this:

struct node *distArray[MAX_SIZE]={NULL};

Without this distArray has some arbitrary values so in the first time also,it doesnt give NULL.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970253
Ankur I did that. But still get same error. Segmentation fault and the program exits after inserting just one element into the list.
Thanks for your help. I appreciate your patience.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970257
I think its  a pointer casting  problem.Any suggestions?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970270
This is what i did with your code.This works.

Make sure you have included malloc.h,string.h and stdio.h

#include<malloc.h>
#include<string.h>
#include<stdio.h>
#define MAX_DISTRICTS 31
#define MAX_CHARACTERS 25
#define MAX_SIZE 100
struct node
{
   char name[MAX_CHARACTERS];
   struct node *next;
};

struct node *distArray[MAX_SIZE]={NULL};//MAX_SIZE-> maximum number of elements in the hash table.
void insertSchool(int index,char* scname);

main()
{
       char sname[MAX_CHARACTERS];
//       scanf("%s",sname);
       insertSchool(1,"abc");
       insertSchool(1,"sdef");
       return 0;
}

void insertSchool(int index,char* scname)
{
   struct node *q,*r,*temp;

   temp=q=distArray[index];

   r=(struct node *)malloc(sizeof(struct node));
   strcpy(r->name,scname);

   /*if list is empty*/
   if(q==NULL||strcmp(q->name,scname)>0)
   {
       q=r;
       q->next=temp;
       distArray[index]=q;
   }
   else
   {
       /*traverse the list*/
       while(temp!=NULL)
       {
               if((strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
                   ||temp->next==NULL)
               {
                               r->next=temp->next;
                               temp->next=r;
                               return;
               }
               
               temp=temp->next;
       }
       r->next=NULL;
       temp->next=r;
   }
}
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970271
Very well:

"The segmentation fault is usually given when you’re trying to access an invalid memory location. You may get this message when you leave out the & in scanf, or when you put too many or too few * to deference a data value, or if you try to access a memory location (struct , string, etc.) before you allocate memory for it."

So what exactly I am doing in my insert method is one of the aforementioned tasks. I am trying to track it down and frying my brain. If you find it please point it out. Thanks.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970277
ankur,
 It works only for two strings:(. Try to insert a third one.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970283
Hi,
its working for multiple strings(>2) as well on my compiler.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970292
what compiler are you using.
I am getting segmentation fault on gcc. Let me check it again. It is totaly weird. I checked it few seconds ago and i found out that I was able to insert three string elements without any problem into the list but fourth I got segmentation fault.

0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970302
Another thing. Number of elements in my hash table is 31 i.e. MAX_DISTRICTS and my hash fucnction is:
hash(k)=k%MAX_DISTRICTS;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970316
Nope no good. I tried everything and the segmentation fault is chewing my wits.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970322
I am using Borland's TurboC++ compiler.

Did you try the same program that i posted.?

If you made any changes,pls let me know.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970367
I am still struck at the same point. Segmentation fault:(.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970376
It is an allocation problem. Still lookin at my code.......................
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970377
Did you try the same program that i posted.?

You could check the return value of malloc to see if the mem. was allocated or not.

for e.g. r=(struct node *) malloc(sizeof(struct node));
if(r==NULL) printf("Not enough memory");
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970390
I used your code but did not work for me. I tried your suggestion of return from malloc. But before doing that my program just crashes giving the segmentation fault.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970393
Try debugging to see at what point the seg fault occurs.

You could also run this to test a few things:

void insertSchool(int index,char* scname)
{
  struct node *q,*r,*temp;

  temp=q=distArray[index];

  printf("%s",q->name);

  r=(struct node *)malloc(sizeof(struct node));

  if(r==NULL) printf("Not enough Memory");

  strcpy(r->name,scname);

  printf("%s",r->name);

    /*if list is empty*/
  if(q==NULL||strcmp(q->name,scname)>0)
  {
      q=r;
      q->next=temp;
      distArray[index]=q;
  }
  else
  {
      /*traverse the list*/
      while(temp!=NULL)
      {
              if((strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
                  ||temp->next==NULL)
              {
                              r->next=temp->next;
                              temp->next=r;
                              return;
              }
             
               temp=temp->next;
      }
      r->next=NULL;
      temp->next=r;
  }
}
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970401
Place a printf("any text"); before and before the malloc statement.

And see whether both of those get printed before segfaulting or not.

If you dont see either of the text,the problem lies before the malloc.
If you see only one of the messages,malloc is the problem,
if you see both the msgs,the problem is somewhere after malloc.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970408
I had two print statement before malloc()
and the verdict is
>>if you see both the msgs,the problem is somewhere after malloc.
So is it after malloc(). Does temp pointer need any memory allocation too?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970413
for gcc,you need to include stdlib.h for malloc().
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970418
An Access violation (Segmentation fault) raised in your program. A window popped up saying that thing while I tried to debug.
So as I said before its an access violation due to which I am getting a seg fault.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970421
>Place a printf("any text"); before and before the malloc statement.

I am sorry.Small typo here.

Place a printf("any text"); before and *after* the malloc statement.

Something like:

printf("Hello");
//your malloc statement here
printf("World");
if(r==NULL) printf("No Memory");
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970422
i have included all neccessary header files including stdlib.h and malloc.h :(
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970430
>>I am sorry.Small typo here.

Yeha even I guessed that and tried it before you realised your typo :).

It is now confirmed that access violation is occuring after malloc (because both before and after malloc strings are printed).
Now where is this violation occuring.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970431
>An Access violation (Segmentation fault) raised in your program. A window popped up >saying that thing while I tried to debug.
>So as I said before its an access violation due to which I am getting a seg fault.


>>Try debugging to see at what point the seg fault occurs.
>>You could also run this to test a few things:
>void insertSchool(int index,char* scname)
>{
> struct node *q,*r,*temp;

> temp=q=distArray[index];

> printf("%s",q->name);

> r=(struct node *)malloc(sizeof(struct node));

> if(r==NULL) printf("Not enough Memory");

> strcpy(r->name,scname);

> printf("%s",r->name);

In this program,what printfs were executed.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970440

-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 1

Enter the school district number.
31
distArray[0]= 31
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
hert
(null)StringaStringb
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
furt
hertStringaStringb
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
furt
hert
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
gert

Enter a school name
furtStringaStringb
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
furt
gert
hert
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
shirt
furtStringaStringb
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970444
I guess the problem may lie here:

    /*traverse the list*/
     while(temp!=NULL)
     {
             if((strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
                 ||temp->next==NULL)
             {
                             r->next=temp->next;
                             temp->next=r;
                             return;
             }
 
              temp=temp->next;
     }
     r->next=NULL;
     temp->next=r; //here temp is NULL.So this assignment is invalid.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970455
It is working but ridiculous. Here is my sample output:


-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 1

Enter the school district number.
31
distArray[0]= 31
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
Xavier
(null)StringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
SBInst
XavierStringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
SBInst
Xavier
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
NVInst
SBInstStringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
NVInst
SBInst
Xavier
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
School
NVInstStringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
NVInst
SBInst
School
Xavier
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
Good
NVInstStringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
Good
NVInst
SBInst
School
Xavier
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
31

Enter a school name
ATtOP
GoodStringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number31

list:
ATtOP
Good
NVInst
SBInst
School
Xavier
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 1

Enter the school district number.
21
distArray[21]= 21
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
21

Enter a school name
JOTIRGAMAYA
(null)StringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number21

list:
JOTIRGAMAYA
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 2

Enter the school district number.
21

Enter a school name
Gurukul
JOTIRGAMAYAStringaStringbx&#9786;=
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection: 4

Enter the district number21

list:
Gurukul
JOTIRGAMAYA
-------------------------------------------------------------
1. Insert a new district.
2. Insert a new school.
3. Remove an existing school name.
4. Display alphabetical list of schools in a district.
5. Determine whether or not a district exists in  hashtable.
6. Exit
-------------------------------------------------------------

Selection:
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970457
>Selection: 2
>Enter the school district number.
>31

>Enter a school name
>shirt
>furtStringaStringb

Its crashing here.Isnt it??

This is because this is the only insertion when it will be inserted at the end.so when
you try to do:

temp->next=r;

temp is NULL,so setting its NEXT pointer is accessing unallocated memory which might be causing the seg fault.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970458
Some times it works and sometimes it does not. What does that mean?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970464
>It is working but ridiculous. Here is my sample output:

What was ridiculous about the output?
I thought the output was correct.

>Gurukul
>JOTIRGAMAYAStringaStringbx&#9786;=
                        ^         ^          ^
Are you talikng about these?These must be the printfs that you've inserted.These were for debugging purposes only.Remove them.


0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970471
Almost exhausted:
temp=(struct node *)malloc(sizeof(struct node));

thats what i did before temp->next=r; and yes this time it is working wihout any problems and I inserted almost 11 to 15 elements without any problem.

So that was it or am I missing something else?

Everything is fine now and a great thanks to ANKURATVB and also to PCableGuy.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970481

   /*traverse the list*/
    while(temp!=NULL)
    {
            if((strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
                ||temp->next==NULL)
            {
                            r->next=temp->next;
                            temp->next=r;
                            return;
            }

              temp=temp->next;
    }
    r->next=NULL; //these 2 lines will never be executed.
    temp->next=r; //not even for the last node.

>strcmp(temp->next->name,scname)>0)
if temp->next is NULL,then trying to access temp->next->name may cause a seg fault.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970485
>>>Gurukul
>JOTIRGAMAYAStringaStringbx&#9786;=
                       ^         ^          ^

Lol.........No it wasnt about those extra printfs. I said ridiculous because I was able to insert mor elements without any problem and not doing any changes.
Anyway everything looks fine now. I have to still implement
remove school
search for district functions

I will probably continue with them tomorrow.Till then i wont close this question. What do you think ankuratvb? Should I close this question and start a new thread again if i get any problems while implementing the two leftover functions. Can you stay with this thread for some more time? I am feeling too sleepy. So lets continue it tommrrow.Thanks^ALOT.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970492
I say that those 2 lines wont be executed because for the last node,temp->next=NULL.
In your if condition,

>      if((strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
>           ||temp->next==NULL) //this part will be true and since its an OR,if will eval. to true

It will work OK.
because,
r->next=temp->next;
//r->next will be set to NULL.
temp->next=r;
//temp->next will now point to r
return;

the problem may be in strcmp(temp->next->name,scname).

0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970494
>>>strcmp(temp->next->name,scname)>0)
if temp->next is NULL,then trying to access temp->next->name may cause a seg fault.

So where exactly am I going to allocate space for temp. Please reply ASAP.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970501
You dont need to allocate memory for temp.
You're using temp only to point to already allocated nodes.

You just need to check that you dont read temp->next->name when temp->next=NULL.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970502
i guess the boolean within if is handled properly and there should be no pointer problems in future and specialy seg fault problems in future.

"No of times a program can crash is in direct proportion to no of pointers in it."
-- Author Yashwant Kanetkar Book:Pointers In C.

Lol.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970511
>>
I will probably continue with them tomorrow.Till then i wont close this question. What do you think ankuratvb? Should I close this question and start a new thread again if i get any problems while implementing the two leftover functions. Can you stay with this thread for some more time? I am feeling too sleepy. So lets continue it tommrrow.Thanks^ALOT
<<

My opinion,open a new thread for the other functions. :~)

That stands us experts to gain more points. :~))

After all,according to the rules,you're supposed to ask only one question in one thread.

Its up to you now.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10970519
Anyway you are right. My program crashed again. Is there anything wrong in allocating memory for temp?
>>You just need to check that you dont read temp->next->name when temp->next=NULL.
How exactly do I proceed in that direction?

I tried if(temp->next!=NULL) but that does not work. This is so annoying.

>>After all,according to the rules,you're supposed to ask only one question in one thread.


Right I will open new thread for other two functions but atleast let me finish this first function without any problems. C is so weird:(.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10970537
You can safely comment/remove those 2 lines.They never get executed.
Separate the 2 conditions.

Try this:
       while(temp!=NULL)
       {
             if(temp->next==NULL)
             {
                         r->next=temp->next;
                         temp->next=r;
                         return;
             }
             if(strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
             {
                         r->next=temp->next;
                         temp->next=r;
                         return;
             }
               
             temp=temp->next;
       }
//       r->next=NULL;
//       temp->next=r;
   }
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10971278
Hi b_vishwajit,

I tested your program on my linux box and it turns out,i was right.

I got the seg fault just as you did,the moment you insert a node at the end of the list after the second node.

Just separate the 2 if conditions to make it work.

The code for insertSchool():

void insertSchool(int index,char* scname)
{
   struct node *q,*r,*temp;

   temp=q=distArray[index];

   r=(struct node *)malloc(sizeof(struct node));
   strcpy(r->name,scname);

   /*if list is empty*/
   if(q==NULL||strcmp(q->name,scname)>0)
   {
       q=r;
       q->next=temp;
       distArray[index]=q;
   }
   else
   {
       /*traverse the list*/
       while(temp!=NULL)
       {
             if(temp->next==NULL)
             {
                         r->next=temp->next;
                         temp->next=r;
                         return;
             }
             if(strcmp(temp->name,scname)<0&&strcmp(temp->next->name,scname)>0)
             {
                         r->next=temp->next;
                         temp->next=r;
                         return;
             }
               
             temp=temp->next;
       }
//       r->next=NULL;
//       temp->next=r;
   }
}
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 480 total points
ID: 10971286
The explanation is that now we check for temp->next=NULL before we do the temp->next->name.

If temp->next=NULL,we dont execute the temp->next->name part because the function returns before it gets to the second if condition.

0
 
LVL 2

Expert Comment

by:Avik77
ID: 10972603
I sorry but I may be referring to a point that may have already been discussed and dealt with by ankuratvb ..... tired of going through this :-)
But the point is, did u initialize all the entries of ur hash table to NULL ? If yes, then how? If no, then do that immediately ...
for(i=0;i<MAX_DISTRICTS;i++){ distarray[i]=NULL; }

If I am wrong at understanding please correct me (anyone) .

Avik.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10973288
Avik you have mentioned a good point to be taken care of. But I think ankuratvb already  told that. So I just added the line
distArray[MAX_DISTRICTS={NULL}; and hence all array elements are by default initialised to NULL. There is no need for a for loop.Actualy it does the same thing but consumes more CPU runtime.

Ankuratvb that was great. You are C guru man!!!!It works without any problems now.But why did you comment out
r->next= NULL; . Is'nt it true that last element in a LL  points to NULL.
Thanks I am closing this question now and continue this in another thread.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10973320
Points for ankuratvb:
http://oldlook.experts-exchange.com:8080/Programming/Q_20974857.html

Ankur just post a comment there I want to give you those points for your help since you deserved a perfect 1000 actualy.
Here is the pointer to continuation of this discussion:
http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/C/Q_20975301.html
Thanks a lot.
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 10974046
>>Points for ankuratvb:
http://oldlook.experts-exchange.com:8080/Programming/Q_20974857.html


I am sorry actually and EE page editor asked me to delete that question.Thanks.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10974304
>But why did you comment out
>r->next= NULL; . Is'nt it true that last element in a LL  points to NULL.

Yes.But that is done here.
           if(temp->next==NULL)
           {
                     r->next=temp->next; //temp->next=NULL so r->next is set to NULL
                     temp->next=r; //last node's next pointer now points to this new node
                     return;             //return.

Since you return back from here only,those 2 lines would never get executed.
If its the las node,this if condition is satisfied,otherwise the other if condition is satisfied and there is a return there as well.

           
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now