Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:46:01+00:00 2026-06-11T05:46:01+00:00

I have one problem with this code. I should create one structure and share

  • 0

I have one problem with this code. I should create one structure and share it across 5 new process created from the father:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <time.h>

struct compartido {
    int pid1, pid2, pid3, pid4, pid5;   
    int propietario;            
    int contador;                   
    int pidpadre;
};

struct compartido var;  


int main(int argc, char *argv[]) {

    key_t llave1,llavesem;
    int idmem,idsem;    
    llave1=ftok("/tmp",'a');

    idmem=shmget(llave1,sizeof(int),IPC_CREAT|0600);
    if (idmem==-1) {
       perror ("shmget");
       return 1;
    }

var=shmat(idmem,0,0); /*This line is giving the error*/

/*rest of the code*/
}

The exact error is giving is:
error: incompatible types when assigning to type ‘struct compartido’ from type ‘void *’

I need to put this structure in the shared variable to be able to see and modify all those data from the 6 process (5 children and the father).

What I’m doing bad? Thanks in advance and best regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:46:03+00:00Added an answer on June 11, 2026 at 5:46 am

    You probably should declare a pointer

     struct compartido* var; 
    

    then initialize it with

     var= (struct compartido*) shmat(idmem,0,0); 
    

    You should handle the error case

     if ((void*)var == (void*)-1) {
        perror("shmat failed");
        exit(1);
     }
    

    since shmat(2) is (on Linux) a syscall which can fail.

    At last, use pointer field notation to access the fields, e.g. var->propietario or var->pidpatre etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm training code problems like UvA and I have this one in which I
This is my problem, I have one textbox, one button and one label. Everything
I really have problem with this one. So I have a jar with a
My problem is this one, I am using Sharepoint 2010, I have a form
So i'm having this problem. I have two tables (Oracle), one is called Destination
This is a thought problem, and one I have been wrestling with for about
I have a problem with my WCF Data Service, and this one is just
i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created
I have one problem with parsing *.docx document with OpenXML (C#). So, here's my
I have one problem with Javascript Nodes.I want to find out what button was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.