我想为链表定义一个结构,从接下来的两个选项中,其中一个主题更好?他们俩都会工作吗?有什么不同,你将使用哪一个?
typedef struct suppliers * SUP;
typedef struct suppliers{
int num;
int moths;
SUP next;
} su;
Run Code Online (Sandbox Code Playgroud)
其他选项是:
typedef supplier *suppliers
typedef struct supplier{
int num;
int moths;
struct supplier *next;
} supplier;
Run Code Online (Sandbox Code Playgroud) 我不明白为什么我不能定义这个结构:
//Class.h
template <class T>
struct Callback {
T* Object;
std::function<void()> Function;
};
template <class T>
struct KeyCodeStruct {
typedef std::unordered_map<SDL_Keycode, Callback<T>> KeyCode;
};
template <class T>
struct BindingStruct{
typedef std::unordered_map<int, KeyCodeStruct<T>> Binding;
};
class Class {
public:
template <class T>
void bindInput(SDL_EventType eventType, SDL_Keycode key, Callback<T> f);
private:
template <class T>
BindingStruct<T> inputBindings; //How do I define this? This gives me an error.
}
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
Member 'inputBindings' declared as a template.
我不太了解模板所以我可能只是错过了我需要的信息.
更新(回应deviantfan)
现在我的cpp类遇到了我所拥有的函数的问题.
template <class T>
void …Run Code Online (Sandbox Code Playgroud) 我正在尝试读取文件内容并将其存储到结构中,因为某些原因我不断收到分段错误.请帮助我,我也不了解valgrind.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int day;
int month;
int year;
char text[401];
} journal;
int main(int argc, char** argv){
int i, numberEntries;
FILE* fp = fopen(argv[1], "r");
fscanf(fp,"%d", &numberEntries); /* reads value on first line of file for number of entries */
printf("%d", numberEntries); /* check that it worked */
journal *entryArray ;
entryArray = (journal*)malloc(sizeof(journal));
if(fp == NULL){
perror("Error opening file");
} else {
for(i=0; i<4; i++){
fscanf(fp,"%d/%d/%d", entryArray[i].day, entryArray[i].month, entryArray[i].year);
fgets(entryArray[i].text, 400, fp);
printf("%s", …Run Code Online (Sandbox Code Playgroud) 我正在使用GLFW并创建一个GLFWwindow,它在它的标题"glfw3.h"中声明如下:
typedef struct GLFWwindow GLFWwindow;
通常,在堆上初始化结构时,我会执行以下操作:
GLFWwindow *window = new GLFWwindow();
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:无效使用不完整类型'GLFWwindow {aka struct GLFWwindow}','GLFWwindow {aka struct GLFWwindow}'的前向声明
这是我对该问题的修复:
GLFWwindow **window = new GLFWwindow*();
这个修复实际上有效,但我不明白为什么我需要它.
Thx提前
#include <stdio.h>
#include <stdlib.h>
struct Album {
char* title;
}
int main(){
int i, size;
struct Album* pAlbum;
printf("Enter the number of album: ");
scanf_s("%d", &size);
pAlbum = malloc(sizeof(pAlbum) * size);
for(i=0; i<size; i++) {
printf("Enter the album title: ");
scanf_s("%p", pAlbum[i].title);
}
free(pAlbum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想让用户输入他们想要的多个专辑的标题.错误是scanf只pAlbump[i].tittle为循环出现一次.我是不是错误地分配了内存?
我遇到了结构问题.在每个函数声明之前,我收到有关标识符的错误.错误发生在'typedef','coords stackCreate'和'coords stackPush'之前
typedef struct coords * coordPtr
{
int x = -1;
int y = -1;
struct coords * next;
};
coords stackCreate(int x, int y){
coordPtr stack = malloc(sizeof(coords));
stack->x = x;
stack->y = y;
return stack;
}
coords stackPush(int x, int y, coords stack){
stack->next = malloc(sizeof(coords));
stack->next->x = x;
stack->next->y = y;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助!
我构建了一个测试并获得了以下结果:
分配类:15.3260622,分配结构:14.7216018.
分配结构而不是类时看起来有4%的优势.这很酷但是真的足以添加语言值类型吗?在哪里可以找到一个显示结构真正击败类的例子?
void Main()
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
for (int i = 0; i < 100000000; i++)
{
var foo = new refFoo()
{
Str = "Alex" + i
};
}
stopWatch.Stop();
stopWatch.Dump();
stopWatch.Restart();
for (int i = 0; i < 100000000; i++)
{
var foo = new valFoo()
{
Str = "Alex" + i
};
}
stopWatch.Stop();
stopWatch.Dump();
}
public struct valFoo
{
public string Str;
}
public class refFoo
{
public string Str;
}
Run Code Online (Sandbox Code Playgroud) 我写了这个程序
#include<stdio.h>
struct student
{
char name[22];
char rollno[10];
unsigned long long int phno;
};
int main()
{
int i,j;
char c[1];
struct student cse[10],*p;
p=cse;
for(i=0;i<3;i++)
{
printf("enter student name\n");
gets(cse[i].name);
printf("enter student roll number \n");
gets(cse[i].rollno);
printf("enter student phone number \n");
scanf("%llu",&cse[i].phno);
gets(c); //to catch the '\n' left unprocessed by scanf
}
for(i=0;i<3;i++);
printf("the following is the information about CSE B student\n");
printf("%-6s%-24s%-14s%-14s \n","S.no","student Name","Roll no","phone no.");
for(i=0;i<3;i++)
{
printf("%-6d%-24s%-20s%-14llu \n",i+1,(*p).name,(*p).rollno,(*p).phno);
++p;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
the …Run Code Online (Sandbox Code Playgroud) 当我们尝试以n-> myBool访问它时,结构中这个bool的值是多少?我很想知道在为它分配内存之后bool的"默认"值是什么,但是没有为它赋值true.
#include <stdbool.h>
typedef struct node
{
bool myBool;
} Node;
void main()
{
Node* n = malloc(sizeof(Node));
return;
}
Run Code Online (Sandbox Code Playgroud)
编辑:更正了代码中的拼写错误(应该是sizeof(Node)而不是sizeof(节点)
我有一个游戏,我想计算一个玩摇滚纸剪刀的玩家对计算机的输赢(C++的随机数函数).为了跟踪玩家的胜利,损失以及与计算机的关系,我创建了这个结构(这是全局的):
struct human{
short wins = 0;
short losses = 0;
short ties = 0;
};
Run Code Online (Sandbox Code Playgroud)
当我尝试使用short结构中的一个变量进行数学运算时会出现问题:
int main(){
short totalPlays = 1;
float winsPerc;
outcome(totalPlays, winsPerc);
}
void outcome(short totalPlays, float& winsPerc){
winsPerc = (static_cast<float>(human.wins) / static_cast<float>(totalPlays))* 100;
}
Run Code Online (Sandbox Code Playgroud)
在这两个函数中,我试图计算玩家获胜的百分比.但是,我的编译器似乎human将human.wins变量的一部分解释为一个类型.所以当然我试图改变human.wins到short human.wins无济于事.之后,编译器只表示类型short是意外的.由于这是如此模糊,我真的不知道我做错了什么.任何帮助将非常感谢!