简体   繁体   中英

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

I want to print out and intiliaze a Structure with a pointer and give it to a another function.

My Problem is: How can i intiliaze the structure with the variable name 'register' and print it out to test and see my values and adresses? My Programms goal is basically to simulate a CPU. Therefore i have 3 pointers for the Orders, Stack and the Calculator (its a task given by our teacher). But im not so good with structures and pointers

#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;
}

Because the program says:

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

To make it work rewrite the code as follows :

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"

If you want the dinamic allocation of the memory you can do this:

#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;
}

else, if you don't need dinamic allocation of memory:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM