标签: struct

在C中打印结构的名称

是否可以打印结构名称?

说我有一个结构:

typedef struct someName{
uint16_t  value;
uint16_t  field;
} someName_t;

someName_t test;

printf("%" PRIu16 "\n", test;
printf("%" PRIu16 "\n", test.value);
Run Code Online (Sandbox Code Playgroud)

打印test.value是oke.打印测试给了我警告..这是打印结构名称的正确方法,它甚至可以在C中使用吗?

首先PRIu16是我在stackoverflow上找到的,这是打印uint16_t的正确方法.

我想打印名称someName_t.

c printf struct

-4
推荐指数
1
解决办法
4555
查看次数

为什么*struct_pointer = struct_variable与struct_pointer =&struct_variable的行为不同?

我对C++比较陌生,我正在尝试使用 - > with structs.我有这个代码,应该从结构中打印出名称和年龄.我知道 - >当变量是一个指针时使用,所以我试图用以下代码声明一个指向struct的指针:

typedef struct Person{
  string name;
  int age;
};

int main() {
  Person me;
  me.name = "name";
  me.age = 1;
  Person* me2;
  *me2 = me;
  cout << me2->name << "," << me2->age << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码导致分段错误.

但是,如果我改变了

*me2 = me
Run Code Online (Sandbox Code Playgroud)

me2 = &me
Run Code Online (Sandbox Code Playgroud)

代码工作正常(打印出名称和年龄).

我不确定为什么这两行代码不相同.我以为我被允许用*更改指针的内容,但我不确定为什么这不起作用.请问有人帮我理解吗?

c++ struct pointers

-4
推荐指数
1
解决办法
88
查看次数

使用struct作为函数崩溃程序的返回值

我想使用struct作为fucntion的返回值,但它不起作用,我不知道为什么.运行此程序时程序崩溃.我得到了RTE.这段代码有什么问题:

#include <iostream>

using namespace std;
    struct Tablica{
        int T[201][201];
    };

    Tablica test(Tablica A, int m){
       if(m==1)return A;
       if(m%2 ==1){
            return test(A, m-1);
       }else{
           cout <<" #1 m " << m<<endl;
            Tablica B = test(A,m/2);
            cout <<" #2 m " << m<<endl;
            return B;
       }

    }
int main(){
    Tablica T;
   test(T,10);

}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c++ struct iostream

-4
推荐指数
1
解决办法
245
查看次数

结构功能中的“ *”是什么?

struct Node *addToEmpty(struct Node *last, int data) 
{ 
  // This function is only for empty list 
  if (last != NULL) 
    return last; 

  // Creating a node dynamically. 
  struct Node *temp =  
       (struct Node*)malloc(sizeof(struct Node)); 

  // Assigning the data. 
  temp -> data = data; 
  last = temp; 

  // Creating the link. 
  last -> next = last; 

  return last; 
} 

struct Node *addBegin(struct Node *last, int data) 
{ 
  if (last == NULL) 
      return addToEmpty(last, data); 

  struct Node *temp =  
        (struct Node *)malloc(sizeof(struct …
Run Code Online (Sandbox Code Playgroud)

c syntax struct

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

随机初始化一个结构体

我定义了两个结构。点和三角形,我想在主函数中用随机值初始化它们。但是这一行的编译器出了问题,停了下来。问题是什么?

#include <cmath>
#include <ctime>

struct Point{
    double x;
    double y;
    double z;
};
struct Triangle{
    Point* a;
    Point* b;
    Point* c;
};
int main() {
    Triangle t1;
    srand(time(0));
        t1.a->x=10.0*(rand()/RAND_MAX)-5; //this line gives Segmentation fault
        t1.a->y=10.0*(rand()/RAND_MAX)-5;
        t1.a->z=10.0*(rand()/RAND_MAX)-5;
        t1.b->x=10.0*(rand()/RAND_MAX)-5;
        t1.b->y=10.0*(rand()/RAND_MAX)-5;
        t1.b->z=10.0*(rand()/RAND_MAX)-5;
        t1.c->x=10.0*(rand()/RAND_MAX)-5;
        t1.c->y=10.0*(rand()/RAND_MAX)-5;
        t1.c->z=10.0*(rand()/RAND_MAX)-5;
}
Run Code Online (Sandbox Code Playgroud)

c++ struct segmentation-fault

-4
推荐指数
1
解决办法
252
查看次数

自变异只读结构域

我知道Eric Lippert关于情况的博客文章, 但我认为这是一个不同的情况,因为该领域自我而不是其领域.如果Enumerator只是readonly没有显示任何效果并且输出始终为0,你怎么解释调用MoveNext()?

 class SomeClass
    {
        private List<int> list;
        private [readonly] List<int>.Enumerator enumerator;

        public SomeClass()
        {
            list = new List<int>() { 1, 2, 3 };
            enumerator = list.GetEnumerator();
        }

        public int ReadValue()
        {
            if (enumerator.MoveNext())
                return enumerator.Current;
            return -1;

        }
    }
static void Main()
    {
        SomeClass c = new SomeClass();
        int value;
        while ((value = c.ReadValue()) > -1)
            MessageBox.Show(value.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

c# struct readonly

-5
推荐指数
1
解决办法
844
查看次数

当我们有一个更短的方法时,为什么我们在结构中使用typedef

当我定义一些随机结构时,例如在Visual Studio中的cpp文件中

1)    struct CAddition {
    int x, y;

    CAddition(int a, int b) { x = a; y = b; }
    int result() { return x + y; }
};
Run Code Online (Sandbox Code Playgroud)

现在,如果我定义一些结构对象

CAddition foo;
Run Code Online (Sandbox Code Playgroud)

它没有任何错误,但如果我最后使用任何别名

2) struct CAddition {

    int x, y;

CAddition(int a, int b) { x = a; y = b; }
int result() { return x + y; }
}CAddition;
Run Code Online (Sandbox Code Playgroud)

我不能简单地定义任何对象而不在定义之前使用struct

 struct CAddition foo;
Run Code Online (Sandbox Code Playgroud)

或者另一种方法是添加

typedef struct CAddition { 
Run Code Online (Sandbox Code Playgroud)

在方法2中,为了避免每次都重写struct,我的问题是这两个定义之间的区别是什么,方法1不使用更少的关键字,更容易使用在什么条件下我们应该使用结构的第二个定义.

c++ struct

-5
推荐指数
1
解决办法
83
查看次数

将内容附加到嵌套结构中不起作用

我有两个这样的嵌套结构:

type Block struct {
    ID       string
    Contents []string
}

type Package struct {
    Name   string
    Blocks []Block
}
Run Code Online (Sandbox Code Playgroud)

p当我尝试在特定块中附加新内容时,原始包 ( ) 不会更改。

for _, b := range p.Blocks {
    if b.ID == "B1" {
        fmt.Println("Adding a new content")
        b.Contents = append(b.Contents, "c3")
    }
}
Run Code Online (Sandbox Code Playgroud)

例子:

https://play.golang.org/p/5hm6RjPFk8o

struct append go slice

-5
推荐指数
1
解决办法
45
查看次数

这为什么会这么分裂?有人可以解释valgrind错误吗?

所以我有这个程序在我的家用机器上编译很好,但是一旦我在大学服务器上编译它就会打破......:/这对我的屁股来说是一个巨大的痛苦.我不知道可能导致错误的位置或原因.我首先从大学的valgrind报告开始.

    ==13527== Memcheck, a memory error detector
==13527== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==13527== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==13527== Command: ./main stock.dat coins.dat
==13527== 
==13527== Invalid write of size 8
==13527==    at 0x402762: load_data (in /RMIThome/shr/5/s3234575/Assignments2/main)
==13527==    by 0x4028BE: main (in /RMIThome/shr/5/s3234575/Assignments2/main)
==13527==  Address 0x6172676f72502074 is not stack'd, malloc'd or (recently) free'd
==13527== 
==13527== 
==13527== Process terminating with default action of signal 11 (SIGSEGV)
==13527==  General Protection Fault …
Run Code Online (Sandbox Code Playgroud)

c struct valgrind memory-leaks segmentation-fault

-6
推荐指数
1
解决办法
356
查看次数

如何在C中打印结构的内容?

使用与下面建议的方法不同的方法解决了我自己的问题!:)

感谢您查看我的问题!:)

我一直在学习结构和在C中的练习实验室工作,我的代码似乎没有正确编译我对它做的任何改变.目前我没有收到任何输出,程序崩溃.我仍然非常困惑如何在将它们传递给函数时正确使用'*'和'&'符号.我的目标是:

  • 以与数据文件相同的格式打印数组的内容
  • 打印具有最佳GPA的学生的全名
  • 计算并打印平均GPA
  • 打印GPA高于平均水平的所有学生的姓名
  • 打印GPA低于平均水平的最年轻学生的姓名
  • 按照从最低到最高GPA的顺序对数组中的结构进行排序
  • 再次打印数组(现在将与上次不同)

如何从学生结构中正确地调用和打印这些项目?我如何访问gpa值以传递给计算平均值的函数?

#include <stdio.h>
#include <stdlib.h>

// define constants
#define ARR 100
#define FIRST 7
#define MIDINIT 1
#define LAST 9
#define STREET 16
#define CITY 11
#define STATE 2
#define ZIP 5
#define AGE 3
#define GPA 4
#define START 0
#define FIRSTID 8
#define INITID 10
#define STREETID 20
#define CITYID 37
#define STATEID 49
#define ZIPID 52
#define AGEID 57
#define GPAID 64

// defined structs …
Run Code Online (Sandbox Code Playgroud)

c printf struct average

-6
推荐指数
1
解决办法
2599
查看次数