给出以下代码:
#include <iostream>
using namespace std;
class CRectangle {
public:
int *width, *height;
CRectangle (int,int);
~CRectangle ();
int area () {return (*width * *height);}
};
CRectangle::CRectangle (int a, int b) {
width = new int;
height = new int;
*width = a;
*height = b;
}
CRectangle::~CRectangle () {
delete width;
delete height;
}
int main () {
CRectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << …Run Code Online (Sandbox Code Playgroud) 我重写了一个将struct指针作为参数的方法.我没有结构的声明,所以我不知道它的大小等.我能从一个指向它的指针找到一个结构吗?
是char* string[]等同于char** string在C++?这两个声明之间到底有什么区别?在内存访问速度方面最好的声明是什么?
谢谢,
嗨,我想制作一个外部单链表.我有一个问题"赋值中的非Ivalue"及其出现在行"this = currP-> next"我试图使它成为currP.next但它也产生错误
#include <cstdlib>
using namespace std;
struct node{
int data;
node *next;
node(int i){
data = i;
next = NULL;
}
void insert(int position, node &n){
node *currP = this;
node *prevP= NULL;
for(int counter = 0; counter>=position;counter++, prevP = currP, currP = currP->next){
if(counter==position)
{
n.next = currP->next;
currP->next = &n;
}
}
}
void add(node &n){
next = &n;
}
void deleteNode(int i){
node *currP = this;
node *prevP = NULL;
while(currP!= NULL){
if(currP->data == …Run Code Online (Sandbox Code Playgroud) 这段代码正在运行,但它并不完全是我想要的.有没有人知道如何使它正确,没有q排序?想法是了解如何使用指针.这三个数字应该是-3到12之间的随机数.下面的代码类似,我发现的最接近.任何帮助将非常感激.提前致谢!!.
#include <stdio.h>
#include <stdlib.h>
//functions
int compare(const void *a, const void *b)
{
const int *ia = a;
const int *ib = b;
if (*ia < *ib)
return -1;
else if (*ia > *ib)
return +1;
return 0;
}
//qsort function
void sort3(int *a, int *b, int *c)
{
int temp[3];
temp[0] = *a;
temp[1] = *b;
temp[2] = *c;
qsort(temp, 3, sizeof(int), &compare);
*a = temp[0];
*b = temp[1];
*c = temp[2];
}
//random function
int rand_int(int a, …Run Code Online (Sandbox Code Playgroud) 给一个void *变量作为输入(只能指向一个进程或线程),我想首先确定它的类型,然后将其转换为该类型.
我应该如何在C++中做到这一点?我知道这是一个愚蠢的问题,但我以前从未做过C/C++,也不能想到C/C++.
编辑:我需要在Linux和Windows上实现这一点.
下面的代码使用八面体作为起始3D形状(我在网上找到它),经过一些调整后我仍然无法使其工作.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h> /* must include for the offsetof macro */
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdlib.h>
#include <vector>
#include <iostream>
using namespace std;
struct XYZ {
GLdouble x;
GLdouble y;
GLdouble z;
};
struct FACET3 {
XYZ p1;
XYZ p2;
XYZ p3;
};
void Normalise(XYZ *p_input)
{
double magnitude = 0;
magnitude = sqrt((p_input.x * p_input.x )+ (p_input.y * p_input.y) + (p_input.z * p_input.z));
p_input.x = p_input.x …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释一下之间的区别:
int *arr[5]vs. int (*arr)[5]vs.int *(*arr)[5]
我试图围绕如何在C中实际表示指针/数组...
我有一个类设计问题,可以通过这个例子简化:
// foo.h
#include "foo2.h"
class foo
{
public:
foo2 *child;
// foo2 needs to be able to access the instance
// of foo it belongs to from anywhere inside the class
// possibly through a pointer
};
// foo2.h
// cannot include foo.h, that would cause an include loop
class foo2
{
public:
foo *parent;
// How can I have a foo pointer if foo hasn't been pre-processed yet?
// I know I could use a generic LPVOID pointer …Run Code Online (Sandbox Code Playgroud) 我只想写下:
char* test="test";
printf("%s",test[0]);
Run Code Online (Sandbox Code Playgroud)
它说seg故障; 然后我改变
printf("%s",&test[0]);了错误但是这不是我想要的; 控制台打印:"测试"如何从该指针获得值"t"?