how to do this using pointer? (codevision avr)
thank :)
how copy int to char[2] using pointer?
you can use union
union i2c
{
char c[2];
int i;
};
please show example how convert value 0xefda to c[2]
At least 3 ways I can think of.
1) Union, as mentioned
2) Explicit memcpy
3) Implicit memcpy, using a casted pointer
At least 3 ways I can think of.1) Union, as mentioned
2) Explicit memcpy
3) Implicit memcpy, using a casted pointer
"There must be 50 ways to love your lever..."
(we don't know the byte order, nor why one might want to send arbitrary "int" values--does OP really want itoa()? [see https://www.avrfreaks.net/index.p... ] is the data really signed?)
4) c[0] = (unsigned char)i; c[1] = (unsigned char)((unsigned int)i >> 8);
char c[2];
c[0] = (char)(0xefda & 0xff);
c[1] = (char)((0xefda >> 8 )&0xff);
--------------------------------or-------------
int i = 0xefda;
char c[2];
char *pc;
pc = &c[0];
*pc = (char)(i & 0xff);
pc++;
*pc = (char)((i >> 8 )& 0xff);
char* itoa( int __val, char * __s, int __radix )
The function itoa() converts the integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.
Note:
The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.
Warning:
If the buffer is too small, you risk a buffer overflow.
Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after '9' will be the letter 'a'.
If radix is 10 and val is negative, a minus sign will be prepended.
The itoa() function returns the pointer passed as s.
@pardhu4poori
I'm pretty sure the OP is not looking for a printable string representation of the integer, since two chars will not suffice for this.
Rather, it seems his question is "how to split a (16-bit) integer into two (8-bit) chars?".
Suppose you have an integer, you can declare a pointer to an array of 2 char:
int my_int; char (*my_array_p)[2]; ... // then inside main my_int=0xABCD; // give a value to the integer my_array_p= &my_int // assign the address of my_int //to read you have to do: (*my_array_p)[0]; //low byte (0xCD) (*my_array_p)[1]; //high byte (0xAB)
if you want to copy the contents to another char array
char my_array[2]; my_array[0]=(*my_array_p)[0]; my_array[1]=(*my_array_p)[1];
Be careful how you declare and how you read the pointer,they should be the same as my example.
char (*my_array_p)[2]; // this is a pointer to an array of 2 char
char *my_array_p[2]; // this is an array of 2 char pointers
Alex
char c[2];int i = 0xefda;
char c[2];
char *pc;
pc = &c[0];*pc = (char)(i & 0xff);
pc++;
*pc = (char)((i >> 8 )& 0xff);
this has the same result as
int i = 0xefda; char c[2]; char *pc; pc=&i; c[0]=*pc; pc++; c[1]=*pc;
but you will get a few warnings from the compiler,
assuming you know what you are doing you can ignore them.
Alex
int ival; int * p_ival; char c[2]; p_ival = (int *)c; // (int *)&c[0] is more explicit *p_ival = ival;
This is the implicit memcpy using a casted pointer that I mentioned before.