Raw output of a struct - c

I have a simple C program of below type
#include<stdio.h>
struct test{
char a;
char b;
char c;
char d;
int e;
};
void main() {
struct test mem = {0xa, 0xb, 0xc, 0xd, 0xef12};
long int *tmp = (long int *)(&mem.a);
printf("Struct in binary %p : 0x%lx\n", &mem.a, *tmp);
printf("Printing only a member %p : 0x%x\n", &(mem.e), *(int *)(&(mem.e)));
}
The output of above program is as below on a Big endian machine :
Struct in binary 0x7ffc1206abc0 : 0xef120d0c0b0a
Printing only a member 0x7ffc1206abc4 : 0xef12
The question is that why is the raw output of the struct in the reverse order of its members' values?
The address of 'member e' is still 4 bytes from the start of the struct base address..!

Related

C variables in struct have same address each other

define code :
#include <stdio.h>
#include <string.h>
typedef int count_t;
typedef struct ptid_t {
short index;
short ioffset;
unsigned char type;
unsigned char networkType;
} ptid_t;
typedef union ptid_lst {
count_t count;
ptid_t item[1];
} plist_t;
main code :
int main(void) {
plist_t test;
memset(&test, 0x0, sizeof(plist_t));
test.count = 0xABCDABCD;
printf("%x\n", test.count);
printf("%x\n", test.item[0].index);
printf("%x\n", test.item[0].ioffset);
return 0;
}
console output :
abcdabcd
ffffabcd
ffffabcd
I just trying to change struct first value 'count' but other variables are changed.
The change value is 'count' in plist_t. but, why index and ioffset variables are changed both?
Because of this situation, I try to get the variable's address and result :
0x80479f4
0x80479f4
0x80479f6
The 'count' variable and item[0] struct has same address. why occured this situation?
In oppsite case are same too.
int main(void) {
plist_t test;
memset(&test, 0x0, sizeof(plist_t));
test.item[0].index = 0xabcd;
test.item[0].ioffset = 0xabc0;
printf("%x\n", test.count);
printf("%x\n", test.item[0].index);
printf("%x\n", test.item[0].ioffset);
return 0;
}
console output:
abc0abcd
ffffabcd
ffffabc0
Because plist_t isn't a struct, it's a union
In C, each member of a union starts at the same memory address.
If you want to change them independently, simply convert plist_t into a struct instead:
typedef struct ptid_lst {
count_t count;
ptid_t item[1];
} plist_t;

Converting unsigned char pointer of data to struct containing ints

I am trying to cast this pointer of data to my struct and the actual value populate in the struct.
unsigned char *data = "00000001000000020000000300000004AE93KD93KD91Q830DMNE03KEkdaredgreenblueorangeyellow";
typedef struct mystruc {
int a;
int b;
int c;
int d;
} mystruc;
mystruct ms = (mystruct *)data;
printf("%i", ms->a);
Output:
808464432
I am trying to find out how to fill in a, b, c, d with the actual values 1, 2, 3, 4
I would like the output to be:
1
I will also need to later access the rest of the data.
Use sscanf() to parse the numbers in the string.
mystruct ms;
sscanf(data, "%8d%8d%8d%8d", &ms.a, &ms.b, &ms.c, &ms.d);
%8d means to parse an 8-character decimal field as an int. If it's actually hexadecimal, change it to %8x.
Your code is interpreting the character codes in the string as the binary representation of the structure members, it doesn't parse it.
Your unsigned char is one byte wide, so "00000001" will be 3030303031 in hex, because the ASCII code for '0' is 0x30 in hex, and the ASCII for '1' is 0x31.
Your int is 4 bytes wide, so it'll capture the first 4 bytes of data, which will be 30303030 in hex, or 808464432 in decimal.
This, however, will work on a little-endian machine:
#include <stdio.h>
typedef struct mystruct {
int a;
int b;
int c;
int d;
} mystruct;
unsigned char *data = "\1\0\0\0"; // use octal numbers, not ASCII, also note the reversed order
int main(void) {
mystruct *ms = (mystruct *)data;
printf("%i", ms->a); // outputs: 1
}

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

Can't understand the output of a program

can anyone tell me why this program is printing 3 as the output or tell the functionality of the program.
#include<stdio.h>
#include<stdlib.h>
struct st
{
int a;
int b;
};
void func(struct st*);
int main()
{
struct st ab={128,768};
struct st *pq=&ab;
func(pq);
return 0;
}
void func(struct st *p)
{
char *pt;
p->a=768;
p->b=128;
pt=(char*)p;
printf("----%d\n",*(++pt));
}
You have stored 768 in first member of struct st, so address at start of struct has 0x00000300(considering 4 bytes of int) in memory, when viewed as little endian stream it will look as 0x00030000
You have stored its pointer in ptr which is char * which now points to value 0x00, ++ptr will point to next value ie 0x03

Freeing a pointer to a string in a structure [in C]

i'm trying to find the most efficient way to use the 'free' command in a program I made. Basically there are several structers, the first is called Operation. Here's how it's defined -
struct Operation {
unsigned int ic;
unsigned int dc;
struct symbolStruct *externHead;
struct symbolStruct *symbolHead;
struct DataCode *dataHead;
struct MachineCode *machineHead;
int linenumber;
};
It has several pointers to other structers, let's take machineHead for example.
struct MachineCode {
unsigned int row : WORD_SIZE;
unsigned int line;
OperandType *structure;
struct MachineCode *next;
};
And OperandType looks like this -
typedef struct {
unsigned int numOfOperands : 2;
unsigned int addrMethSou : 2;
unsigned int addrMethDest : 2;
unsigned int operation : 4;
unsigned int extraWords : 2;
char *firstOperand;
char *secondOperand;
} OperandType;
What I want to do is to free the strings "firstOperand" and "secondOperand" in structure (which is in machineHead ) and then to free machineHead itself, I tried to write it down using the following code -
void clear(struct Operation *op) {
struct MachineCode *mh = op->machineHead, *fmh;
while(mh != NULL) {
fmh = mh;
mh = mh->next;
free(fmh->structure->firstOperand);
free(fmh->structure->secondOperand);
free(fmh->structure);
free(fmh);
}
But the program crashes in runtime. Is there an elegant way to do it or do I have to make a pointer varibale of every type in order to clear the memory?

Resources