簡體   English   中英

如何使用指針初始化和打印結構並將其提供給函數?

[英]How can i initialize and print out a Structure with a pointer and give it to a function?

我想打印出一個帶有指針的結構並將其賦予另一個函數。

我的問題是:如何使用變量名稱“注冊”來初始化結構並將其打印出來以測試並查看我的值和地址? 我的程序目標基本上是模擬 CPU。 因此,我有 3 個指向訂單、堆棧和計算器的指針(這是我們老師給的任務)。 但我不太擅長結構和指針

#include<stdio.h>
#include<stdint.h>
#include<stdbool.h>

struct reg {
  unsigned char pc; // Befehlszeiger
  unsigned char sp; // Stapelzeiger
  unsigned char fa; // Flags + Akkumulator
};

bool cpu(struct reg *registers, unsigned char data[128], uint16_t cmd[256]);


int main(){
unsigned char data[128];
uint16_t cmd[256];
cmd[127] = 12;
data[127] = 'D';
data[128] = 'Z';
struct reg *registers = { '1', '2', '3'};
printf("The number before function: %d\n", registers->pc);
cpu(registers, data, cmd);
return 0;
}

bool cpu(struct reg *registers, unsigned char data[128], uint16_t cmd[256])
{
printf("The number in function: %d\n", registers->pc);
return 0;
}

因為程序說:

|20|warning: initialization makes pointer from integer without a cast [-Wint-conversion]|
20|note: (near initialization for 'registers')|
20|warning: excess elements in scalar initializer|
|20|note: (near initialization for 'registers')|
c|20|warning: excess elements in scalar initializer|
c|20|note: (near initialization for 'registers')|

要使其工作,請按如下方式重寫代碼:

struct reg registers = { '1', '2', '3'};  // "registers" isn't a pointer
printf("The number before function: %d\n", registers.pc); // "." instance of a struct
cpu(&registers, data, cmd);// pass the address of "registers"

如果你想要內存的動態分配,你可以這樣做:

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

struct reg {
  unsigned char pc; // Befehlszeiger
  unsigned char sp; // Stapelzeiger
  unsigned char fa; // Flags + Akkumulator
};

bool cpu(struct reg *registers, unsigned char data[128], uint16_t cmd[256]);


int main(){
unsigned char data[128];
uint16_t cmd[256];
cmd[127] = 12;
data[127] = 'D';
data[128] = 'Z';
struct reg *registers;
registers = (struct reg*)malloc(sizeof(struct reg));
registers->pc = '1';
registers->sp = '2';
registers->fa = '3';

printf("The number before function: %d\n", registers->pc);
cpu(registers, data, cmd);
if(registers){
    free(registers);
}
return 0;
}

bool cpu(struct reg *registers, unsigned char data[128], uint16_t cmd[256])
{
printf("The number in function: %d\n", registers->pc);
return 0;
}

否則,如果您不需要動態分配內存:

#include<stdio.h>
#include<stdint.h>
#include<stdbool.h>

struct reg {
  unsigned char pc; // Befehlszeiger
  unsigned char sp; // Stapelzeiger
  unsigned char fa; // Flags + Akkumulator
};

bool cpu(struct reg *registers, unsigned char data[128], uint16_t cmd[256]);


int main(){
unsigned char data[128];
uint16_t cmd[256];
cmd[127] = 12;
data[127] = 'D';
data[128] = 'Z';
struct reg registers = { '1', '2', '3'};
printf("The number before function: %d\n", registers.pc);
cpu(&registers, data, cmd);
return 0;
}

bool cpu(struct reg *registers, unsigned char data[128], uint16_t cmd[256])
{
printf("The number in function: %d\n", registers->pc);
return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM