标签: struct

不同 C 标准中的结构体值

在不同的 C 标准中应该打印什么 C 代码?

struct X { int a; char b[4]; };

struct X x, y; struct X *px;

printf("%x %x\n", x, &x);   // what mean x in this context?  suppose we get 0x12b45 and 0x34235
printf("%d\n", x == 0x12b45 ? 1 : 0)
px = &x;
x = *px;
printf("%x %x\n", x, *px);
x = y
printf("%x %x\n", x, y);
Run Code Online (Sandbox Code Playgroud)

换句话说,结构体的价值是什么?或者更好地说,结构解析为什么值?使用数组进行类比,数组名称解析为第一个数组项的值,什么值解析为结构名称?

int a[3];

printf("%x %x\n", a, a[0]);  // prints the same value twice

struct s { int a; …
Run Code Online (Sandbox Code Playgroud)

c struct

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

C:将一个指针数组作为参数传递给结构体

这个问题可能已经得到了解答,所以我将简单地解释一下:

我已经定义了一个"列表"结构.在一个单独的函数中,我初始化了一个名为"table"的数组:

int table_size = 500;
struct list* table[table_size];
Run Code Online (Sandbox Code Playgroud)

这将存储指向这些"列表"结构的指针.稍后在函数中,我将此表作为第三个参数传递给另一个函数:

generate(word, table_size, table);
Run Code Online (Sandbox Code Playgroud)

这个"生成"函数按以下方式定义:

void generate(char *str, int table_size, struct list* table)
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,我收到以下错误:

从不兼容的指针类型传递'generate'的参数3 [-Werror]

注意:预期'struct list*'但参数类型为'struct list**'

感谢任何能解释出错的人.

c c++ arrays struct pointers

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

在c中使用带有许多结构的malloc

我试着用我的结构存储很多东西.变量似乎存储在"do-while"中,但是当我尝试访问do-while之后存储的元素时,元素就消失了.我做错了什么?

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

struct Sauto {
int part;

};

int main() {
int i=0;
struct Sauto *car;

do {
    car = malloc(sizeof(struct Sauto)+(sizeof(struct Sauto)*i));
    struct Sauto car[i];

    printf("part%i:", i);
    scanf("%i", &car[i].part);
    printf("part%i is %i\n", i, car[i].part);
    i++;
} while (i<3);
printf("part 0 is %i\n", car[0].part);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c malloc struct

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

计算双向链表中节点的大小

我使用struct在C中创建了一个节点:

struct node{
       int data;
       struct node* left;
       struct node* right;
}
Run Code Online (Sandbox Code Playgroud)

现在我使用以下代码计算节点的大小

printf("%d", size of(struct node));
Run Code Online (Sandbox Code Playgroud)

输出是6,我使用的是Turbo c ++编译器.

请清楚我为什么节点的大小是6?

c struct

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

如何在C中使用自引用结构?

我正在学习C中的自引用结构.以下代码中的错误是什么?我如何使其工作?

#include<stdio.h>

struct student
{
  char grade;
  struct student *ptr_dat;  //we have a pointer of datatype struct student.
};

int main()
{
  struct student data;
  struct student data1;
  data.grade = 'C';
  data1.grade = 'B';

  data.ptr_dat = &data1;  //the address of another structure is assigned. 

  /*
   print the element grade in both structures directly.
  */
  printf("%c\n",data.grade);
  printf("%c\n",data1.grade);
  /*
   how to print the element grade in data1 using pointer
  */
  printf("%c\n",data.(*ptr_dat)); //This is error.

  return 0;

} 
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c struct pointers

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

C中结构大小的解释?

所以我在看这段代码:

#include <stdio.h>

struct Student {
    int id;
    char name[32];
} s, *sp;

int main() {
    printf("sizeof(structStudent) = %u\n", sizeof(structStudent));
    printf("sizeof(s) = %u\n", sizeof(s));
    printf("sizeof(structStudent*) = %u\n", sizeof(structStudent*));
    printf("sizeof(sp) = %u\n", sizeof(sp));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出如下:

sizeof(struct Student) = 36
sizeof(s) = 36
sizeof(struct Student*) = 4
sizeof(sp) = 4
Run Code Online (Sandbox Code Playgroud)

为什么尺寸为struct Student*4,为什么尺寸sp也是4?我的powerpoint没有详细说明这一点.我知道为什么的大小struct Students为36:因为32个char(对于一个字节+4字节int)= 36总.

c struct sizeof

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

为什么为数组结构赋值不工作C.

我正在尝试编写一个收集学生信息的程序.我正在使用一系列学生(结构)

typedef struct {
  char name[50];
  struct Course* course;
}Student;
Run Code Online (Sandbox Code Playgroud)

在我的主要()我这样做了

Student* stds = (Student*) malloc(app.std_cnt * sizeof(*stds) );

getStdData(stds);
Run Code Online (Sandbox Code Playgroud)

这是getStdData函数

void getStdData(struct Student *students){

 int i;
 char name[50];

 Student std;

 printf("\n");

 for(i = 0; i < app.std_cnt; i++){

    printf("std [%i] name : ",i+1);


    scanf("%s",&name);

    strcpy(std.name,name);

    students[i] = std;

 }
}
Run Code Online (Sandbox Code Playgroud)

当我编译我得到

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

gpa.c
gpa.c(124): error C2440: '=': cannot convert from 'Student' to 'Student'
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么?为什么抱怨学生转学?他们不是同一类型?

c arrays malloc struct

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

CONTEXT结构不包含Eip?C++

我正在尝试修改线程,而CONTEXT结构不包含Eip.

HANDLE hThread;
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_ALL;

DWORD processId = GetCurrentProcessId();
DWORD mainThreadId = GetMainThreadId(processId);

hThread = OpenThread((THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME), FALSE, mainThreadId);
if (hThread)
{
    if (GetThreadContext(hThread, &ctx))
    {
        ???ctx.Eip??
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么,尽管它应该.谢谢!

c++ winapi struct

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

如何将结构传递给参数类型有两个星号的函数

让我们有一个名为Employee的结构:

struct Employee
{
    int basicsalary;
    int bonus;
    int netsalary;
};
Run Code Online (Sandbox Code Playgroud)

让我们有一个函数原型如下:

int calc_NetSalary(struct Employee**,int);
Run Code Online (Sandbox Code Playgroud)

让main函数声明结构数组的变量,如下所示:

struct Employee emp[100];
Run Code Online (Sandbox Code Playgroud)

让函数在main中调用如下:

for (i = 0; i < n; i++)
{
    emp[i].netsalary = calc_NetSalary(emp[i],n);    // Warning is here
}
Run Code Online (Sandbox Code Playgroud)

让函数定义如下:

int calc_NetSalary(struct Employee** emp,int n)
{
    int i;
    for(i = 0; i < n; i++)
    {
        emp[i]->netsalary = emp[i]->basicsalary + emp[i]->bonus;
    }
    return emp[i]->netsalary;
}
Run Code Online (Sandbox Code Playgroud)

我不知道作为一个参数传递什么,该函数需要一个带有两个星号的参数(代替emp上面的).我正在收到警告passing argument 1 of ‘calc_NetSalary’ from incompatible pointer type.我知道这一定是一个愚蠢的错误,或者我不清楚指针的概念.请帮忙 !

c struct pointers arguments function

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

指向结构的指针

嗨有一个函数,它接受指针的参数,指向结构的指针.我无法访问我的struct的成员.结构指针的行为与其他类型的指针的行为不同,还是我只是缺少某些必要的东西?

struct mystr {
int num;
};

void fun(mystr **out) {
  printf("%d",**out.num); <-- where the problem arises
}
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

标签 统计

struct ×10

c ×9

pointers ×4

arrays ×2

c++ ×2

malloc ×2

arguments ×1

function ×1

sizeof ×1

winapi ×1