所以它似乎意味着a pointer to a constant pointer to char.那是它指向a char * const,到目前为止一直很好.
令我困惑的是我在哪里以及如何看待它.我正在查看手册页,qsort并且示例执行以下操作将指针转换为char **(字符串数组)的元素(指向元素的指针const void *)转换为可提供给的普通char指针strcmp:
static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么有演员char * const …
我正在使用C++中的结构编写一个简单的代码,但在使用g ++进行编译时,我遇到了核心转储错误.如果我使用cin在下面的player.name中输入"Mario",则不会报告错误.谁能解释一下出了什么问题?
struct character
{
char *name;
};
int main()
{
character player;
player.name = new char[10];
player.name = "Mario";
cout<<player.name<<endl;
delete player.name;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
早上好 - 这些都是我的面条烹饪了一点(同样,谷歌关于我注意到的c编程语言很难...)
void prepareFrames(PDouble prep){
int gap = prep->gap;
printf("Gap is %d\n", gap);
prep->intFrames = (PFrame*)malloc(gap*sizeof(PFrame));
PFrame copyFrame = prep->first;
int i;
for(i=0; i < gap; i++){
prep->intFrames[i] = (PFrame)malloc(sizeof(Frame));
copyFrame(prep->intFrames[i], prep->first, i); //LINE 189
}
}
void copyFrame(PFrame new, PFrame copy, int num){
new->sequence = copy->sequence;
new->evaluatedFrame = copy->evaluatedFrame + num;
new->numBoxes = copy->numBoxes;
new->boxes = (PBox*)malloc(copy->numBoxes*sizeof(PBox));
int i;
for(i=0; i < copy->numBoxes; i++){
PBox old = copy->boxes[i];
new->boxes[i] = (PBox)malloc(sizeof(Box));
copyBox(new->boxes[i], old);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
error: called …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码片段:
int *a;
int p = 10;
*(a+0) = 10;
*(a+1) = 11;
printf("%d\n", a[0]);
printf("%d\n", a[1]);
Run Code Online (Sandbox Code Playgroud)
现在,我希望它能打印出来
10
11
Run Code Online (Sandbox Code Playgroud)
但是,会出现一个窗口,显示program.exe已停止工作.如果我注释掉第二行代码int p = 10;然后再次调试代码就行了.
为什么会这样?(我想要做的是创建一个动态大小的数组.)
我有一个简单的问题,我似乎无法在网上找到.
我正在使用CUDA做一些GPU工作,我需要在GPU上分配一些数据.cudaMalloc函数如下:
cudaMalloc(void** identifier, size_t space);
Run Code Online (Sandbox Code Playgroud)
很容易.所以,让我们分配一个整数.
int i = 5;
cudaMalloc((void**)&(&i), sizeof(int));
Run Code Online (Sandbox Code Playgroud)
但是这个错误("表达式必须是左值或函数指示符").明显的解决方法是声明i为开头的指针,然后获取它的地址,并且完全正常; 我只是讨厌变通办法.
我觉得这个问题应该有一个明显的答案-毕竟**,***即使**********在C.工作得很好,所以,我怎么得到一个变量的地址的地址"干净"?
谢谢!
我正在尝试通过编写链接列表来教自己C. 我是指针和内存管理的新手,我有点困惑.我有这个代码:
/* Remove a node from the list and rejiggle the pointers */
void rm_node(struct node **listP, int index) {
struct node *prev;
struct node *n = *listP;
if (index == 0) {
*listP = *listP->next;
free(n);
return;
}
for (index; index > 0; index--) {
n = n->next;
if (index == 2) {
prev = n;
}
}
prev->next = n->next;
free(n);
}
Run Code Online (Sandbox Code Playgroud)
从列表中删除元素.如果我想删除第一个节点,我仍然需要某种方式来引用列表,这就是为什么listParg是一个双指针,所以它可以指向列表的第一个元素并允许我释放使用的节点成为头.但是,当我尝试取消引用listP访问指向下一个节点的指针时,编译器会告诉我error: request for member ‘next’ in something not a structure …
我有一个来自键盘的字符,整数:
int c = getch();
Run Code Online (Sandbox Code Playgroud)
我想将它附加到字符串只有它不是return:
void somefunction()
{
std::string str = "you pressed: ";
int c;
while ( 1 )
{
c = getch();
if ( c == 10 ) break;
char* ch;
sprintf(ch,"%c",c);
str += std::string(ch);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当somefunction剩下范围时,这会产生分段错误.我正在考虑当str调用dtor for时,指针ch不再可用.
我该如何解决这个问题?
我的课程有效,但我的教授说我的代码不正确,但表示他会在秋季学期找到原因......他在说什么?也许是不合适的东西?即使你是不正确的我会很感激你的大脑:)
void CResizableArray::SetSize( int intNewSize )
{
int intIndex = 0;
if( intNewSize < 0 ) intNewSize = 0;
if( intNewSize > intMAXIMUM_ARRAY_SIZE )
{
intNewSize = intMAXIMUM_ARRAY_SIZE;
}
//////////////////////////////////////
// ---> HUGE BUG HERE <--- //
// Code works but is WRONG //
// WHY IS THIS HELP ME FIND THE BUG //
//////////////////////////////////////
m_intArraySize = intNewSize;
m_paintValues = new int [m_intArraySize];
// Initialize to zero
for( intIndex = 0; intIndex < m_intArraySize; intIndex++ )
{
*( m_paintValues + …Run Code Online (Sandbox Code Playgroud) 我正在更改数据库库中的一些代码.它的工作方式我发送一个void指针,以获得它的大小我调用一个查询并使用查询我计算结构的大小.现在的问题是我接收结构作为参数但是函数在第一次获取之前/之中失败.之后,我需要清除结构,但我甚至没有大小.我知道最好的方法是将结构的大小作为参数发送,但我已经编译了成千上万的程序,该库是从1996开始的,所以我需要找到一种方法来计算结构大小,即使类型是无效的.
我的一个想法是计算下一个不在结构中的元素的位置
0x000010 0x000042
[int|char[30]|int|int][int]
Run Code Online (Sandbox Code Playgroud)
因此大小为32,因为0x00042-0x000010是32.
有没有办法知道我什么时候离开了这个结构.
功能的原型是
int getData(char* fields, void* myStruct)
Run Code Online (Sandbox Code Playgroud)
我需要找出结构尺寸.
对不起,如果我错过了一些信息,代码是巨大的,不幸的是我不能在这里发布.
我写了一个在COM中实现IGroupPolicyObject接口的类.其中一个方法现在返回C#中的IntPtr对象:
IntPtr ghKey = objectGPolicy.GetRegistryKey(GpoSectionMachine);
Run Code Online (Sandbox Code Playgroud)
我想在使用属于Microsot.Win32命名空间的RegistryKey对象时derefrence该指针.
这不起作用:
(RegistryKey)ghKey = rootKey.CreateSubKey(policyPath);
Run Code Online (Sandbox Code Playgroud)
我找不到如何降低IntPtr.有人可以帮忙吗?
谢谢