我有一个来自C++书籍的代码片段:
#include<stdio.h>
#include<stdlib.h>
struct S1
{
const char *s;
int i;
struct S1 *slp;
};
main()
{
static struct S1 a[]= {{"abcdef", 1, a+1},
{"ghijkl", 2, a+2},
{"mnopqr", 3, a} };
struct S1 *p = a;
printf("a[0].s = %s p-> = %s a[2].slp->s = %s \n", a[0].s, p->s, a[2].slp->s);
for(int i=0; i<3; i++)
{
printf("--a[i].i = %d ", --a[i].i);
printf("++a[i].s[3] = %c \n", ++a[i].s[3]);
}
}
Run Code Online (Sandbox Code Playgroud)
使用Dev C++ v5.11,我无法使用错误代码编译它:
[Error] increment of read-only location '*(a[i].S1::s + 3u)'
Run Code Online (Sandbox Code Playgroud)
由@Corristo评论的解决方案有效.
感谢大家的帮助.我下次会得到更好的IDE和书籍.谢谢,@ Tomaz-Canabrava.
我试图将total_elements设置为0,但它最终会给我一个随机的大数字.我对为什么会发生这种情况感到非常困惑.这是我的结构:
typedef struct sack {
Element *elements;
size_t total_elements;
} Sack;
typedef struct element {
char *name;
int occurances;
struct element *next;
} Element;
Run Code Online (Sandbox Code Playgroud)
这是代码:
void init_sack(Sack *sack) {
if(!(sack = calloc(1, sizeof(Sack)))) {
return;
}
sack->elements = NULL;
sack->total_elements = 0;
return;
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样定义的typedef结构:
typedef struct myStruct {
int id;
double value;
bool operator <(const myStruct &x, const myStruct &y) {
return (x.id < y.id);
}
} myStruct;
Run Code Online (Sandbox Code Playgroud)
我需要将此结构用作std :: map中的键,从而重载运算符.但是,我在尝试编译时收到以下错误消息:
overloaded 'operator<' must be a binary operator (has 3 parameters)
Run Code Online (Sandbox Code Playgroud)
好的,所以我尝试了这个:
bool operator <(const pointcloud_keyframe &x) {
return (this->id < x.id);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为我在尝试插入地图时收到此错误消息:
invalid operands to binary expression ('const myStruct' and 'const myStruct')
Run Code Online (Sandbox Code Playgroud)
请帮忙!
我的应用程序在崩溃时崩溃了.
所以我有一个像这样的结构(但实际上它有更多的东西)
struct Record
{
float m_fSimulationTime;
unsigned char m_szflags;
};
Run Code Online (Sandbox Code Playgroud)
在我的课堂上,我声明它是这样的:
Record *m_record[64];
Run Code Online (Sandbox Code Playgroud)
然后我将其初始化:(并且此处崩溃发生(读取时的访问冲突))
void ClassXYZ::initRecord()
{
for (int i = 0; i <= 32; i++)
for (int j = 0; j < 9; j++)
m_record[i][j].m_fSimulationTime = 0.0f; // here happens the crash
}
Run Code Online (Sandbox Code Playgroud)
我希望你能帮我解决我在这里缺少的xx
谢谢你的建议!
我尝试了这段代码
struct Bar {
public int Value;
}
async Task doItLater(Action fn) {
await Task.Delay(100);
fn();
}
void Main() {
Bar bar = new Bar { Value = 1 }; //Bar is a struct
doItLater(() => {
Console.WriteLine(bar.Value);
}).Wait();
}
Run Code Online (Sandbox Code Playgroud)
得到了输出1.现在这让我很困惑.我的逻辑如下
Task.Delay(100)命中时,该执行线程完成,并且请求TPL fn()稍后执行.bar 存储在堆栈上,当我们在闭包中访问它时,该帧不应该存在.那么我怎么得到一个输出1?
所以,我正在研究Structs vs Classes.到目前为止,我知道以下内容:
当传递给另一个函数时,类可以直接修改数据.类比Struct使用更多内存.
结构可以传递给函数,但数据在函数内克隆,而不是修改原始数据.
因此,如果我使用类,我可以将数据传递给函数并直接从新函数修改类中的数据.
如果我使用结构,我可以将数据传递给一个函数,该函数生成要在函数中使用的数据的副本,但不会影响原始数据.
但是,如果我将Struct传递给具有Ref关键字的函数,则它会修改Struct中的原始数据.
考虑到这一点,使用Struct与Ref关键字或仅使用类之间的主要区别是什么?它纯粹与记忆有关吗?我一直在使用Structs,但我需要修改原始数据 - 如果最好使用Ref关键字传递Struct,或者将我的所有Structs更改为Classes,则尝试解决.
在header.h中我有:
typedef struct {
int score;
int dice[6];
int scorecard[2][7];
} player;
Run Code Online (Sandbox Code Playgroud)
然后在定义.我有:
struct player do_turn(struct player user) {
for (int i = 0; i < 6; i++) {
int die = roll_die();
user.dice[i] = die;
}
return (user);
}
Run Code Online (Sandbox Code Playgroud)
在函数定义中我得到错误"函数do_turn返回不完整类型struct player"
我究竟做错了什么?
#include <stdio.h>
typedef struct StockDetail {
char* name;
int code;
int price;
} Stock;
int main(void)
{
Stock a[200]; int i; int b;
for(i=0; i<20 ; i++ )
{
printf("Stock %i\n",i+1);
printf("Name:");
scanf("%s",a[i].name);
printf("Code:");
scanf("%i",&a[i].code);
printf("Name:");
scanf("%i",&a[i].price);
}
printf("Maximum price of the stock:");
scanf("%i", &b);
for(i=0; i<20 ; i++)
{
if(a[i].price<=b)
{
printf("%s\n",a[i].name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
嗨,我正在尝试实施一个程序,读取20个股票详细信息,如名称,代码和价格,然后要求用户输入最高价格并打印成本低于价格的股票.代码看起来很好但是当我试图运行它时给出了"分段错误"错误行.
使用gcc -Wall -std = c99进行编译时遇到错误:
pokerhand.c:20:17: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
Card *cards = malloc(sizeof(Card)*5);
这是我的错误发生的代码
typedef struct card
{
char suit;
char *face;
} Card;
typedef struct hand
{
Card *cards = malloc(sizeof(Card)*5);
char *result;
} Hand;
Run Code Online (Sandbox Code Playgroud)
在这些结构之前我所拥有的只是标题包括
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
Run Code Online (Sandbox Code Playgroud) 我有一个存储在数组中的两个大数字(p->数字[50]和q->数字[50]),并以十六进制打印出来
1319df046
111111111
Run Code Online (Sandbox Code Playgroud)
当加在一起时,我以十六进制的形式返回
242af'11'257
但是,显然我的答案"应该"
242af0157
将f和1加在一起时有一个差异,等于17,但是打印11(17是十六进制的11).我不确定为什么我的输出应该是0而不是11
int sum = 0;
int carry = 0;
for(i = 9; i >= 0; i--)
{
sum = p->numbers[i] + q->numbers[i];
sum = sum + carry;
answer[i] = sum;
carry = sum / 10;
printf("%x", answer[i]);
}
Run Code Online (Sandbox Code Playgroud)