-1

I have the problem described in the headline above. These are my files and their code:

run.c:

[...]  // I think it's not relevant for the problem

declarations.h:

#ifndef DECLARATIONS_H
#define DECLARATIONS_H

#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <errno.h>

[...]    

struct  
    {
        int position;    
        int currentNumberOfMessages;    
        int numberOfProcesses;    
        char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
    } mySharedMemory_sct = {0, 0, 0, '0'};

struct mySharedMemory_sct *myShMem_ptr;  

[...]

#endif //DECLARATIONS_H

lib.h:

#ifndef LIB_H
#define LIB_H

#include "declarations.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <errno.h>

[...]

int init (int *argc, char **argv[])  
    {
        /**
         * map the shared memory into the process
         */
        if ((myShMem_ptr = mmap(NULL, sizeof(mySharedMemory_sct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
        MAP_FAILED)    
            {
                printf("Error: %s\n", strerror(errno));
                return EXIT_FAILURE;
            }

        /**
         * increment the number of running processes called by the 'run'-process
         */
        myShMem_ptr->numberOfProcesses += 1;     <------- ERROR

        [...]

        return EXIT_SUCCESS;
    }

[...]


#endif //LIB_H

For the line marked 'error' the compiler throws this error message:

"dereferencing pointer to incomplete type ‘struct mySharedMemory_sct’"

and highlights the '->' in "myShMem_ptr->numberOfProcesses += 1;" red as the problem.

I've read the other posts to this error message, but the problem causes were different (i think), so I haven't found a solution yet.

In advance: Thank you for your help!

a kind person
  • 329
  • 1
  • 6
  • 17

1 Answers1

1

You need to change

struct  
    {
        int position;    
        int currentNumberOfMessages;    
        int numberOfProcesses;    
        char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
    } mySharedMemory_sct = {0, 0, 0, '0'};

to

struct mySharedMemory_sct 
    {
        int position;    
        int currentNumberOfMessages;    
        int numberOfProcesses;    
        char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
    } mySharedMemory_sct = {0, 0, 0, '0'};

Let's have a look at a simpler case:

struct a {
    int x;
} b;

So what do we have here? We have declared a struct and given it the name a, so this makes it possible to declare instances of that struct with struct a <name>. What about b? Well, that is an example of such an instance.

So what does this mean?

struct a {
    int x;
} b = {0};

Well, it does certainly NOT mean that when you create an instance of struct a that the instance will have its x value initialized to 0. It only means that this is true for the very instance b.

You have not posted the complete code, but I suspect that this might do what you want:

struct mySharedMemory_sct {
        int position;    
        int currentNumberOfMessages;    
        int numberOfProcesses;    
        char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};

struct mySharedMemory_sct *myShMem_ptr = &mySharedMemory_sct;

An important thing to remember here is that mySharedMemory_sct and struct mySharedMemory_sct are two completely different things. mySharedMemory_sct is a variable with type struct mySharedMemory_sct. You can change their names independently of each other.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • Thank you for answering. My goal is that the struct pointer to the shared memory 'myShMem_ptr' is not a pointer to any kind of this truct type but to the exact variable that is initialised with '0's. In other words: I want that the shared memory is initialised with '0' and thought I could do that with "struct *myShMem_ptr". I want that the 'init'-function is called by every child process, so I can not initialise to '0' in this function, because it shall only be done once and then the 'init'-function shall increment by 1, so that the number of processes gets count. – a kind person Apr 20 '19 at 21:22
  • @akindperson Well, in my last code snippet, `myShMem_ptr` will point to that zero-initialized instance. But since both the instance and the pointer is declared as globals, the pointer does not seem too necessary. – klutt Apr 20 '19 at 21:33
  • Yes, you did that with "myShMem_ptr = &mySharedMemory_sct;" but this changes the address the pointer points to and with "myShMem_ptr = mmap(...);" this address changes. But my plan wasn't to get a specific address but a specific datatype. I thought it might be possible to make the pointer have the datatype "struct " instead of "struct ", so that 'myShMem_ptr' points to the shared memory to a '0'ed struct. Perhaps that's not possible. I'm no expert in that...thank you for your time – a kind person Apr 20 '19 at 21:45
  • @akindperson This seems like a completely different question. I suggest that you write a new question. Take your time to make it really clear, and also try to make a [mcve] that clearly demonstrates what you want. Also say what the code should do and what it does instead. – klutt Apr 20 '19 at 22:07
  • You are right, I will do that. Have a nice Easter. – a kind person Apr 20 '19 at 22:16