struct pointer manipulation - c

The Code used
#include<stdio.h>
struct st
{
char a;
short c;
int b;
};
struct st s1;
int main()
{
printf("%p %p \n",(&s1.b)-1, &s1);
}
If I print the address of &s1.b it prints 0x804a01c and &s1.b-2 prints 0x804a018
why it is printing same address 0x804a01c if i select &s1.b-1 ?

There's probably something wrong with your printing code.
#include <stdio.h>
struct st
{
char a;
short c;
int b;
};
struct st s1;
int main() {
printf("%p\n", (void*)(&s1.b));
printf("%p\n", (void*)(&s1.b - 1));
printf("%p\n", (void*)(&s1.b - 2));
}
Output:
0x403024
0x403020
0x40301c

Most likely you're printing it wrong:
#include <stdio.h>
struct st
{
char a;
short c;
int b;
};
struct st s;
int main(void)
{
printf("s: %p\n", (void *)&s);
printf("s.a: %p\n", (void *)&s.a);
printf("s.b: %p\n", (void *)&s.b);
printf("s.b-1: %p\n", (void *)(&s.b-1));
printf("s.b-2: %p\n", (void *)(&s.b-2));
return 0;
}
Prints for me:
s: 0x100001068
s.a: 0x100001068
s.b: 0x10000106c
s.b-1: 0x100001068
s.b-2: 0x100001064
Things to note:
Pointer to struct == pointer to struct's first element (guaranteed by the C standard),
I am printing the pointers with "%p" format string. "%p" needs void *, and since printf is a variadic function, I need to cast the arguments to printf to void * in this case.
What does the above program print for you?
Edit: based upon the actual code posted for printing that you posted later: you are not getting the same value for &s1.b and &s1.b-1. You are getting the same value for &s1.b-1 and &s1. The answer to that is: this happens because of chance. In your case, there is struct padding, and sizeof(short)+sizeof(char) happens to be ≤ sizeof(int). If you were on a machine where any of those assumptions were invalid, you wouldn't see that behavior. I am sure if you changed char or short to int in your code, &s1.b-1 would not equal &s1, when printed.
Finally, you should cast pointers to void * before printing:
printf("%p %p \n",(void *)((&s1.b)-1), (void *)&s1);

If the address of s1.b is 0x804a01c, then &s1.b-2 should be 0x804a014 (assuming that int is 4 bytes), not 0x804a018. Perhaps you made a mistake when you reported the address?

Thank you for posting your code. Now I see the issue. It is because of padding. To wit:
printf("sizeof(char): %d\n", sizeof(char));
printf("sizeof(short): %d\n", sizeof(short));
printf("sizeof(int): %d\n", sizeof(int));
printf("sizeof(struct st): %d\n", sizeof(struct st));
On my machine this prints
1
2
4
8
You might think, shouldn't sizeof(struct st) be 1 + 2 + 4 = 7? That's certainly a reasonable thought but because of alignment issues there is padding between a and c. Therefore, in memory, the struct looks like the following (relative to the first byte of the struct):
0x00000000: char a
0x00000001: padding
0x00000002: first byte of short c
0x00000003: second byte of short c
0x00000004: first byte of int b
0x00000005: second byte of int b
0x00000006: third byte of int b
0x00000007: fourth byte of int b
Consequently (relative to &s1):
&s1.b - 1 is ((long)&s1.b) - sizeof(int) = 4 - 4 = 0 = &s1
This is why both &s1 and &s1.b - 1 will print the same address. In particular if
&s1 = 0x804a01c
then
&s1.b = 0x804a01c + 0x00000004 = 0x804a020
and
&s1.b - 1 = 0x804a020 - 0x00000004 = 0x804a01c
and
&s1.b - 2 = 0x804a020 - 0x00000008 = 0x804a018
Note, finally, that this is implementation-specific behavior. This is not portable!

Related

Confusion regarding pointer size

This may be a noob question ..but this is really confusing me..
Below is some sample code
void main() {
int a = 300;
char *ptr = &a;
int *intptr = &a;
printf("%d\n %d\n", *ptr, *intptr);
}
output:
44
300
As per my understanding,why dereferencing *ptr prints 44 is due to the fact that char pointer is one byte and so it reads only 8 bits from the address of int...
But this question:What is the size of a pointer? states Regardless of what data type they are pointing to, they have fixed size
Am i missing some thing.Why dereferencing char pointer prints 44,if pointer size is same ?
Object pointers (e.g. pointers to anything besides a function) are typically the same size on most systems you're likely to come across, however there's no guarantee of that. That being said, even though the pointers may be the same size, the types that they point to are not.
For example, on a 64-bit Windows system, pointers are typically 8 bytes in size. In your example you have char * and an int * which are most likely both 8 bytes. The difference here is that dereferencing a char * will read/write 1 byte while dereferenceing an int * will read/write 4 bytes (assuming an int is 32 bit).
Assuming little endian byte ordering, a looks like this in memory:
------------------
a | 44 | 1 | 0 | 0 |
------------------
Both ptr and intptr contain the address of a. When dereferencing ptr, which is of type char *, it only looks at the first byte. In contrast, when dereferencing intptr, which is of type int *, it looks at all 4 bytes.
First of all, your code is not valid C and it also invokes undefined behavior, so anything can happen. If your compiler didn't show you any diagnostic messages, you need to uninstall it and get a working one instead. You have the following bugs:
void main() will only work on freestanding implementations that specifically allow this form of main.
char *ptr = &a; is not a valid form of assignment in C. The pointers are not compatible(1).
Printing a character with the %d format specifier invokes undefined behavior.
After fixing these bugs, we get:
#include <stdio.h>
int main (void)
{
int a = 300;
char *ptr = (char*)&a;
int *intptr = &a;
printf("%d\n %d\n", (int)*ptr, *intptr);
}
None of this prints the size of a pointer. You print the contents of the first byte in the int a, which you translate to a char, which may or may not be signed and possibly give an incorrect result (this part is endianess-dependent). And then you print the contents of the whole int a.
What you seem to want is to print the size of the pointers themselves:
printf("%zu %zu\n", sizeof ptr, sizeof intptr);
(1) C11 6.5.16.1 emphasis mine:
Constraints
One of the following shall hold:
/--/
- the left operand has atomic, qualified, or unqualified pointer type, and (considering
the type the left operand would have after lvalue conversion) both operands are
pointers to qualified or unqualified versions of compatible types, and the type pointed
to by the left has all the qualifiers of the type pointed to by the right;
In this statement
printf("%d\n %d\n", *ptr, *intptr);
there are outputted not the pointers themselves but the data they point to.
For example 300 can be represented like 256 + 44. So 44 can be stored in one byte while 256 can be stored in another byte. And this expression *ptr gives the value 44 stored in the byte pointed to by the pointer ptr.
On the other hand the pointer intptr points to the whole object of the type int and the expression *intptr gives the value 300.
If you want to output the addresses stored in the pointers you should write
printf("%p\n %p\n", ( void * )ptr, ( void * )intptr);
To output sizes of the pointers you could write
printf("%zu\n %zu\n", sizeof( ptr ), sizeof( intptr ));
Take into account that according to the C Standard the function main without parameters shall be declared like
int main( void )
Also in this declaration you should use an explicit casting
char *ptr = ( char * )&a;
Here is a demonstrative program
#include <stdio.h>
int main(void)
{
int a = 300;
char *ptr = ( char * )&a;
int *intptr = &a;
printf( "*ptr = %d, *intptr = %d\n", *ptr, *intptr );
printf( "ptr = %p, intptr = %p\n", ( void * )ptr, ( void * )intptr );
printf( "sizeof( ptr ) = %zu, sizeof( intptr ) = %zu\n",
sizeof( ptr ), sizeof( intptr ) );
return 0;
}
Its output might look like
*ptr = 44, *intptr = 300
ptr = 0x7ffe5972613c, intptr = 0x7ffe5972613c
sizeof( ptr ) = 8, sizeof( intptr ) = 8
You introduce undefined behaviour if you cast a pointer to an object to a different type than that of the object AND access it then through this pointer.
Statement int a = 300 introduces an object of type int, char* ptr = &a introduces a pointer to an object of type char but let it point to an object of type int. This per se is not a problem, but dereferencing this pointer then through *ptr is undefined behaviour. And this does not have something to do with the printf - a statement like char x = *ptr would be UB, too. However, a statement like int x = *((int *)ptr) would be OK, as the ptr is casted back to the original type of the object it points to.
Concerning the sizes of pointers: The size of a pointer (i.e. the value necessary for storing a memory address to which the pointer points to) is fixed size; it's usually 4 bytes on a 32 bit system and 8 bytes on a 64 bit system. The size necessary for representing a memory address has nothing to do with the size of the object that resides at the respecite memory address to which the pointer points to.
The following example demonstrates this behaviour:
int main() {
int a = 300;
void *ptr = &a;
int *intptr = &a;
printf("%d %d\n", *((int*)ptr), *intptr);
}
Output:
300 300
Indeed, on a little endian architecture, the LSB would appear first in memory, thus to store 300, memory looks like
44 , 1 , 0 , 0 (for a size 4 int)
The
printf("%d\n %d\n", *ptr, *intptr);
doesn't print the pointer sizes (which are likely the same), it prints the dereferenced pointers, values of one byte, a char, then the int value.
To print the pointer sizes
printf ("%zu\n%zu\n", sizeof(ptr), sizeof(intptr));
Code
#include <stdio.h>
#include <stdint.h>
unsigned int
trans(unsigned char c){
if ('0' <=c && c <= '9') return c - '0';
if ('A' <=c && c <= 'F') return c - 'A' + 0x0A;
if ('a' <=c && c <= 'f') return c - 'a' + 0x0A;
return 0;
}
uint16_t
hex_to_uint16(const char* s) {
char *p = (char*) s;
uint16_t v = 0;
while (*p) {
if (p > s) v = v << 4;
v += trans(*p);
p++;
}
return v;
}
int main (void)
{
int n = 1;
// little endian if true
if(*(char *)&n == 1) {printf("little\n");}
int a = 300;
char *ptr = (char*)&a;
int *intptr = &a;
printf("charSize:%zu intSize:%zu\n", sizeof (char), sizeof (int));
printf("PcharSize:%zu PintSize:%zu\n", sizeof (ptr), sizeof (intptr));
//printf("Hex: %x, Decimal: %d\n", 0x2c, (int)0x2c );
//printf("int: %d\n", hex_to_uint16("2c"));
//printf("300H: %x\n", (a));
printf("PcharAddr: %p\nPintAddr: %p\n", ptr, intptr);
printf("int: %d\n", *intptr);
printf("LSB | | | MSB|\n");
printf(" 0 | 1 | 2 | 3 |\n");
printf("------------------------\n");
printf("Dec\n");
printf(" %d %d %d %d\n", *(ptr), *(ptr+1), *(ptr+2), *(ptr+3));
printf("Hex\n");
printf(" %x %x %x %x\n", *(ptr), *(ptr+1), *(ptr+2), *(ptr+3));
printf("To Big Endian:\n%.2x%x%x%x\n", (uint8_t)*(ptr+3), (uint8_t)*(ptr+2), (uint8_t)*(ptr+1), (uint8_t)*(ptr));
///https://stackoverflow.com/questions/19275955/convert-little-endian-to-big-endian
/*
* chux aswer
uint32_t num = 300;
uint8_t b[4];
b[0] = (uint8_t) (num >> 0u);
b[1] = (uint8_t) (num >> 8u);
b[2] = (uint8_t) (num >> 16u);
b[3] = (uint8_t) (num >> 24u);
printf("%x%x%x%x\n", b[3], b[2], b[1], b[0]);
*/
return 0;
}
Compile, Run
gcc -Wall -Wextra te.c -o te && ./te
no, no, no
As per my understanding,why dereferencing *ptr prints 44 is due to the fact that char pointer is one byte and so it reads only 8 bits from the address of int...
char is one byte, not char pointer. If you want to print pointer you should
printf("%p\n %p\n", ptr, intptr);

Manually stepping through a struct in C

I don't understand why I can't print out ints b and c in this way:
#include "stdio.h"
typedef struct{
int a;
int b;
int c;
} myStruct;
int main(){
myStruct MS;
MS.a = 13;
MS.b = 27;
MS.c = 39;
myStruct* pMS = &MS;
printf("pMS = %u\n", pMS );
printf("&a = %u\n", &pMS->a ); // addr of a is addr of struct
printf("&b = %u\n", &pMS->b ); // addr of b is +4 from a
printf("&c = %u\n", &pMS->c ); // addr of c is +8 from a
printf("*pMS = %d\n",*(pMS) );
printf("*pMS+4 = %d\n",*(pMS+4) );
printf("*pMS+8 = %d\n",*(pMS+8) );
}
The terminal shows bogus values for b and c (at least, I think b and c should be located at pMS+4 and pMS+8):
gcc version 4.6.3
pMS = 1926301980
&a = 1926301980
&b = 1926301984
&c = 1926301988
*pMS = 13
*pMS+4 = 32765
*pMS+8 = 32765
pMS+4 is not the address of the integer located four bytes beyond the start of the structure. Additions to pointers are scaled based on the pointer type so, if it were a pointer to an integer, it would be four integers beyond the start (16 bytes if you have 32-bit ints). See, for example:
int someInt[2] = {4, 9}; // assume 4-byte int, big-endian
int *x = &someInt[0];
// | someInt[0] # 0x1000 | someInt[1] # 0x1004 |
// | 0,0,0,4 | 0,0,0,9 |
// | x = 0x1000 | x+1 = 0x1004 |
// | *(x) = 4 | *(x+1) = 9 |
However, it's even worse in your case since your pointer is to the actual structure. That means it's scaling by the size of the entire struct, three complete integers (plus padding if needed).
This line for example
printf("*pMS+4 = %d\n",*(pMS+4) );
When you add to the pointer, it is same as indexing to array, so equal to this:
printf("*pMS+4 = %d\n", pMS[4]);
Of course there is no array, so bogus struct value is passed.
And then printf can't print structs at all, %d prints something non-sensical.
Double undefined behavior, in other words.
By perform some changes on code and implementing what #Jonathan Leffler saying on comments this way works for me.
Code
#include <stdio.h>
struct t{
int a;
int b;
int c;
};
int main(){
struct t MS;
MS.a = 13;
MS.b = 27;
MS.c = 39;
struct t* pMS = &MS;
printf("pMS =%p\n", pMS );
printf("&a = %p\n", &pMS->a ); // addr of a is addr of struct
printf("&b = %p\n", &pMS->b ); // addr of b is +4 from a
printf("&c = %p\n", &pMS->c ); // addr of c is +8 from a
printf("*pMS.a = %d\n", (* ((int *) ((void *) pMS))));//13
printf("*pMS.b = %d\n", (* ((int *) ((void *) pMS)+1)));//27
printf("*pMS.c = %d\n", (* ((int *) ((void *) pMS)+2)));//39
return 0;
}
But also this works, as you see in #hyde answer working with this way not good. structures in C are packed depending on CPU architecture, Compiler means putting struct members in packed form, for example below struct takes 8 Bytes instead of 5.
struct example {
int a;
char b;
};
I use my way because i checked before, sizeof(void *), sizeof(int *) both return 4.
Consider these lines:
printf("*pMS.a = %d\n", (* ((int *) ((void *) pMS))));//13
printf("*pMS.b = %d\n", (* ((int *) ((void *) pMS)+1)));//27
printf("*pMS.c = %d\n", (* ((int *) ((void *) pMS)+2)));//39
Why using +1 inside parenthesis cause accessing to our integers?
On my system integers numbers occupy 4 bytes, as i expressed above void * and int * have size of same 4 Bytes.
First remove void * to simplify (int *) (pMS) + 1, behave pMS like an integer size, when we add one to it jumps to next for byte address that refers to 27.
I think adding void * is for remove UB from code, as pointer have void * type pointer to structure.
For this case removing void * also works.
At last
printf("pMS =%u\n", sizeof(MS) );//12(3 * 4)
printf("pMS =%u\n", sizeof(pMS) );//4
In my platform there is no any padding or packing, as see above entire structure hold 12 byte 3 integers with 4 bytes size.
But pMs that is a pointer to first address of this structure, as first elements is integer this have 4 byte size.
ToDO
Edit post to add some further explain about pointers type and arithmetic.
Some extra things on difference between two way of using structure in this case.
Structure padding, packing depending on platform, OS, Compiler.
Why we see different address of structure when declare Global or Local inside main, that case different behavior in this case?

Regarding double and triple pointers/double dimension arrays

So, i was playing with C pointers and pointer arithmetic since i'm not entirely comfortable with them. I came up with this code.
char* a[5] = { "Hi", "My", "Name", "Is" , "Dennis"};
char** aPtr = a; // This is acceptable because 'a' is double pointer
char*** aPtr2 = &aPtr; // This is also acceptable because they are triple pointers
//char ***aPtr2 = &a // This is not acceptable according to gcc 4.8.3, why ?
//This is the rest of the code, the side notes are only for checking
printf("%s\n",a[0]); //Prints Hi
printf("%s\n",a[1]); //Prints My
printf("%s\n",a[2]); //Prints Name
printf("%s\n",a[3]); //Prints Is
printf("%s\n",a[4]); //Prints Dennis
printf("%s\n",*(a+0)); //Prints Hi
printf("%s\n",*(a+1)); //Prints My
printf("%s\n",*(a+2)); //Prints Name
printf("%s\n",*(a+3)); //Prints Is
printf("%s\n",*(a+4)); //Prints Dennis
printf("%s\n",*(*(aPtr2) +0)); //Prints Hi
printf("%s\n",*(*(aPtr2) +1)); //Prints My // ap = a, *ap = *a, *(ap)+1 = *a+1 ?
printf("%s\n",*(*(aPtr2) +2)); //Prints Name
printf("%s\n",*(*(aPtr2) +3)); //Prints Is
printf("%s\n",*(*(aPtr2) +4)); //Prints Dennis
char*** aPtr2 = &a is not acceptable according to gcc 4.8.3, why?
Sorry forgot to add compiler warning:
warning: initialization from incompatible pointer type [enabled by default]
It maybe unclear what I'm trying to say, so I had to add this links:
This is the code that works: http://ideone.com/4ePj4h. (line 7. commented out)
This is the code that does not work: http://ideone.com/KMG7OS. (line 6. commented out)
Notice the commented out lines.
a is the address of a buffer of 5 ptrs and is immutable (i.e. its a fixed ptr). If you allowed
char ***aPtr2 = &a;
then
*aPtr2 = &a[3];
would actually modify the address a (which is verboten).
This comes from the way C treats arrays and addresses:
int a[5];
a is of the type int * const, true, meaning you can use it where a pointer is expected. However, on the stack the space for the pointer isn't allocated, only the space for the five ints. Meaning a is the same as &a[0]. This is all expected, but here comes the weird part:
a is the same as &a. This is because the pointer isn't stored anywhere, you can't get it's address. But instead of failing the compile, the C standard just says that they are the same, so some arithmetic will work.
However, since you are doing char ***aPtr2 = &a;, you are effectively doing char ***aPtr2 = a;, which is why you get a segfault. It is only a double pointer, not a triple one.
You can inspect all the values in the debugger to see it more clearly, or run a program such as:
#include <stdio.h>
int main(void)
{
char *a[5] = { "Hi", "My", "Name", "Is" , "Dennis"};
char **aPtr = a;
char ***aPtr2 = &aPtr;
char ***aPtr3 = &a;
printf("%p %c\n", a, *a);
printf("%p %p %c\n", aPtr, *aPtr, **aPtr);
printf("%p %p %p %c\n", aPtr2, *aPtr2, **aPtr2, ***aPtr2);
printf("%p %p %c\n", aPtr3, *aPtr3, **aPtr3);
return 0;
}
which produces the output:
0xfff65578 H
0xfff65578 0x8048648 H
0xfff65574 0xfff65578 0x8048648 H
0xfff65578 0x8048648 H
EDIT: Another interesting thing is the function pointers are treated in the same fashion. &func is the same as func.
int add(int a, int b) { return a + b; }
typedef int (*PFUNC)(int, int);
PFUNC p1 = add;
PFUNC p2 = &add;
if (p1 == p2)
printf("Huh. %d, %d\n", (*p1)(1,2), p2(3, 4)); // the dereference is also optional

Why does second printf print 0

#include<stdio.h>
int main()
{
char arr[] = "somestring";
char *ptr1 = arr;
char *ptr2 = ptr1 + 3;
printf("ptr2 - ptr1 = %ld\n", ptr2 - ptr1);
printf("(int*)ptr2 - (int*) ptr1 = %ld", (int*)ptr2 - (int*)ptr1);
return 0;
}
I understand
ptr2 - ptr1
gives 3 but cannot figure out why second printf prints 0.
It's because when you substract two pointers, you get the distance between the pointer in number of elements, not in bytes.
(char*)ptr2-(char*)ptr1 // distance is 3*sizeof(char), ie 3
(int*)ptr2-(int*)ptr1 // distance is 0.75*sizeof(int), rounded to 0
EDIT: I was wrong by saying that the cast forces the pointer to be aligned
If you want to check the distance between addresses don't use (int *) or (void *), ptrdiff_t is a type able to represent the result of any valid pointer subtraction operation.
#include <stdio.h>
#include <stddef.h>
int main(void)
{
char arr[] = "somestring";
char *ptr1 = arr;
char *ptr2 = ptr1 + 3;
ptrdiff_t diff = ptr2 - ptr1;
printf ("ptr2 - ptr1 = %td\n", diff);
return 0;
}
EDIT: As pointed out by #chux, use "%td" character for ptrdiff_t.
Casting a char pointer with int* would make it aligned to the 4bytes (considering int is 4 bytes here). Though ptr1 and ptr2 are 3 bytes away, casting them to int*, results in the same address -- hence the result.
This is because sizeof(int) == 4
Each char takes 1 byte. Your array of chars looks like this in memory:
[s][o][m][e][s][t][r][i][n][g][0]
When you have an array of ints, each int occupies four bytes. storing '1' and '2' conceptually looks more like this:
[0][0][0][1][0][0][0][2]
Ints must therefore be aligned to 4-byte boundaries. Your compiler is aliasing the address to the lowest integer boundary. You'll note that if you use 4 instead of 3 this works as you expected.
The reason you have to perform a subtraction to get it to do it (just passing the casted pointers to printf doesn't do it) is because printf is not strictly typed, i.e. the %ld format does not contain the information that the parameter is an int pointer.

Does sizeof(var) always work in C?

Whenever I want to read or write into a binary file using C, I do use the fread() and fwrite() functions. They need as a parameter the bytes of the datum that is being read or written so I use the sizeof() function. Now the question is:
The books says that I should declare a function like this:
fread(&variable,sizeof(TYPE_OF_VAR),quantity,file);
I've use the following statement which works most of the time but not always:
fread(&variable,sizeof(VARIABLE),quantity,file);
Why does it works sometimes but sometimes it doesn't?
Does it depends on the type of the variable (int, char, etc)?
Does it depends on the quantity of the datum that I use?
The thing to keep in mind is that sizeof is based on what the compiler knows about the type at compile time (ignoring VLA's for now). If you give it a variable, it will use the type of that variable.
So, the only time I can think of where it wouldn't work as you expect is with pointers.
basically what it boils down to is this:
int x[5];
int *y = &x[0];
int *p = malloc(sizeof(int) * 5);
sizeof(x); // == sizeof(int) * 5
sizeof(y); // == sizeof(int *)
sizeof(p); // == sizeof(int *)
This gets tricky when dealing with functions because arrays decay to pointers when passed to a function. Also note that all 3 of these are exactly equivalent:
int func(int *p);
int func(int p[5]);
int func(int p[]);
in all 3, p is a pointer, not an array.
See this Stack Overflow question for a discussion of heap vs. stack: What and where are the stack and heap.
sizeof() on heap elements, like pointers, will return the size of the pointer, not the number of elements that the pointer can store.
However, sizeof() on stack elements: arrays or const char * will return the length of the array or string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 100
int main(int argc, char *argv[])
{
char *a = NULL;
char b[10] = "abcdefghi";
printf("sizeof(b): %zu\n", sizeof(b));
a = malloc(LENGTH + 1);
if (a) {
*(a + LENGTH) = '\0';
memset(a, ' ', LENGTH);
fprintf(stdout, "a (before memcpy):\t[%s]\n", a);
printf("sizeof(a), before: %zu\n", sizeof(a));
memcpy(a, b, sizeof(b) - 1);
fprintf(stdout, "a (after memcpy):\t[%s]\n", a);
printf("sizeof(a), after: %zu\n", sizeof(a));
free(a);
}
return EXIT_SUCCESS;
}
To compile:
$ gcc -Wall sizeofTest.c -o sizeofTest
Output:
$ ./sizeofTest
sizeof(b): 10
a (before memcpy): [ ]
sizeof(a), before: 8
a (after memcpy): [abcdefghi ]
sizeof(a), after: 8
On my platform, a char * points to a memory address that takes up eight bytes.
It will always work.
int main()
{
char a[10];
int b[10];
printf("%d %d %d %d %d %d",sizeof(char),sizeof(int), sizeof(a),sizeof(b), sizeof(a[0]), sizeof(b[0]) );
}
Try the above code. You should see (depending on compiler, mine has char as 1byte and integer as 4 bytes) 1,4,10,40,1,4
It is not different for fread or fwrite or wherever you use it.

Resources