标签: struct

C++:将结构传递给函数

这是我在做的事情:

在single.h文件中定义了一个struct:

typedef struct
{
    double x,y,z;
}PhysicalSize;
Run Code Online (Sandbox Code Playgroud)

此结构用于single.cpp文件中

PhysicalSize physicalSize;
physicalSize.x = xstageStepSize;
physicalSize.y = ystageStepSize;
physicalSize.z = zstageStepSize;

long SaveTIFFWithOME(wchar_t *filePathAndName, char * pMemoryBuffer, long width, long height, unsigned short * rlut, unsigned short * glut,unsigned short * blut,
                 double umPerPixel,int nc, int nt, int nz, double timeIncrement, int c, int t, int z,string * acquiredDateTime,double deltaT, 
                 string * omeTiffData, AcquireSingle::PhysicalSize physicalSize)    
Run Code Online (Sandbox Code Playgroud)

我介绍了这个功能,看起来很好,所有人physicalSize.x, physicalSize.y and physicalSize.z都有正确的价值.但是,当我迈步时SaveTIFFWithOME(),所有physicalSize.x, physicalSize.y and physicalSize.z看起来都很奇怪,好像它们从未被初始化.

我想知道我做错了什么,以及将结构作为参数传递给函数的正确方法是什么?谢谢.

c++ struct function parameter-passing

-3
推荐指数
1
解决办法
214
查看次数

memcmp是否适用于非平面结构?

struct Flat {
  int a1; 
  int a2;
}
// a hierarchical struct which containing a struct attribute
struct NonFlat {
  Flat  b1; 
  int b2;
}

Flat f1, f2;
memcmp (&f1, &f2, sizeof f1) 
Run Code Online (Sandbox Code Playgroud)

在我的编译器中,它起作用,意思是

f1.a1 == f2.a1,f1.a2 == f2.a2 <=> memcmp(f1,f2)== 0;

NonFlat n1, n2;
memcmp (&n1, &n2, sizeof n1) // does it also work for non-flat structs, considering the padding?     
Run Code Online (Sandbox Code Playgroud)

我认为它也适用于NonFlat结构.但是,在我的编译器中,对于非平面结构,即使成员属性相等,memcmp的结果也表明它们是不同的.

c c++ struct padding memcmp

-3
推荐指数
1
解决办法
533
查看次数

NXC和F#中的结构

我有个问题:

在NXC中有这样的结构函数:

struct colorType
{
  int colorval;
  unsigned int rawRed;
  unsigned int rawGreen;
  unsigned int rawBlue;
  unsigned int normRed;
  unsigned int normGreen;
  unsigned int normBlue;
};

colorType cubeColor[6*9];
Run Code Online (Sandbox Code Playgroud)

我在F#中创建了相同的结构,如下所示:

type colorType =
    struct 
        val colorval: int
        val rawRed: uint16
        val rawGreen: uint16
        val rawBlue: uint16 
        val normRed: uint16
        val normGreen: uint16
        val normBlue: uint16
    end
Run Code Online (Sandbox Code Playgroud)

但我不知道如何调用colorType cubeColor [6*9]; 在F#中.

你可以帮我解决这个案子吗?

谢谢.

c c++ f# struct nxc

-3
推荐指数
1
解决办法
140
查看次数

为什么不允许在其元素中使用结构变量本身?

为什么不允许声明具有自己的标识符的结构作为其元素?作为一个自引用结构,由结构的同一个变量用指针符号声明!

这是我试过的代码:

 #include<stdio.h>
    #include<conio.h>
    struct am
    {
     int a;
     struct am k; // this reported an error
    };
Run Code Online (Sandbox Code Playgroud)

这段代码报告了一个错误,当我用它作为指针接受它时,我搜索了网络,我开始知道它被称为自引用结构

#include<stdio.h>
    #include<conio.h>
    struct ko
    {
     int a;
     struct ko * op; // this was allowed by my compiler
    };
Run Code Online (Sandbox Code Playgroud)

这个结构工作请告诉我!

我正在使用borland international inc的TurboC++ 3.0版.

c c++ struct

-3
推荐指数
1
解决办法
111
查看次数

添加时Struct的地址不一致

我正在处理一个内存,我在分配的块中使用此标头.我正在尝试使用指针算法来返回新区域.这是一个简单的问题.当我将1,2,3添加到整数地址时,编译器会带来下一个第一个,第二个,第三个整数的地址,它们都会因sizeof(int)而异.但是在这段代码中,一个结构的大小为8.而地址的变化是2和8.下面是代码,输出得到:

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

typedef struct header {
int size;
int magic;
}header;

int main() {

int n = 10;
printf("size of int %d\n", sizeof(n));
printf("addr %p\n", &n);
printf("addr+1 %p\n", &n+1);
printf("addr+2 %p\n", &n+2);
printf("addr+3 %p\n", &n+3);

header *h = malloc(sizeof(header));
printf("size of header %d\n", sizeof(header));
printf("addr %p\n", h);
printf("addr+1 %p\n", h+1);
printf("addr+2 %p\n", h+2);
printf("addr+3 %p\n", h+3);


return 0;
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

size of int 4
addr 0xbfeb7898
addr+1 0xbfeb789c
addr+2 0xbfeb78a0
addr+3 0xbfeb78a4

size of header 8
addr …
Run Code Online (Sandbox Code Playgroud)

c ubuntu struct pointers

-3
推荐指数
1
解决办法
43
查看次数

在结构中使用结构

如何使用 struct A 修改 struct B 中的数据。它没有名称,只有一个类型。

struct A {
     struct B;
};

struct B {
     int data;
 };
Run Code Online (Sandbox Code Playgroud)

由于这是学校用的,我无法更改上面的代码。我只能使用它。我为我的主要尝试过这样的事情,但它不起作用

int main (){
      struct A myStruct;
      myStruct.B.data = 3;
      return 0;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢。

编辑:抱歉,我只是想尽快发布此内容,这就是为什么我没有使用正确的 c 语法发布此内容。无论如何,我的问题不够清楚是我的错。

我知道我的 main 不起作用我只是想知道是否有可能访问结构 B 中的数据而不像上面那样在结构 A 中声明它的名称。这是老师给我的代码,所以我不想修改结构体,因为我想也许她想让我们集思广益,像她写的那样使用它。

iharob 解释它的方式通过在结构 A 之前声明结构 B 并实际为结构 B 命名来完美地工作。

是否根本不可能在不给它命名的情况下访问结构 B 中的数据?

c struct nested

-3
推荐指数
1
解决办法
1845
查看次数

在C中定义结构

所以我有这个代码:

 typedef struct node link;
 struct node {int v; link *next;};
 struct graph{int V; int E; link **adj;};
Run Code Online (Sandbox Code Playgroud)

在第一行中,我得到错误"将链接重新定义为不同类型的符号".

在第二行和第三行,我得到"Unkown type name"链接"".

有任何想法吗?

编辑:我将名称链接更改为其他内容并且它可以工作,虽然我的项目中没有任何其他命名链接.

c struct

-3
推荐指数
1
解决办法
69
查看次数

我无法访问数组内部结构

我修好了它!

  typedef struct student
{
char id[11];
}Student;

 #include<stdio.h>
 #include<stdlib.h>
 int main(int argc, char **argv){
 int input = 0, i = 0;
 FILE * fp = fopen("student.txt", "wt");

if (fp == NULL) {
    printf("Error to open student.txt");
    return -1;
}
 scanf("%d",&input);

 student = (Student *)malloc(input*sizeof(Student));

      for(i=0;i<input;i++){
      strcpy(student[i].id, "a"); // A is just for default. 
      fprintf(fp,"%s\n",student[i].id);
 }
 fclose(fp);
 return 0;
 }          
Run Code Online (Sandbox Code Playgroud)

我已经修改了评论和善意的答案.它确实有效

感谢帮助

我真的很感激!这真的很有用.

c printf struct

-3
推荐指数
1
解决办法
59
查看次数

预期的类,委托,枚举,接口或结构(CS1518)

这里简单的代码和我发现的答案似乎不起作用.

我正在使用

SharpDevelop版本:3.2.1.6466 .NET版本:2.0.50727.5485

问题是错误代码Expected类,委托,枚举,接口或结构(CS1518).

有任何想法吗?

Program.cs代码:

using System;
using System.Threading;

namespace Threshold
{
public class Class1
{
    public Class1()
    {
        Heritage YOLO = new Heritage();
        YOLO.Fractal();
    }
}
    static void Main()
    {
       //do nothing
    }
}
Run Code Online (Sandbox Code Playgroud)

它调用的cs文件是:

using System;
using System.Threading;

namespace Threshold
{
public class Heritage
{
int Fractal()
{
    //Do stuff.
}
}

internal partial class DefineConstants
{
    public const string DRIVERPATH = "d:\\tc\\bgi";
}

}
Run Code Online (Sandbox Code Playgroud)

请帮忙修复.谢谢.

c# enums struct

-3
推荐指数
1
解决办法
232
查看次数

C Sizeof char[] 在结构中

我知道什么是填充以及对齐是如何工作的。鉴于以下结构:

typedef struct {
char word[10];
short a;
int b;  
} Test;
Run Code Online (Sandbox Code Playgroud)

我不明白 C 如何解释和对齐结构内的 char 数组。它应该是 9 个字符 + 终止符,它应该被认为是最长的,如下所示:

| - _ - _ - _ - _ - word - _ - _ - _ - _ - |
| - a - | - _ - b - _ - | padding the remaining 4 bytes
Run Code Online (Sandbox Code Playgroud)

“-”代表一个字节,“_”分隔字节。所以我们有 10 字节长的字,2 字节长的 a 和 4 字节长的 b 以及 4 字节的填充。但是当我打印 sizeof(Test) 它返回16.

编辑:我明白了。

c byte struct sizeof

-3
推荐指数
1
解决办法
218
查看次数

标签 统计

struct ×10

c ×8

c++ ×4

byte ×1

c# ×1

enums ×1

f# ×1

function ×1

memcmp ×1

nested ×1

nxc ×1

padding ×1

parameter-passing ×1

pointers ×1

printf ×1

sizeof ×1

ubuntu ×1