Question

I need to add to the shared memory string from arguments (ex. ./a.out abcxyz). I wrote the code, but it don't add string or don't show me string. What is the reason?

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

    int shmid;
    char *buf;

        shmid = shmget(KEY, 5, IPC_CREAT | 0600);
        buf = (char *)shmat(shmid, NULL, 0);

        *buf = argv[1];

    printf("\n%c\n",  buf);

    return 0;
}
Était-ce utile?

La solution

You're copying a string, so you can't just use assignment - you need strcpy:

#include <string.h>

...


strcpy(buf, argv[1]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top