标签: structure

修改foreach中的内容

我倾向于使用结构的ArrayLists.然后用foreach循环遍历列表非常容易.

我遇到的问题是我不能使用foreach来修改结构内容,并且必须使用for和messy类型转换.

((dataStructure)files[x]).name = here;
Run Code Online (Sandbox Code Playgroud)

有更简洁的方法吗?

c# casting structure arraylist

1
推荐指数
2
解决办法
1062
查看次数

Ruby:从模块中重新打开一个类

为什么这不起作用?

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中.如果我删除模块包装器,它可以工作.

ruby syntax structure class

1
推荐指数
1
解决办法
1150
查看次数

指向结构指针的指针

我有一个如图所示的结构.当我有一个指向结构的指针时,我能够正常初始化或修改它的任何成员.

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是一个全局变量.

c pointers structure

1
推荐指数
2
解决办法
243
查看次数

使用结构指针访问C中的struct成员时出现Seg错误

我的程序出了什么问题,当我尝试打印值时出现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)

指针对我来说一直是个噩梦,我从来不理解它.

c pointers structure

1
推荐指数
1
解决办法
7165
查看次数

在javascript中向数组或对象添加多个值

我喜欢有数组或对象:

[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)

javascript arrays structure object

1
推荐指数
1
解决办法
9963
查看次数

C中的嵌套结构

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)

没有任何错误.我不明白为什么这是指针允许但不是非指针变量!

c nested structure

1
推荐指数
1
解决办法
2633
查看次数

适合初学者的结构

我是结构新手,我正在尝试做一些教程,看看我是否理解了我一直在学习的东西.这是我写的代码:

#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]左右吗?

c struct structure

1
推荐指数
1
解决办法
95
查看次数

为什么C中的以下代码打印-1?

我期待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)

c structure bit-fields

1
推荐指数
1
解决办法
143
查看次数

我可以使用简单的Int创建结构的实例吗?

以这种方式构建结构的实例是正确的吗?

public struct Barometer {
   public var pressure: Int
   public init(pressure: Int) {
      self.pressure = pressure
   }
} 

var barometer: Barometer = 80
Run Code Online (Sandbox Code Playgroud)

或者我需要采用协议?

structure instance swift

1
推荐指数
1
解决办法
664
查看次数

格式'%s'需要'*char'类型的参数,但参数2的类型为'int'

当我尝试编译我的程序时,我在标题中收到警告消息,当我在扫描名称和分数后运行它时它就停止了.我在练习使用字符串时遇到过这个问题很多次,但我找不到解决方案.

#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)

c arrays string structure

1
推荐指数
1
解决办法
126
查看次数

标签 统计

structure ×10

c ×6

arrays ×2

pointers ×2

arraylist ×1

bit-fields ×1

c# ×1

casting ×1

class ×1

instance ×1

javascript ×1

nested ×1

object ×1

ruby ×1

string ×1

struct ×1

swift ×1

syntax ×1