LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-09-2004, 12:52 AM   #1
suwandy_chandra
Member
 
Registered: Mar 2004
Location: Australia
Distribution: Debian & Mandrake
Posts: 44

Rep: Reputation: 15
Inserting element to a list of list in C


I have a code which look like this,
Code:
     struct list1 {
         char *string;
         struct list1 *next;
     };
     struct list2 {
          struct list1 *first;
          struct list2 *next;
     };
and I wanted to use the second list in one my function, which is list_insert, which goes like
Code:
     struct list2 * list_insert (struct list2 *new, struct list1 *input) {
     struct list2 *temp;
   
         if (new == NULL) {
             new->first = input;  // ===> line of error
             new->next= NULL;
         }
         else {
            // some more codes
         }
However, as it turns out, after it compiles correctly, and I run it with one of my testcases, it gives me and error saying Bounds error : NULL or ILLEGAL pointer used in pointer arithmetic. I have malloc'ed the struct list2 *new in my main function. What's wrong with that? Do I have to malloc the struct list1 *first as well?

Any help will be very appreciated

Last edited by suwandy_chandra; 03-09-2004 at 12:53 AM.
 
Old 03-09-2004, 02:39 AM   #2
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Re: Inserting element to a list of list in C

Quote:
Originally posted by suwandy_chandra
Code:
     struct list2 * list_insert (struct list2 *new, struct list1 *input) {
     struct list2 *temp;
   
[1]      if (new == NULL) {
[2]          new->first = input;  // ===> line of error
[3]          new->next= NULL;
         }
         else {
            // some more codes
         }
It might be a coding error or a misunderstanding, but at [1] you are testing if `new' is NULL. If this returns a true value, then `new' is NULL and you cannot dereference and assign to it as you have done in [2] and [3]. The actual pointer arithmetic problem is because that is the way the structure dereferences are being calculated. My guess would be that you are not passing the structure you allocated to the function properly. There would not be an error in the case you have shown if `input' is NULL, as it would just be a NULL assignment which is acceptable -- however, you will need to allocate it somewhere if you want to use it ( though it does not have to be malloc(3), necessarily ).

One question, did you possibly mean to use `test' inside that block? If you did, that makes more sense. You will need to dynamically allocate it though, using malloc(3).
 
Old 03-09-2004, 03:08 AM   #3
suwandy_chandra
Member
 
Registered: Mar 2004
Location: Australia
Distribution: Debian & Mandrake
Posts: 44

Original Poster
Rep: Reputation: 15
Thanks, I found the error, which is as what you said.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not init font path element unix/:7100, removing from list! jeffreybluml Fedora 18 10-12-2011 08:06 AM
Need Recommendation for Debian Etch sources.list ( /etc/apt/sources.list ) forgox Debian 6 05-05-2007 01:57 PM
"Could not init font path element""Unix /: 7100 removing from list/ zameer_india Linux - Networking 7 07-03-2006 06:11 AM
get list of latest list of packages bobwall Linux - Distributions 1 11-30-2004 03:48 PM
wireless channel list different from router's list heluani Linux - Laptop and Netbook 1 08-29-2004 10:04 PM


All times are GMT -5. The time now is 04:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration