我是 C 的新手,我有一个关于字符指针和共享内存以及它将打印什么的问题。看一看:
key_t ke =ftok("./exe","k");
int ds_shm=shmget(ke,100,IPC_CREAT|IP_ECXL|0644);
char *p;
if ( ds_shm<0) { //if the memory alredy exists
ds_shm=shmget(ke,100,0644); //get the identifier
p= (char*) shmat(ds_shm,NULL,0); //p cointain the address of the shm
} else { // if not texted create it
p= (char*) shmat(ds_shm,NULL,0); // p now contain the address
strncpy(p,"hello",sizeof("hello")); //init
}
现在,如果我想看我的教授所做的内容:
printf(" Content of shared memory: %s\n" , p);
但不应该使用 *
来访问变量的值吗?
printf(" Content of shared memory: %s\n" , *p); // ??
答案 0 :(得分:0)
%d
告诉 printf
“我正在传递一个 int
。打印出来。”
%s
告诉 printf
“我正在传递字符串第一个字符的地址。从该地址获取字符并打印出来。”