0

I have below program for shared memory access in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHMSZ 27

int main() {
    char c;
    int shmid;
    key_t key;
    char *shm, *s;

    key = 5678;
    /* Create the segment */
    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        exit(1);
    }

    /* Attach the segment */
    if ( (shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

    /* Put things into memory for other processes to read*/
    s = shm;
    for (c = 'a'; c <= 'z'; c++) {
        *s++ = c;
    }
    *s = NULL; 

    /*Wait till other processes read and change the memory*/
    while (*shm != '*') {
           sleep(1);
    }

    exit(0);
}

In this program I am getting error for below line

   *s = NULL;

Error:

shm_server.c: In function ‘main’:
shm_server.c:36:8: warning: assignment makes integer from pointer without a cast [enabled by default]

Why am I receiving this error?

Note: I also have a other script as shm_client.c to read the memory while the server program shm_server.c waits till client program changes the first character to '*'

1 Answers1

1

If you want to write 'null' NOT 'NULL' at the end of data then do this:

*s = '\0'; 

OR

*s  = 0x00; 
Vagish
  • 2,520
  • 19
  • 32
  • wont that NULL complete previous assignment done in while. I just want to add NULL as last element –  Feb 13 '15 at 11:10
  • ok now it works. With the first one I was getting Segmentation fault. Since I have a other prog as shm_client.c to read the memory while the server script waits till shared memory is changed. –  Feb 13 '15 at 11:17
  • Good that its working for you,If you do not have any more issues accept the answer. – Vagish Feb 13 '15 at 11:19
  • It is better explained here: http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0 – Vagish Feb 13 '15 at 11:23
  • 1
    `NULL` is not an object. And (outside POSIX) not necessarily of pointer type. `#define NULL 0` is valid (and would have made the code compile). – mafso Feb 13 '15 at 11:49