I am trying to get the bytes and pointers and how they are stored can any one explain or answer some of my questions. Thank you
int num = 513; <-- allocating a 4 bit memory by initializing
//[01][02][00][00] <-- (numbers are sorted and shown as litle endian)
char * ptr = # //char is (one byte)
↓
//[01][02][00][00]
// pointer always start from the [0] (as in this array byte length)
// in the allocated address in the memory ptr[0] is in this case = [01]
// (printed as %x02 "printf("the byte %02x\n",ptr[0]);" - if it's only
//single number 1 a zero will be added on the length so it prints out as 01)
int * ptr = # //now creating a pointer with the type of int (four bytes)
↓ ↓ ↓ ↓
//[01][02][00][00]
- how can i access the first byte of this int pointer? [question01]
- is there a way to see the bites inside the of the first byte([01])? [question02]
- where does the pointer save the address? does it have to allocate a memory space in the ram to save whe address such as 0x233828ff21 and if so this(0x233828ff21) address requires a lot of bytes? [question03]
- where does this int pointer stores it's type length (4bytes)? [question05]
- what happens if i declare a type with longer byte memory allocation such as
long long * ptr = #[01][02][00][00][00][00][00][00] since i am pointing a long long to a 4 byte int, can those 4 last already been allocated by another program and in use? can i read it? [question06] - binary are only 0 and 1 and whether one of those(0 or 1) is called a bite? [question07]
- one byte is 8 bits right? why am i getting 16 bits 0000000000000001 when converting the number 1 in this website (https://www.rapidtables.com/convert/number/decimal-to-binary.html) shouldn't it be 8? [question08]
Generally, it is preferable to use
unsigned charrather thancharto access arbitrary bytes, so let’s do that.After
unsigned char *ptr = #,ptris a pointer tounsigned char, and you could access the first byte of theintwith*ptrorptr[0], as inprintf("The first byte, in hexadecimal, is 0x%02hhx.\n", *ptr);.If instead you have
int *ptr = #, there is no direct way to access the first byte.ptrhere is a pointer to anint, and, to access an individual byte, you need a pointer to anunsigned charor other single-byte type. You could convertptrto a pointer tounsigned char, as with(unsigned char *) ptr, and then you can access the individual byte with* (unsigned char *) ptr.The C standard does not provide a way to display the individual bits of a byte. Commonly programmers print the values in hexadecimal, as above, and read the bits from the hexadecimal digits. You can also write your own routine to write binary output from a byte.
A pointer is a variable like your other
intandcharvariables. It has space of its own in memory where its value is stored. (This model of variables having memory is used to specify the behavior of C programs. When a program is optimized by a compiler, it may change this.)In current systems, pointers are commonly 32 or 64 bits (four or eight 8-bit bytes), depending on the target architecture. You can find out which with
printf("The size of a 'char *' is %zu bytes.\n", sizeof (char *));. (The C standard allows pointers of different types to be different sizes, but that is rare in modern C implementations.)The compiler knows the sizes of pointers. The pointer itself does not store the length of the thing it is pointing to. The compiler simply generates appropriate code when you use the pointer. If you use
*ptrto get the value that a pointer points to, the compiler will generate a load-byte instruction if the type ofptrischar *, and it will generate a load-four-byte instruction of the type ofptrisint *(andintis four bytes in your C implementation).When
long longis an eight-byte integer, and you have along long *ptrthat is pointing to a four-byte integer, the C standard does not define the behavior when you attempt to use*ptr.In general-purpose multi-user operating systems, the memory after the
intcannot be allocated by another program (unless this program and the other program have both arranged to share memory). Each process is given its own virtual address space, and their memory is kept separate.Using this
long long *ptrin your program may access memory beyond that of theint. This can cause various types of bugs in your program, including corrupting data and alignment errors.One binary digit is a “bit”. Multiple binary digits are “bits”.
The smallest group of bits that a particular computer operates on as a unit is a “byte”. The size of a byte can vary; early computers had bytes of different sizes. Modern computers almost all use eight-bit bytes.
If your program includes the header
<limits.h>, it defines a macro namedCHAR_BITthat provides the number of bits in a byte. It is eight in almost all modern C implementations.The web site is not merely converting to one byte.
It seems to show at least 16 bits, choosing the least of 16, 32, or 64 bits that the value fits in as a signed integer type.