我倾向于使用结构的ArrayLists.然后用foreach循环遍历列表非常容易.
我遇到的问题是我不能使用foreach来修改结构内容,并且必须使用for和messy类型转换.
((dataStructure)files[x]).name = here;
Run Code Online (Sandbox Code Playgroud)
有更简洁的方法吗?
为什么这不起作用?
module XT
puts Fixnum.class # So we're sure to re-open the Fixnum class
class Fixnum
def hi
puts "HI from module XT"
end
end
end
Run Code Online (Sandbox Code Playgroud)
在要求并加载模块后,该hi方法仍未添加到Fixnum中.如果我删除模块包装器,它可以工作.
我有一个如图所示的结构.当我有一个指向结构的指针时,我能够正常初始化或修改它的任何成员.
struct node{
int key;
int nno;
char color;
struct node* out;
struct node* next;
struct node* pre;
};
Run Code Online (Sandbox Code Playgroud)
但是,当我将结构指针的地址传递给函数并使用双指针捕获它时,并尝试使用该双指针访问成员时,我的编译器抛出错误'member undefined'.
void DFSVisit(struct node** u){
*u->color = 'g';
struct node* v;
while(*u->out != NULL){
v = *u->out;
if(v->color == 'w'){
v->pre = *u;
DFSVisit(&v);
}
}
*u->color = 'b';
}
Run Code Online (Sandbox Code Playgroud)
而且,这就是我访问该功能的方式.
DFSVisit(&root);
Run Code Online (Sandbox Code Playgroud)
Root是正确初始化的指针.而且,Root是一个全局变量.
我的程序出了什么问题,当我尝试打印值时出现seg错误.
我的目标是在sample_function中分配一些值.
在main函数中我想将结构复制到另一个结构.
#include<stdio.h>
#include<string.h>
typedef struct
{
char *name;
char *class;
char *rollno;
} test;
test *
sample_function ()
{
test *abc;
abc = (test *)malloc(sizeof(test));
strcpy(abc->name,"Microsoft");
abc->class = "MD5";
abc->rollno = "12345";
printf("%s %s %s\n",abc->name,abc->class,abc->rollno);
return abc;
}
int main(){
test *digest_abc = NULL;
test *abc = NULL;
abc = sample_function();
digest_abc = abc;
printf(" %s %s %s \n",digest_abc->name,digest_abc->class,digest_abc->rollno);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
指针对我来说一直是个噩梦,我从来不理解它.
我喜欢有数组或对象:
[0]
text:"first"
id: 1
[1]
text:"second"
id: 2
[2]
text:"third"
id: 3
Run Code Online (Sandbox Code Playgroud)
得到自己:
1: first
2: 1
3: second
4: 2
5: third
6: 3
Run Code Online (Sandbox Code Playgroud)
这是我的javascript,目前正在为数组实现:
var numberOfQuestions = questionaireResult.numberOfQuestions;
var i;
var j;
var result = [];
for (i = 0; i < numberOfQuestions; i++) {
debugger;
var question = questionaireResult.questions[i].text;
var questionID = questionaireResult.questions[i].id;
for (j = 0; j < questionaireResult.questions[i].answers.length; j++) {
var text = questionaireResult.questions[i].answers[j].text;
var id = questionaireResult.questions[i].answers[j].id;
result.push(text, id); …Run Code Online (Sandbox Code Playgroud) struct s{
int a;
struct s b;
};
Run Code Online (Sandbox Code Playgroud)
上面的代码段引发错误 error: field 'b' has incomplete type而
struct s{
int a;
struct s *b;
};
Run Code Online (Sandbox Code Playgroud)
没有任何错误.我不明白为什么这是指针允许但不是非指针变量!
我是结构新手,我正在尝试做一些教程,看看我是否理解了我一直在学习的东西.这是我写的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct variables{
float Vx;
float Vy;
float Vz;
}velocity;
int main(){
velocity *pv;
pv = (velocity*)malloc(sizeof(velocity));
pv[0].Vx = 1;
pv[0].Vy = 2;
pv[0].Vz = 3;
free(pv);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是2:
我是否以正确的方式分配了三个变量?
由于我使用数组表示法为什么要我曾经写的[0]
,而不是[1]或[2]左右吗?
我期待1作为输出..
#include<stdio.h>
int main(){
struct A{
int a:1;
};
struct A bb;
bb.a=1;
printf("%d",bb.a);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 以这种方式构建结构的实例是正确的吗?
public struct Barometer {
public var pressure: Int
public init(pressure: Int) {
self.pressure = pressure
}
}
var barometer: Barometer = 80
Run Code Online (Sandbox Code Playgroud)
或者我需要采用协议?
当我尝试编译我的程序时,我在标题中收到警告消息,当我在扫描名称和分数后运行它时它就停止了.我在练习使用字符串时遇到过这个问题很多次,但我找不到解决方案.
#include <stdio.h>
struct students {
char name[20];
int score[20];
} student;
int main() {
int i, n;
printf("Number of students:\n");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Name of the student:\n");
scanf("%s", &student.name[i]);
printf("Score of the student:\n");
scanf("%d", &student.score[i]);
}
for(i=0;i<n;i++) {
if(student.score[i] >= 15) {
printf("%s passed the exam\n", student.name[i]); }
else {
printf("%s failed the exam\n", student.name[i]);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)