-2

I am trying to save a graph in the adjacency list representation and hence, I am using pointers for that purpose. It is not fruitful to write the whole code snippet, hence I am writing the code snippet of that part, which I am failing to implement.

Just for clarification, I'm using the current so as to be able to append a node at the end, and I don't need to traverse the whole list.

I'm not sure whether this way of assigning pointers works correct or not. Any subtle hint will be of great help. Many thanks !!

int n;

struct node{
    int i;
    struct node * next;
};

int main(){
    printf("Enter nodes");
    scanf("%d",&n);

    struct node *adjlist=(struct node*)calloc(n,sizeof(struct node));
    struct node *current=(struct node*)calloc(n,sizeof(struct node));

    struct node *temp=(struct node*)calloc(1,sizeof(struct node));
    temp->i=10;
    temp->next=0;

    /* PROBLEM IS HERE
    What I want to do is :
    (adjlist+n-2)=temp;
    But it reflects the following error :
    lvalue required as left operand of assignment       
    Hence, using the BELOW statement.*/

    adjlist[n-2]=*temp;
    current[n-2]=*temp;

    temp=(struct node*)calloc(1,sizeof(struct node));
    temp->i=20;
    temp->next=0;

    /* The same I want to implement here as well.*/
    current[n-2].next=temp;
    current[n-2]=*temp;

    return 0;
}

Update :
I am trying to print the linked list corresponding to the address (adjlist+n-2) i.e. the values 10 and 20 should get printed. But only the first value(10) is getting printed. The print function that I am using is :

void printlist(struct node * adjlist){
struct node *list=adjlist+n-2;
while((*list).next!=NULL){
printf ("\t%d",list->i);
list=list->next;}
printf ("\t%d",list->i);
}

Is it my print function that is creating blunder, or error lies somewhere else?

Gaurav
  • 398
  • 8
  • 23
  • What do you mean by "But it reflects error"? Something doesn't work right, you get a compiler error... what? – Carey Gregory Aug 11 '14 at 04:50
  • 1
    Why are you allocating memory for `current` and `temp`? I feel like you should be storing the data directly in the memory you allocated for `adjlist`. And please don't cast the return value from `calloc()`. – Filipe Gonçalves Aug 11 '14 at 04:52
  • @CareyGregory : The error being displayed is "lvalue required as left operand of assignment" – Gaurav Aug 11 '14 at 04:54
  • @FilipeGonçalves : In a nutshell, I am using all these variables to dynamically add nodes to a linked list. Is typecasting return value from calloc() redundant or erroneous ? – Gaurav Aug 11 '14 at 04:58
  • 1
    @Gaurav See http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Filipe Gonçalves Aug 11 '14 at 05:16
  • You are fighting a couple of problems. The first (and most important) is that you are NOT creating a linked-list. You create `n` pointers to `struct node` and allocate memory for them -- but that isn't how a linked list works. With a linked-list you `add` elements. When each element is *added* you create/allocate a new `struct node` and assign **the new pointer value** to `node-> next` and assign new `node-> next = NULL`. That provides the mechanism to allow you to iterate through the linked-list. Once you get iteration working, then printing can follow. – David C. Rankin Aug 11 '14 at 06:00
  • I want to create `n` linked lists, and hence, I'm creating `n` pointers to `struct node`. Is my implementation right for this purpose? – Gaurav Aug 11 '14 at 06:16

2 Answers2

1

(adjlist+n-2) is a constant expression. You cannot use it on the left side of an assignment statement. I think what you intended was *(adjlist+n-2).

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • No, I still intend to assign an address to the second last pointer location of the `adjlist` locations i.e. `(adjlist+n-2)` – Gaurav Aug 11 '14 at 05:03
  • What I suggested accomplishes exactly the thing you believe is a solution to your own problem; ie, `adjlist[n-2]=*temp;`. So what is it that you're trying to accomplish that I don't understand? – Carey Gregory Aug 11 '14 at 05:06
  • Now I don't understand what your question is. – Carey Gregory Aug 11 '14 at 05:41
0

Copying structure in C with assignment instead of memcpy()

As a side, you need to free temp before you allocate memory the second time or you will be leaking memory.

Community
  • 1
  • 1
shikamaru
  • 54
  • 4