- #1
RedX
- 970
- 3
In C, why do you have to define a pointer type? For a 32 bit computer, the address is 4 bytes long no matter what, so it shouldn't matter if the pointer is to a char or an int, yet I get a compiler warning when I use a char pointer to point to the address of an int.
Also, I don't quite understand how this program works:
main(){
char* ptr;
ptr="this is a test";
printf("%s",ptr);
}
This program prints out "this is a test", but how is a pointer to a char equal to a string? I thought pointers stored an address, and not values of data? It would make a little more sense if the pointer were dereferenced:
main(){
char* ptr;
*ptr="this is a test";
printf("%s",*ptr);
}
but that still doesn't make sense as I never specified an address for the pointer to point to, so where exactly does the computer store "this is a test"?
Also, I don't quite understand how this program works:
main(){
char* ptr;
ptr="this is a test";
printf("%s",ptr);
}
This program prints out "this is a test", but how is a pointer to a char equal to a string? I thought pointers stored an address, and not values of data? It would make a little more sense if the pointer were dereferenced:
main(){
char* ptr;
*ptr="this is a test";
printf("%s",*ptr);
}
but that still doesn't make sense as I never specified an address for the pointer to point to, so where exactly does the computer store "this is a test"?