Sponsored Content
Full Discussion: Problem with linked lists
Top Forums Programming Problem with linked lists Post 302277010 by spirtle on Thursday 15th of January 2009 06:07:25 AM
Old 01-15-2009
This line is dodgy:
Code:
data[0].friends=malloc(2*sizeof(struct record));

record::friends is of type pointer to pointer to struct record, but you are allocating space for two structs, which isn't the same thing. You need friends to point to the memory you allocated.
Try this
Code:
#include <stdio.h>
#include <stdlib.h>

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

void subroutine(struct record **data){
    int i;
    *data=malloc(3*sizeof **data);
    struct record* friends = malloc(2*sizeof *friends);
    (*data)[0].friends=&friends;
    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);
}

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

/* This uncommented piece of code works as it is supposed to */
  data=malloc(3*sizeof *data);
  struct record* friends = malloc(2*sizeof *friends);
  data[0].friends=&friends;
  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);

  struct record *data1;
  subroutine(&data1);
  return EXIT_SUCCESS;
}

 

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
PX_PUT_RECORDN(3)					     Library Functions Manual						 PX_PUT_RECORDN(3)

NAME
PX_put_recordn -- Stores record in Paradox file at certain position SYNOPSIS
#include <paradox.h> int PX_put_recordn(pxdoc_t *pxdoc, char *data, int recpos) DESCRIPTION
Stores a record in the buffer data into a Paradox file at the given position recpos. The first position has number 0. The function will determine the data block for this record. If the position is after the last data block, then as many as needed empty data blocks will be added. If the position is in the middle of a data block and there is no record right before the position, then the position will be recal- culated to make sure the new record will be stored in the first free slot of the datablock. For example, if a datablock contains H records and the first call of PX_put_recordn wants to store a record at postion 11, then this will be corrected to position H. PX_put_recordn will also add an empty data block and store the record as the first record of the second data block. Storing a second record at position 11 again, will be executed without any position change. data must be constructed with PX_put_data_xxx(3) functions before. It is of the same structure as the record returned by PX_get_record(3) and PX_get_record2(3). RETURN VALUE
Returns the record position on success and -1 on failure. The first position has number 0. SEE ALSO
PX_get_record(3), PX_get_record2(3), PX_put_record(3), PX_put_data_byte(3), PX_put_data_short(3), PX_put_data_long(3), PX_put_data_dou- ble(3), PX_put_data_alpha(3) AUTHOR
This manual page was written by Uwe Steinmann uwe@steinmann.cx. PX_PUT_RECORDN(3)
All times are GMT -4. The time now is 04:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy