You're trying to pass a pointer to data that is not in
the child's address space? Won't work. Try using
shared memory, if that's what you want.

If you want to pass data over a pipe or socket, you
have to serialize it first. 

HTH,
Brian

--- James Matthew Miraflor <[EMAIL PROTECTED]>
wrote:

> I just want to ask, can I pass a pointer to a
> structure using pipes in
> C. I really want to pass the pointer to a head of a
> linked list, but I
> get nothing. The code is:
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/wait.h>
> #include <unistd.h>
> 
> typedef struct {
>         int whoami;
>         int data;
>         struct datagram *next;
>         struct datagram *prev;
> } datagram;
> 
> void parent_to_child(int input[2], int output[2]) {
>         datagram *head, *trav, *temp;
>         int i=1, num_input;
> 
>         close(input[1]);
>         close(output[0]);
> 
>         /* Receive input from console and create the
> node associated */
>         do {
>                 printf("\nInput number %d : ",i);
>                 scanf("%d",&num_input);
>                 temp =
> (datagram*)malloc(sizeof(datagram));
>                 temp->whoami = i;
>                 temp->data = num_input;
>                 temp->next = temp->prev = NULL;
>                 i++;
>                 /* Passes to child created node */
>                
> write(output[1],(void*)&temp,sizeof(datagram*));
>         } while(num_input>0);
> 
>         /* Receives head node of the list */
>        
> read(input[0],(void*)&head,sizeof(datagram*));
> 
>         /* Traverses and prints the elements of the
> list */
>        
> for(trav=head,i=1;trav!=NULL;trav=trav->next,i++) {
>                 printf("\nNumber %d is %d, which has
> been inputed
> %dth.",i,trav->data,trav->whoami);
>         }
> 
>         close(input[0]);
>         close(output[1]);
>         exit(0);
> }
> 
> void child_to_parent(int input[2], int output[2]) {
>         datagram *head, *tail, *temp, *trav, *mark;
>         int i;
> 
>         close(input[1]);
>         close(output[0]);
> 
>         /* Sentinel node */
>         head = (datagram*)malloc(sizeof(datagram));
>         head->whoami = -1;
>         head->data = -1;
>         head->next = NULL;
>         tail = head;
> 
>         /* Receives node and adds to list until
> encountered data zero */
>         while(1) {
>                
> read(input[0],(void*)&temp,sizeof(datagram*));
>                 printf("temp->whoami = %d,
> temp->data =
> %d",temp->whoami,temp->data);
>                 tail->next = temp;
>                 temp->prev = tail;
>                 tail = tail->next;
>         }
> 
>        
> write(output[1],(void*)&head,sizeof(datagram*));
> 
>         close(input[0]);
>         close(output[1]);
>         exit(0);
> }
> 
> int main() {
>         pid_t child_pid;
>         int child_status, pipe1[2], pipe2[2],
> piperet = 0;
> 
>         piperet += pipe(pipe1);
>         piperet += pipe(pipe2);
>         if(piperet<0) {
>                 perror("pipe");
>                 exit(1);
>         }
> 
>         child_pid = fork();
>         switch(child_pid) {
>                 case -1 : {
>                         perror("fork");
>                         exit(1);
>                 }
>                 case 0 : {
>                        
> child_to_parent(pipe2,pipe1);
>                 }
>                 default : {
>                        
> parent_to_child(pipe1,pipe2);
>                 }
>         }
> }
> 
> Can anyone tell me what is wrong? I would appreciate
> any response.
> 
> -- 
> James Matthew B. Miraflor
> 2002-17119
> BS Computer Science
> University of the Philippines
> Manila
> 
> (+63)02-2876007
> +639197101941
> Block 49 Lot 29 Phase 3 E2
> Dagat-dagatan, Malabon City
> Philippines
>
___________________________________________________________
> 
> "(One) wins his battles by making no mistakes.
> Making no mistakes 
> is what establishes the certainty of victory, for it
> means conquering 
> an enemy that has already been defeated."
> 
> Sun Tzu
> The Art of War 
>
____________________________________________________________
> --
> Philippine Linux Users' Group (PLUG) Mailing List
> [email protected] (#PLUG @ irc.free.net.ph)
> Official Website: http://plug.linux.org.ph
> Searchable Archives: http://marc.free.net.ph
> .
> To leave, go to
> http://lists.q-linux.com/mailman/listinfo/plug
> .
> Are you a Linux newbie? To join the newbie list, go
> to
>
http://lists.q-linux.com/mailman/listinfo/ph-linux-newbie
> 

--
Philippine Linux Users' Group (PLUG) Mailing List
[email protected] (#PLUG @ irc.free.net.ph)
Official Website: http://plug.linux.org.ph
Searchable Archives: http://marc.free.net.ph
.
To leave, go to http://lists.q-linux.com/mailman/listinfo/plug
.
Are you a Linux newbie? To join the newbie list, go to
http://lists.q-linux.com/mailman/listinfo/ph-linux-newbie

Reply via email to