Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印共享内存的C指针_C_Pointers_Char_Shared Memory - Fatal编程技术网

打印共享内存的C指针

打印共享内存的C指针,c,pointers,char,shared-memory,C,Pointers,Char,Shared Memory,我是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 iden

我是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); // ??

%d
告诉
printf
“我正在传递一个
int
。打印它。”


%s
告诉
printf
“我正在传递字符串的第一个字符的地址。从该地址获取字符并打印它们。”

不,如果取消引用指针,则会获得第一个字符的地址表达式
*p
p[0]
完全相等。也就是说,它是第一个元素的值,在您的例子中是单个
char
。看起来您可能需要退后一步,进一步研究基本的C指针用法。但是对于int*p指针,我们需要prinf(*p)?为什么不起作用?请阅读文档以了解。它明确地告诉您必须为
%s
%d
传递什么。。。。要知道C字符串的长度是可变的,所以不能像传递整数一样按值传递。因此,它们总是通过指向第一个字符的指针来使用。
printf(" Content of shared memory: %s\n" , *p); // ??