Are there differences between int pointer and char pointer in c?
short answer: NO
All pointers in C are created equal in the last decades. During some time there were pointers of different sizes, in some platforms like Windows, due to a thing called memory model.
Today the compiler sets the code to use 32-bit or 64 or any size pointers depending on the platform, but once set all pointers are equal.
What is not equal is the thing a pointer points to, and it is that you are confused about.
- consider a
void*
pointer. malloc()
allocates memory in C. It always return a void*
pointer and you then cast it to anything you need
sizeof(void*)
is the same as sizeof(int*)
. And is the same as sizeof(GiantStruct*)
and in a 64-bit compiler is 8 bytes or 64 bits
what about char* c = "c"?
- you must ask yourself what is
"c"
. is is char[2]
. And it is a string literal. Somewhere in memory the system will allocate an area of at least 2 bytes, put a 'c'
and a zero there, since string literals are NULL
terminated in C
, take that address in put in the address allocated to your pointer c
.
what about int* a = 10
?
- it is the same thing. It is somewhat ok until the operating system comes in. What is
a
? a is int*
pointer to an int
. And what is *a
? an int
. At which address? 10
. Well, truth is the system will not let you access that address just because you set a pointer to point to it. In the '80s you could. Today there are very strong rules on this: you can only access memory allocated to your processes. Only. So the system aborts your program as soon as you try to access.
an example
if you write
int* a = malloc(300);
the system will allocate a 300-bytes area, take the area address and write it for you at the address allocated to a. When you write *a=3456
the system will write this value to the first sizeof(int)
bytes at that address.
You can for sure write over all 300, but you must somewhat manipulate the pointer to point inside the area. Only inside that area.
In fact C language was designed just for that.
Let us write "c" to the end of that 300-byte area alocated to a:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char* c = "c";
int* a = (int*)malloc(300);
*a = 3456;
printf("Area content is %d\n", *a);
char* pToEnd = (char*)a + 298;
*(pToEnd) = 'c';
*(pToEnd + 1) = 0;
printf("At the end of the 300-byte area? [Should be 'c'] '%s'\n", pToEnd);
printf("The pointer c points to '%s'\n", c);
//*c = 'x'; // error: cancel program
c = pToEnd;
printf("The pointer c now points do the end of the 300-byte area:'%s'\n", c);
free(a);
return 0;
};
and see
Area content is 3456
At the end of the 300-byte area? [Should be 'c'] 'c'
The pointer c points to 'c'
The pointer c now points do the end of the 300-byte area:'c'
Also note that as soon as you try to write over c
, the char
pointer in your example, your program will also cancel. It is a read only area --- see the comment on line 14. But you can point c
inside the area in the example and use it, since the pointer in itself is not constant.
And when you write c = pToEnd
in the example above access to the string literal is lost forever. In some sense is a memory leak, but it is a static allocated area. The leak is not the area but the address.
Note also that free(a)
at the end, even if being a
int*
will free all 300 bytes