Home > Enterprise >  Passing argument 3 of ‘fgets’ from incompatible pointer type [enabled by default]
Passing argument 3 of ‘fgets’ from incompatible pointer type [enabled by default]

Time:03-26

I am a complete rookie to programming in C and have been trying to program a system that will take an integer input, perform a calculation, and tack them onto a string that will then be passed to a shared memory. Apologies if I am being an idiot but I am getting an error about an incompatible pointer type. I dont know how I can fix this error. Edit: I apologize for the bad initial question. Full code is included

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    /* The size (in bytes) of shared-memory object */
    const int SIZE = 4096;

    /* The name of shared-memory object */
    const char *Obj = "Shm";

    /* The shared-memory file descriptor */
    int shm_fd;

    /* The pointer to shared-memory object */
    void *ptr;

    /* Create the shared-memory object */
    shm_fd = shm_open(Obj, O_CREAT | O_RDWR, 0666);

    /* Configure the size of the shared-memory object */
    ftruncate(shm_fd, SIZE);

    /* Map the shared-memory object in the address space of the process */
    ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

    if (ptr == MAP_FAILED) 
    {
       printf("Map failed\n");
       return -1;
    }

    int cal;
    char newStr[200];
    char currentStr[200];
    char *temp;
    char value;

    printf("Enter an integer");
    scanf("%d", &cal);


    /* Create a message and write it to the shared-memory object */
    /*fgets(ptr, SIZE, stdin);*/
    if (cal == 0) {
        printf("0 is not valid");
        return -1;
    }

    if (cal < 1) {
        printf("Please enter a positive int");
        return -1;
    }
    sprintf(newStr, "%d", cal);

    while (cal != 1) {
        if (cal % 2 == 0) {
            cal = cal / 2;
        }
        else {
            cal = 3 * cal   1;
        }
        value = cal   '0';
        sprintf(currentStr, " --- %d", value);
        strcat(newStr, currentStr);
    }
    fgets(ptr, SIZE, newStr);
    printf("Writing the message to the shared memory is done! \n");

    return 0;
}

Due to nature of my coding environment testing and figuring out the exact nature of errors is particularly difficult.

Edit: Here is the exact error message

Collatz-Producer.c:84:2: warning: passing argument 3 of ‘fgets’ from incompatible pointer type [enabled by default]

And I had cut out the section above with ptr since I was confident it worked, though here is the specifics of what ptr equals

void *ptr;
ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

CodePudding user response:

According to this documentation, the function fgets takes 3 parameters, in this order:

  1. a pointer to the memory buffer to write to
  2. the size of the memory buffer
  3. the FILE * stream to read from, for example stdin

The array newStr is not a FILE * stream. Therefore, it is not valid as a third parameter.

If you do not intend to read from a FILE * stream (such as stdin or a file opened with fopen), then you should not be using the function fgets.

  • Related