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;
}
有帮助吗?

解决方案

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

#include <string.h>

...


strcpy(buf, argv[1]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top