Sponsored Content
Full Discussion: Problem with linked lists
Top Forums Programming Problem with linked lists Post 302276724 by brinch on Wednesday 14th of January 2009 12:58:31 PM
Old 01-14-2009
Problem with linked lists

I am working on a problem for which I need to use a linked list of a sort. For this particular application I need each node to refer to a set of other nodes.
A simplified version of the code looks as follows:
Code:
#include <stdio.h>
#include <stdlib.h>

struct record {
  int id;
  struct record **friends;
};

int main(){
    int i;
    struct record *data;

/* This uncommented piece of code works as it is supposed to */
//  data=malloc(3*sizeof(struct record));
//  data[0].friends=malloc(2*sizeof(struct record));    
//  for(i=0;i<3;i++) data[i].id=i;
//  data[0].friends[0]=&data[1];
//  printf("%d %d\n", data[0].id, data[0].friends[0]->id);  

    subroutine(&data);
}

void subroutine(struct record **data){
    int i;
    
/* This piece of code produces a wrong output */
    *data=malloc(3*sizeof(struct record));
    (*data)[0].friends=malloc(2*sizeof(struct record));
    for(i=0;i<3;i++) (*data)[i].id=i;
    (*data)[0].friends[0]=data[1];
    printf("%d %d\n", (*data)[0].id, (*data)[0].friends[0]->id);  
}

A successful run results in the printing of "0 1". The uncommented code in main() gives the correct result, but if I pass the data to the subroutine, I get something weird. Can anybody help me?
 

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

2. Programming

this code for addind polynomials using linked lists showed segmentation error..any help pls..

the error occurs in the function() "add" used... #include<stdio.h> #include<malloc.h> struct node { int exp; int coef; struct node * link; }; struct node * create_list(struct node *,int,int); void display(struct node *); struct node * add(struct node *,struct node *); ... (3 Replies)
Discussion started by: mscoder
3 Replies

3. UNIX for Dummies Questions & Answers

Linked Servers

Hi, We have 2 UNIX Servers, say test1 and test2. Here, if I create a file or folder/delete a file or folder in the 1st server, it gets reflected automatically in the 2nd server. I don't think any links are established between these 2 servers. Both these have 2 different hostnames. How... (1 Reply)
Discussion started by: Dev_Dev
1 Replies

4. Programming

unidirectional linked list pointer problem

I am trying to test some operations on a directed list. However, the declaration of a pointer is giving me trouble. I seem to have done something incorrectly because I get an error: "listtest.c:29: warning: 'p' may be used uninitialized in this function" Can anyone help? This is my code... (6 Replies)
Discussion started by: bluetxxth
6 Replies

5. Programming

Help with linked list.

#include<stdio.h> #include<stdlib.h> struct LinkedList { int val; struct LinkedList *next; }node; /*Creating a structure variable*/ typedef struct LinkedList Node; Node *start = NULL; int create(int i) { Node *temp = NULL; if (start == NULL) ... (5 Replies)
Discussion started by: prinsh
5 Replies
XML::LibXML::NodeList(3)				User Contributed Perl Documentation				  XML::LibXML::NodeList(3)

NAME
XML::LibXML::NodeList - a list of XML document nodes DESCRIPTION
An XML::LibXML::NodeList object contains an ordered list of nodes, as detailed by the W3C DOM documentation of Node Lists. SYNOPSIS
my $results = $dom->findnodes('//somepath'); foreach my $context ($results->get_nodelist) { my $newresults = $context->findnodes('./other/element'); ... } API
new(@nodes) You will almost never have to create a new NodeList object, as it is all done for you by XPath. get_nodelist() Returns a list of nodes, the contents of the node list, as a perl list. string_value() Returns the string-value of the first node in the list. See the XPath specification for what "string-value" means. to_literal() Returns the concatenation of all the string-values of all the nodes in the list. to_literal_delimited($separator) Returns the concatenation of all the string-values of all the nodes in the list, delimited by the specified separator. to_literal_list() Returns all the string-values of all the nodes in the list as a perl list. get_node($pos) Returns the node at $pos. The node position in XPath is based at 1, not 0. size() Returns the number of nodes in the NodeList. pop() Equivalent to perl's pop function. push(@nodes) Equivalent to perl's push function. append($nodelist) Given a nodelist, appends the list of nodes in $nodelist to the end of the current list. shift() Equivalent to perl's shift function. unshift(@nodes) Equivalent to perl's unshift function. prepend($nodelist) Given a nodelist, prepends the list of nodes in $nodelist to the front of the current list. map($coderef) Equivalent to perl's map function. grep($coderef) Equivalent to perl's grep function. sort($coderef) Equivalent to perl's sort function. Caveat: Perl's magic $a and $b variables are not available in $coderef. Instead the two terms are passed to the coderef as arguments. reverse() Equivalent to perl's reverse function. foreach($coderef) Inspired by perl's foreach loop. Executes the coderef on each item in the list. Similar to "map", but instead of returning the list of values returned by $coderef, returns the original NodeList. reduce($coderef, $init) Equivalent to List::Util's reduce function. $init is optional and provides an initial value for the reduction. Caveat: Perl's magic $a and $b variables are not available in $coderef. Instead the two terms are passed to the coderef as arguments. perl v5.18.2 2014-02-01 XML::LibXML::NodeList(3)
All times are GMT -4. The time now is 10:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy