我是Node类的构造函数:
Node::Node(int item, Node const * next)
{
this->item = item;
this->next = next;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时会出现编译错误:从'const Node*'到'Node*'的无效转换
有没有办法传递一个指向常量数据的指针?
如果我想操作指向一个字节的指针,我应该使用void*或char*,因为我听说过
sizeof(char)
Run Code Online (Sandbox Code Playgroud)
并不总是1个字节,那么void*呢?如果我做
void *p
++p
Run Code Online (Sandbox Code Playgroud)
它会进一步指向一个字节吗?
我正在学习引用和指针,本教程中的内容并没有为我编译(我正在使用GCC).
好的,这是代码:
#include <iostream>
using namespace std;
int main()
{
int ted = 5;
int andy = 6;
ted = &andy;
cout << "ted: " << ted << endl;
cout << "andy: " << andy << endl;
}
Run Code Online (Sandbox Code Playgroud)
编译器输出显示"错误:从'int*'到'int'的无效转换"我也尝试了一个string = v; v =&andy; 但那也不起作用.
如何将内存地址分配给变量?
问候,我遇到了错误"Assignment_1.c:10:18:错误:'s的存储大小'未知"我不是使用指针指向的专家,我想要一个动态大小的动态大小的数组话.任何的想法?
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int size = MAX;
typedef struct{
int numberOfWords,averageWordLength,id;
char ** words;
}sentence;
void main(){
struct sentence s;
s.numberOfWords=3;
s.averageWordLength=5;
s.id=1;
s->words= malloc(size * sizeof(s));
//printf("%s",s.words);
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个链表,当我尝试打印节点的值并使用NULL作为绑定时,它不起作用.例如:
#include <iostream>
typedef struct Node;
typedef Node* Node_ptr;
struct Node
{
int i;
Node_ptr next;
};
int main()
{
Node_ptr ptr, head;
ptr = new Node;
head = ptr;
// load
for(int j = 0; j < 4; j++)
{
ptr->next = new Node;
ptr->i = j;
ptr = ptr->next;
}
// print
ptr = head;
while(ptr->next != NULL)
{
std::cout << "print: " << ptr->i << std::endl;
ptr = ptr->next;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,代码会陷入while循环中的无限循环中.它永远不会理解链表只有5个节点长,它只是继续前进.我不明白为什么会这样.
我试图在c ++中交换数组和指针
我的代码如下:
void foo(int* a, int* b);
void main()
{
int *a = NULL;
int b[6]={2,3,5,6};
foo(a,b);
}
void foo(int* a, int b[])
{
int * c;
c=a;
a=b;
b=c;
}
Run Code Online (Sandbox Code Playgroud)
当我退出方法时没有改变,
在方法内,一切都工作,但当方法返回时没有任何变化.
我的问题是:
A)我的错误是什么?B)我该如何解决它.
我试图在objective-c中理解这一点:
在这个例子中,indexPath是一个指针,但我们在函数中"按原样"使用它:indexPath.section而不是(例如)*indexPath.section(带有*):
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return (indexPath.section == 0) ? nil : indexPath;
}
Run Code Online (Sandbox Code Playgroud)
所以,在objective-c中,我们不需要添加一个*来获取指针所指向的变量的内容......?
但是我找到了这个函数,它们*在函数内部使用了一个指针(on reverse):
NSInteger lastNameFirstNameSort(id person1, id person2, void *reverse)
{
NSString *name1 = [person1 valueForKey:LAST];
//...
if (*(BOOL *)reverse == YES) {
return 0 - comparison;
}
Run Code Online (Sandbox Code Playgroud)
对于id变量,它们使用变量名称:例如:person1
那么,有人可以解释一下这两个例子之间的区别:
为什么在第一个例子中,我们不添加一个*关于indexPath,
为什么我们不在变量*上添加它id,我们*reverse在第二个例子中使用它?
谢谢
我有一些关于在boost库中实现的智能指针的问题.是shared_ptr的和scoped_ptr的没有拷贝构造函数和shared_ptr的有它scoped_ptr的之间的唯一diffrence?当对象不调用复制构造函数时,我应该总是使用scoped_ptr而不是shared_ptr吗?我也不明白共享/作用域数组的想法.我不能只使用std :: vector而不是它吗?
通过一些关于修改CGImageRef数据的文档,我遇到了一个奇怪的例子 - 它有点像这个伪代码:
void *data = Allocate space for data;
if (data != NULL) Manipulate data;
if (data) Free data;
Run Code Online (Sandbox Code Playgroud)
这让我感到疑惑!布尔运算if (data != NULL)和布尔运算有什么区别if (data).
更具体地说,当指针被视为布尔值时,指针如何在Objective C中表现?试图谷歌这一点,我才发现的有关问题pointers-无数到 -booleans,而不是指针被评估为布尔值.
我一直在努力处理C中的一个简单任务......(已经有一段时间了.)我需要构建一个函数,在不使用任何内存分配函数的情况下创建和重置结构数组.
我最初用malloc设计它:
typedef struct {
int ..
int ..
} Branch;
Branch* createBranchList (int N)
{
Branch *List;
Branch reSet = {0}; // a zero'd Branch struct used for the resetting process
int i;
if(!(List=(Branch*)malloc(sizeof(Branch)*N))){
printf("Allocation error");
return NULL;
}
for(i=0; i<N; i++)
List[i] = reSet;
return List;
}
Run Code Online (Sandbox Code Playgroud)
现在如何在不使用内存分配的情况下执行此操作?我可以退回参考吗?我不这么认为.
谢谢任何人的帮助.
pointers ×10
c++ ×5
c ×3
objective-c ×2
struct ×2
arrays ×1
boolean ×1
boost ×1
constants ×1
evaluation ×1
linked-list ×1
malloc ×1
memory ×1
nodes ×1
return ×1
scoped-ptr ×1
shared-ptr ×1
swap ×1