Link to home
Start Free TrialLog in
Avatar of b_vishwajit
b_vishwajit

asked on

error:integer from pointer

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.
Avatar of b_vishwajit
b_vishwajit

ASKER

variable <b>distArray</b> stores hashTable elements which are ints
>>passing arg 2 of `insertName' makes integer from pointer without a cast
sorry about the typo.
SOLUTION
Avatar of PCableGuy
PCableGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm. That was so silly. But string are char arrays right.
Yes, a string in C is an array of characters ending in a "null character."

Try char* instead of char
I already tried it before posting the question but in vain.
Did the compiler give the same error?
Another question:

Was this intended to be an array of strings?
char *name[MAX_CHARACTERS];
>>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.
PCableGuy how long do you plan to stay on EE?
Not sure....

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

Deal?
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.

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...
Does it happen "immediately after you hit the enter key" on keyboard?
>>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.
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?
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);

>>variable <b>distArray</b> stores hashTable elements which are ints
I gave wrong information. distArray is actualy an array of pointers of type node.
struct node *distArray[MAX_SIZE];//MAX_SIZE-> maximum number of elements in the hash table.
Hope that makes it clear.
>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.

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.
>>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.
so i just declared it to be a char array.
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;
    }
}

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.
Thats exactly what you've done. :~)
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.
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.
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.
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.
I think its  a pointer casting  problem.Any suggestions?
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;
   }
}
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.
ankur,
 It works only for two strings:(. Try to insert a third one.
Hi,
its working for multiple strings(>2) as well on my compiler.
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.

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;
Nope no good. I tried everything and the segmentation fault is chewing my wits.
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.
I am still struck at the same point. Segmentation fault:(.
It is an allocation problem. Still lookin at my code.......................
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");
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.
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;
  }
}
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.
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?
for gcc,you need to include stdlib.h for malloc().
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.
>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");
i have included all neccessary header files including stdlib.h and malloc.h :(
>>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.
>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.

-------------------------------------------------------------
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
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.
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:
>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.
Some times it works and sometimes it does not. What does that mean?
>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.


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.

   /*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.
>>>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.
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).

>>>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.
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.
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.
>>
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.
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:(.
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;
   }
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;
   }
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Avik Dasgupta
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.
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.
Points for ankuratvb:
http://oldlook.experts-exchange.com/questions/20974857/ATTENTION-500-point-question.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/questions/20975301/integer-from-pointer-Part2.html
Thanks a lot.
>>Points for ankuratvb:
http://oldlook.experts-exchange.com/questions/20974857/ATTENTION-500-point-question.html


I am sorry actually and EE page editor asked me to delete that question.Thanks.
>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.