我想要一个成员变量,这是一个双指针.对象,指向的双指针不得从类外部修改.
我的以下尝试产生 "从'std :: string**'到'const std :: string**'的无效转换"
class C{
public:
const std::string **getPrivate(){
return myPrivate;
}
private:
std::string **myPrivate;
};
Run Code Online (Sandbox Code Playgroud)
std::string *myPrivate如何返回只读双指针?
做一个明确的演员表是不错的风格return (const std::string**) myPrivate?
我试图从指向数组的双指针获取2D数组的行数和列数.
#include <stdio.h>
#include <stdlib.h>
void get_details(int **a)
{
int row = ??? // how get no. of rows
int column = ??? // how get no. of columns
printf("\n\n%d - %d", row,column);
}
Run Code Online (Sandbox Code Playgroud)
上面的功能需要打印大小的细节,哪里出错了.
int main(int argc, char *argv[])
{
int n = atoi(argv[1]),i,j;
int **a =(int **)malloc(n*sizeof(int *)); // using a double pointer
for(i=0;i<n;i++)
a[i] = (int *)malloc(n*sizeof(int));
printf("\nEnter %d Elements",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("\nEnter Element %dx%d : ",i,j);
scanf("%d",&a[i][j]);
}
get_details(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用malloc来创建数组.
如果我使用这样的东西怎么办?
column …
想象一下,你有一个如下定义的类.
public class SomeClass
{
public Manager m { get; protected set; }
public SpecialData data { get; protected set; }
//More methods and member code here...
}
Run Code Online (Sandbox Code Playgroud)
我需要我的经理类以某种方式能够更改SpecialData成员的集合引用.我可以使用C++中的双指针或朋友类来完成此操作,但遗憾的是,该选项在C#中不可用.如何保持SpecialData不受外部用户设置,同时仍允许Manager类控制设置?我可以用内部关键字做到这一点,但这似乎并不是非常安全或干净......
任何帮助是极大的赞赏.
我正在使用结构实现链表。我有一个结构-
typedef struct llist node;
typedef node *nodeptr;
struct llist
{
int data;
nodeptr next;
};
Run Code Online (Sandbox Code Playgroud)
现在假设我声明了一个变量nodeptr *ptr;。我如何访问成员data和next使用ptr?
我在一些书/教程中看到过这个。
当您将(链表的)头指针传递给函数时,您需要将其作为双指针传递。
例如: // 这是反向链表,其中 head 指向第一个节点。
void nReverse(digit **head)
{
digit *prev=NULL;
digit *curr=*head;
digit *next;
while(curr!=NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
*head=prev;
return;
}
Run Code Online (Sandbox Code Playgroud)
这工作正常。
当我使用单指针时它也有效,
void nReverse(digit *head)
{
digit *prev=NULL;
digit *curr=head;
digit *next;
while(curr!=NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
head=prev;
return;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用头指针打印列表。这两个功能都可以正常工作。
我错过了什么吗?
谢谢,
请找到如下所示的代码段:
#include <stdio.h>
int My_func(int **);
int main()
{
int a =5;
int *p = &a;
My_Func(&p);
printf("The val of *p is %d\n,*p);
}
void My_Func(int **p)
{
int val = 100;
int *Ptr = &val;
*p = Ptr;
}
Run Code Online (Sandbox Code Playgroud)
如何通过在my_Func函数中使用双指针作为参数并使值的更改在main函数中反映相同但如果我们使用单个指针My_Func并不改变main中的值?请尽可能用示例解释我
高级感谢
Maddy
所以这段static_cast代码完全合法:
int n = 13;
void* pn = static_cast<void*>(&n);
void** ppn = &pn;
Run Code Online (Sandbox Code Playgroud)
然而,这必须编成一个reinterpret_cast编译:
int n = 13;
int* foo = &n;
void** bar = static_cast<void**>(&foo);
Run Code Online (Sandbox Code Playgroud)
如果我不改变它我得到错误:
错误C2440 ::
static_cast无法转换int **为void **注释:指向的类型不相关; 转换需要reinterpret_cast,C风格的演员或功能风格演员
所以我认为问题是"类型无关".然而,我还是不明白,如果它从去时是确定int*对void*他们怎么能不相关的int**和void**?
c++ void-pointers double-pointer static-cast reinterpret-cast
int main(int argc, char **argv)
{
int fd;
int i;
char *line;
if (!(fd = open(argv[1], O_RDWR | O_CREAT)))
{
printf("Error in open\n");
return (0);
}
while ((i = get_next_line(fd, &line)) > 0)
{
printf("%i-%s\n",i, line);
free(line);
}
printf("%i-%s",i, line);
free(line);
}
Run Code Online (Sandbox Code Playgroud)
int main(int argc, char **argv)
{
int fd;
int i;
char **line;
if (!(fd = open(argv[1], O_RDWR | O_CREAT)))
{
printf("Error in open\n");
return (0);
}
while ((i = get_next_line(fd, line)) > 0)
{
printf("%i-%s\n",i, *line); …Run Code Online (Sandbox Code Playgroud) 我正在阅读 C 中的 OOP,并且有这个标题:
#ifndef _NEW_H
#define _NEW_H
#include <stdarg.h>
#include <stddef.h>
#include <assert.h>
void *new (const void *type, ...);
void delete (void *item);
typedef struct
{
size_t size;
void *(*ctor)(void *self, va_list *app);
void *(*dtor)(void *self);
void *(*clone)(const void *self);
int (*differ)(const void *self, const void *x);
} class_t;
inline void *new (const void *_class, ...)
{
const class_t *class = _class;
void *p = calloc(1, class->size);
assert(p);
*(const class_t **)p = class; //why would you cast to double …Run Code Online (Sandbox Code Playgroud) 在使用 GDB 进行调试时,我注意到一些非常奇怪的事情。即, . 运算符在打印语句中使用时,可以像 -> 运算符一样取消引用单个甚至双指针以获取结构的字段。
这是一个简单的例子:
#include <stdio.h>
typedef struct node {
struct node *next;
int data;
} Node;
int main()
{
Node head, *head_p, **head_pp;
head.next = 0UL;
head.data = 42;
head_p = &head;
head_pp = &head_p;
// **GDB PAUSED HERE**
printf("head.data: %d\n", head.data);
// These won't compile because the . operator is misused
//
// printf("head_p.data: %d\n", head_p.data);
// printf("head_pp.data: %d\n", head_pp.data);
}
Run Code Online (Sandbox Code Playgroud)
这是 GDB 在暂停时打印的时间printf:
怎么可能。运算符(在 GDB 打印语句中)取消引用这样的指针??
谢谢!
double-pointer ×10
c ×7
c++ ×3
pointers ×3
linked-list ×2
oop ×2
c# ×1
class ×1
const ×1
friend ×1
gdb ×1
reverse ×1
static-cast ×1