PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to fix " assignment makes pointer from integer without a cast [-Wint-conversion]"? C unix program

 April 28, 2022     c, shared-memory, unix, warnings     No comments   

Issue

I'm writing this 2 programs but I recive this warning: " assignment makes pointer from integer without a cast [-Wint-conversion]".

I'm trying to compile these programs to other Machines, but I recive same problem. What can I do?

//PROGRAM 1  (PRODUCTOR)

#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <stdio.h>

#define SHMEMSIZE 4096

int main()
{

    key_t key_mem=6868;
    key_t key_sem=3232;
    int id_mem;
    int id_sem;
    void *addr_mem;
    struct sembuf param[1];

    if(id_mem = shmget(key_mem,SHMEMSIZE,0) == -1)
    {
        printf("Errore1\n");
        return -1;
    }


    if(addr_mem =shmat(id_mem,NULL,0) == (void *)-1)
    {
        printf("Errore2\n");
        return -1;
    }

    if(id_sem = semget(key_sem,2,0)== -1)
    {   
        perror("Errore3\n");
        shmdt(addr_mem);
        return -1;
    }

    while(1)
    {
        param[0].sem_num=1;
        param[0].sem_op=-1;
        param[0].sem_flg=0;

        if(semop(id_sem,param,1)==-1)
        {
            printf("Errore6\n");
            return -1;
        }
        printf("Scrivi messaggio: ");
        if(scanf("%[^\n]", (char *)addr_mem) == 0)
             *((char *)addr_mem) = '\0';
        getc(stdin);

        param[0].sem_num=0;
        param[0].sem_op=1;
        param[0].sem_flg=0;
        if(semop(id_sem,param,1)==-1)
        {
            printf("Errore7\n");
            return -1;
        }
    }

    shmdt(addr_mem);

}






_____________________________________________________

//PROGRAM 2 (CONSUMER)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <stdio.h>
#define SHMEMSIZE 4096

union semun 
{
    int val;
    struct semid_ds *buf;
    unsigned short *array;
    struct seminfo *__buf;
};

int main()
{

    key_t key_mem=6868;
    key_t key_sem=3232;
    int id_mem;
    int id_sem;
    void *addr_mem;

    union semun arg;
    struct sembuf param[1];

    if(id_mem = shmget(key_mem,SHMEMSIZE,IPC_CREAT|0666) == -1)
    {
        printf("Errore1");
        return -1;
    }

    if(addr_mem =shmat(id_mem,NULL,0) == (void *)-1)
    {
        printf("Errore2");
        shmctl(id_mem, IPC_RMID, NULL);
        return -1;
    }

    if(id_sem = semget(key_sem,2,IPC_CREAT|0666)== -1)
    {   
        printf("Errore3");
        shmctl(id_mem, IPC_RMID, NULL);
        shmdt(addr_mem);
        return -1;
    }

    arg.val=0;
    if(semctl(id_sem,0,SETVAL,arg)==-1)
    {
        printf("Errore4");
        semctl(id_sem, -1, IPC_RMID,arg);
        shmctl(id_sem, IPC_RMID, NULL);
        shmdt(addr_mem);
        return -1;
    }
    arg.val=1;
    if(semctl(id_sem,1,SETVAL,arg)==-1)
    {
        printf("Errore5");
        semctl(id_sem, -1, IPC_RMID, arg);
        shmctl(id_sem, IPC_RMID, NULL);
        shmdt(addr_mem);
        return -1;
    }

    while(1)
    {
        param[0].sem_num=0;
        param[0].sem_op=-1;
        param[0].sem_flg=0;

        if(semop(id_sem,param,1)==-1)
        {
            printf("Errore6");
            return -1;
        }

        printf("Il messaggio scritto è: %s\n",(char *)addr_mem);
        param[0].sem_num=1;
        param[0].sem_op=1;
        param[0].sem_flg=0;
        if(semop(id_sem,param,1)==-1)
        {
            printf("Errore7");
            return -1;
        }
    }

}

My program should simply allow a producer process to write on shared memory and the consumer process to read shared memory.

I first receive this "warning" and after that if I try to run the producer I get a "Segmentation Fault". I absolutely can't understand what's going on.

I suppose that the shared memory is not really created, in fact if I delete the line * ((char *) addr_mem) = '0'; I do not get the "Segmentation Fault", and it is clear that the "Scanf" is not writing anything.


Solution

IMO if(id_mem = shmget(key_mem,SHMEMSIZE,IPC_CREAT|0666) == -1) is very bad style, exactly for the problem you have. You forgot to add parantheses around the assignment. You compare the result of shmget with -1 and assign this result to id_mem.

Change it to if((id_mem = shmget(key_mem,SHMEMSIZE,IPC_CREAT|0666)) == -1) or even better

id_mem = shmget(key_mem,SHMEMSIZE,IPC_CREAT|0666);
if (id_mem == -1)

The next ifs have the same problem.



Answered By - mch
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing