如何shmat char var [] [],C

时间:2017-04-28 14:58:47

标签: c ipc shared-memory

我正在尝试将char shm[][] var附加到共享内存中,并且我有以下代码。

int main(){
    int shmid;
    key_t key;
    char shm[15][10];

    if ((key = ftok("test.c", 'R')) == -1) {
        perror("ftok");
        exit(1);
    }

    shmid=shmget(key, SHM_SIZE, IPC_CREAT | 0666);
    if(shmid<0){
        perror("shmget");
        exit(1);
    }


    shm=shmat(shmid, NULL, 0);
    if(shm==(void*)-1){ 
        perror("shmat");
        exit(1);
    }
}

尝试编译我收到以下编译错误:

error: assignment to expression with array type
     shm=shmat(shmid, NULL, 0);

我做错了吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

你想:

SELECT 'P', 
--coalesce-return the first nonnull expression, row_number-numbers all rows sequentially, over-defines a window
*[need to prepad here]* CAST(coalesce(xyz.MaxID, 0) as int) + row_number() over (order by(select NULL)) as theMax,
'P' + right('000000000'+ rtrim(CAST((CAST(coalesce(xyz.MaxID, 0) as int) + row_number() over (order by(select NULL))) as VARCHAR(50))), 9), 
T.ForeName, T.Middle_Name, T.Surname, '1234', 'n', GetDate(), GetDate(), 'SCT', '1', GetDate(), GetDate(), 'PGHERE', '1', 'n'
FROM INSERTED  i 
cross join 
--selecting max number between people or changes
(SELECT MAX(CAST(q1.Test as INT)) as MaxID FROM (SELECT MAX(CAST(PEOPLE_ID as INT)) as Test FROM pc.dbo.People WHERE CAST(PEOPLE_ID as int)<2000000 UNION ALL SELECT MAX(CAST(PEOPLE_ID as INT)) as Test FROM pc.dbo.Changes WHERE CAST(PEOPLE_ID as int)<2000000) q1) AS xyz
--used for testing since inserted table only exists in the context of the trigger when it's executing
CROSS APPLY dbo.Parser(i.Contact) T
WHERE i.Key1 = '23'
OR (i.Key1 = '45')
OR (i.Key1 = '67')

这是指向具有未知行数和10列的二维矩形数组的指针。

答案 1 :(得分:0)

char *shm[15][10]是一个数组类型,但void *shmat(int shmid, const void *shmaddr, int shmflg);返回指向内存的指针。

你应该做的是:

  1. char shm[15][10];更改为char *shm;,现在它是指向包含字符数组的内存位置的指针。
  2. (void *)添加演员到(char *)
  3. shm = (char *) shmat(shmid, NULL, 0);

    shmat(2) - Linux man page