Write into two slots of array at once

Hello i have a little problem.
I want to create a char array and want to store a 2 bytes long number (unsigned short) into it at the declaration time.
The number is not known, it is calclulated or changed time to time.

this is the code,
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <sys/types.h>

/*-- PREPROCESSOR SUBSTITIUTIONS --*/
// Core Basics
#define MEMSIZE 50000	// Size of the Memory.
#define MAS 4		// Minimum Allocation Size Permited in Bytes.
#define MALLOCDATA 6	// Size of the data section of the Memory in Bytes.

/*-- MEMORY DEFINITION AND DATA SECTION INITIALIZATION --*/
char mem[MEMSIZE] = {?, ?, 0, 0, 0, 0, ?, ?};


In the places that i have put question(?) marks there should be the 2Byte numbers. as you can see i want to write the number MALLOCDATA at the first two byts (mem[0],mem[1]) of the array and calculated number (MEMSIZE - MALLOCDATA) in the 7th and 8th bytes (mem[6],mem[7]).
I dont want to change the array type as char gives me the ability to manipulate every byte of it seperately.
I also dont want to use some initialization function because i want this to be very efficient.
I cannot put this code inside of any function.
I am using C language not C++.
Thanks.
OK so if i understand you correctly, you want the first two bytes to represent an unsigned short that has the value 6, and the last two to represent 49994 (50000 - 6). if you replace the first two question marks with 6 and then 0, then the second two with 74 and 195 you will get the correct result. You can calculate these numbers yourself by converting the original numbers to 16-bit binary, then splitting that into two eight bit sections, and converting those 8 bit sections back into decimal to store in your char array. You can check for yourself by using a little bit of code like this:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    unsigned short num;
    char mych[] = {6, 0, 74 ,195};

    memcpy(&num, mych, 2);

    printf("the first number is: %d\n", num);
    memcpy(&num, mych + 2, 2);
    printf("the second number is: %d\n", num);
    getchar();
}
yes my friend, i know that. but thats why i say the number can change. so i dont want to go grab calculater and turn decimal to hexa and replace them.
i think there is a way to do this at least with macros.
do you know a way to do this with macro, i mean ,

break the hexa decimal number
0x0006 to 0x06 , 0x00
and
0xC350 to 0x50 , 0xC3
char mem[MEMSIZE] = {unknownNumber>>8,unknownNumber&255, 0, 0, 0, 0, unknownNumber2>>8,unknownNumber2&255};
ooh.. thanks athar
i will try that.
nice work..
thanks..
Cheers everyone..
hey friends... i got the way to write solved...
but, how can i read them as unsorted short values(2 byte numbers)...????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <sys/types.h>

/*-- PREPROCESSOR SUBSTITIUTIONS --*/
// Core Basics
#define MEMSIZE 50000	// Size of the Memory.
#define MAS 4		// Minimum Allocation Size Permited in Bytes.
#define MALLOCDATA 6	// Size of the data section of the Memory in Bytes.

// Pointers in Data Section
#define FREEHEAD 0		// Works as the Head Pointer of the linked list of free spaces
#define PREVIOUS 2		// Previous Pointer
#define CURRENT 4		// Current Pointer

/*-- MEMORY DEFINITION AND DATA SECTION INITIALIZATION --*/
//	Little Endian Architecture assumed
char mem[MEMSIZE] = {(unsigned short)(MALLOCDATA + 2 ) & 255, (unsigned short)(MALLOCDATA + 2 ) >> 8,\
 0, 0, 0, 0, (unsigned short)(MEMSIZE - MALLOCDATA) & 255,\
 (unsigned short)(MEMSIZE - MALLOCDATA) >> 8, 2, 5 };

void traverse()
{
	// Pre requisitions for the traversal.
	mem[PREVIOUS] = 0;		// Set The,
	mem[PREVIOUS+1] = 0;	//    Previous => 0
	mem[CURRENT] = mem[FREEHEAD];		// Set The,
	mem[CURRENT+1] = mem[FREEHEAD+1];	//	  Current => Head

	// Traverse and locate for an appropiate location.
	while( mem[CURRENT] || mem[CURRENT+1] )	// Untill Current is not NULL
	{
		/*- Go to the next location -*/
		// Previous = Current
		mem[PREVIOUS] = mem[CURRENT];
		mem[PREVIOUS+1] = mem[CURRENT+1];
		// Current = *Previous
		(unsigned short)mem[CURRENT] = (unsigned short)mem[ mem[PREVIOUS] ];
	}
}


i have stroked-through the line that i want to do... the stroked-through line shall give you the idea which i want.....
How can i retreave 2byte number from this char array??
i know hot to do it by creating a short * Pointer....
but i have to do this without creating any more variables, but i am allowed to use any space in the char array as variable space...
Its ok...
I Figured out the solution
i should use the array identifier as a pointer and type cast the address to unsigned short( or short).

 
*(unsigned short *)(mem+CURRENT)


cheers..
That's non-portable, as it would not work on big-endian systems.
Just reverse the initial process: mem[CURRENT]<<8 | mem[CURRENT+1];
I also have the feeling that you need to have a look at bitwise operators again.
ya.. i am not very used to bitwise operators in C.. Thanks Athar...

but i have a question,
Why is it not going to work on Big Endians..?

When i was writing the line 17, i thought that this will only work for little endians... am i wrong...?

can your code work on both..?

Please, can you explain..

and again i have to say,
Your code is awsome.. i was thinking the other way around, with NASM in my head, that i should try to make the size of the operation to 16 bits..

cheers..
Topic archived. No new replies allowed.