我有一个相对简单的算法,它走std :: vector寻找两个相邻的元组.一旦找到X值左右两侧的元组,我就可以在它们之间进行插值.不知何故,这有效:
std::vector<LutTuple*>::iterator tuple_it;
LutTuple* left = NULL;
LutTuple* right = NULL;
bool found = 0;
// Only iterate as long as the points are not found
for(tuple_it = lut.begin(); (tuple_it != lut.end() && !found); tuple_it++) {
// If the tuple is less than r2 we found the first element
if((*tuple_it)->r < r) {
left = *tuple_it;
}
if ((*tuple_it)->r > r) {
right = *tuple_it;
}
if(left && right) {
found = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
而这个:
std::vector<LutTuple*>::iterator tuple_it; …Run Code Online (Sandbox Code Playgroud) 这是我刚才写的函数的简化版本:
int foobar(char * foo) {
puts(type);
struct node * ptr = (struct node *) malloc (sizeof(struct node));
puts(type);
memset(ptr, 0, sizeof(ptr));
ptr=head;
return head->id;
}
Run Code Online (Sandbox Code Playgroud)
这里node只是一个在链表中声明为节点的结构,它包含char *一个指向下一个节点的指针.但是,我意识到malloc()这里正在腐蚀我的输入char * foo.
为什么会malloc()破坏我的输入字符指针?另外,我怎么能在这里解决这个问题?现在我只是将该指针的内容复制到本地数组,但这太过于hacky,即使是我的口味(这不是最好的).
感谢您的任何投入!
编辑:嗯,这是更真实的代码:
void foobar(char * type) {
puts(type); <-- here it's a long string about 30 char
struct node * ptr = (struct node *) malloc (sizeof(struct node));
puts(type); <- chopped of, 10 left with some random thing at …Run Code Online (Sandbox Code Playgroud) 作为c ++的初学者,我很难理解指针和数组.我写了以下程序:
int main (void){
int p[3]={0};
int * iptr = new int [4];
iptr++;
*iptr=2;
iptr++;
*iptr=3;
for (int i=0;i<4;i++){
cout << "iptr: " << *iptr << endl;
iptr++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我没有得到预期的结果.我希望元素[1]和[2]分别具有值2和3.但是我得到了一个结果:
iptr: 3
iptr: -842150451
iptr: -33686019
iptr: 0
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我,我的理解在哪里出错了吗?
谢谢丹
在我的班级Tabla中,我有一个指向方法的公共指针:
public:
int (Tabla :: *punterofunc)(int,int);
Run Code Online (Sandbox Code Playgroud)
在主要我把它指向一个类方法:
tablita.punterofunc = &Tabla :: in_lineal;
Run Code Online (Sandbox Code Playgroud)
但这个电话不起作用!
tablita->punterofunc(num,0);
Run Code Online (Sandbox Code Playgroud) 如果我们从函数返回指针,我们可以写任何一个
const X* f();
Run Code Online (Sandbox Code Playgroud)
要么
X* const f();
Run Code Online (Sandbox Code Playgroud)
,以这种方式控制是否可以重新分配指针或是否可以在内部修改类.但是当返回引用时,这两个似乎具有相同的含义:
const X& f();
X& const f();
Run Code Online (Sandbox Code Playgroud)
似乎不可能返回一个你可以修改X的引用,但不能重新分配它?如果它确实不可能,为什么我们应该在指针在这个区域看起来有效时返回引用?
更新:正如已经指出的那样,不能重新分配引用.然而,这让我更加困惑,因为下面的代码打印33 55,而不是33 33,正如我所料.如何匹配该引用无法重新分配?
struct X
{
X(int i_) { i = i_;}
int i;
};
struct Y
{
X& get2() {tmp2 = new X(55); return *tmp2;}
X& get() {tmp = new X(33); return *tmp;}
void print () {cout << tmp->i << endl;}
X* tmp;
X* tmp2;
};
int _tmain(int argc, _TCHAR* argv[])
{
Y y;
X& tmp2 = y.get2();
X& tmp = y.get();
y.print(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习C中的指针,但是它与以下概念混淆:
char *string = "hello"
char *string2;
Run Code Online (Sandbox Code Playgroud)
以下是什么主要区别:
A.) *string2 = string;
Run Code Online (Sandbox Code Playgroud)
然后
B.) string2 = "bye";
Run Code Online (Sandbox Code Playgroud) 为什么这段代码不能用于在c ++中制作2D数组指针?编译器抱怨第二行不是可修改的l值.
int* g[2][2];
g[0] = new (int*)[2];
Run Code Online (Sandbox Code Playgroud) 我在访问另一个结构中的char*时遇到问题,继承代码:
User_Network.c:
struct User_Networks_t {
SocialNetwork network;
};
typedef struct User_Networks_t* User_Networks;
void aaa(User_Networks u1)
{
u1->network->Name = "test"; /// ERROR: dereferencing pointer to incomplete type
}
Run Code Online (Sandbox Code Playgroud)
这是我在SocialNetwork.h中的内容:
typedef struct SocialNetwork_t* SocialNetwork;
Run Code Online (Sandbox Code Playgroud)
SocialNetwork.c中的结构:
struct SocialNetwork_t {
char *Name;
};
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这个错误?
这种方式是合法的吗?
void probability(void **value)
{
double v = 0.1234;
*value = (void *) *(uint64_t *) &v;
}
Run Code Online (Sandbox Code Playgroud)
我知道,这是一件坏事,但我100%肯定sizeof(double)= sizeof(void *)= sizeof(uint64_t)在目标机器上.
我想在我的类的头文件中声明一个指向整数数组的指针.我有
private:
int *theName;
Run Code Online (Sandbox Code Playgroud)
然后在构造函数中
theName = new int[10];
Run Code Online (Sandbox Code Playgroud)
这会有用吗?还有另一种方法可以做到这一点,所以你有一个指向数组的指针吗?
我以为int*theName创建了一个指向整数而不是整数数组的指针,如果这创建了一个指向整数数组的指针,那么如何创建一个只指向整数的指针?
我也可以在析构函数中调用
delete theName; theName = NULL;
Run Code Online (Sandbox Code Playgroud)
是否会删除name整数数组对象,然后将name指向内存中的null?
谢谢