-4

i have a basic question, Like C says a char object will be 1 byte so in case of an unsigned char the range is from 0 to 255

But in case of a char pointer how 1 byte variable can store more than 255 ??

like in the below program as illustration i find the a character pointer p store a value of 2358855 > 255 ??

(where i know the value 2358855 is the address, but how can a 1 byte object store more than 255 )??

Can any one help on this ??enter image description here

  • 1
    Pointer to char (`char*`) is a **pointer** (an address), which is different than `char`. Any pointer (not just to char) has 8 bytes (or 4 bytes on 32bit programs) - and thus can store lots of values. If you want the *pointed value* (which in this case is the `char`) back from the pointer, you must *dereference* it (`printf("dereferenced pointer=%c\n", *p);`). – CristiFati Aug 29 '18 at 18:41
  • 3
    Please *have the text verbatim in the question - both the code and the output*, not a screenshot. – Antti Haapala -- Слава Україні Aug 29 '18 at 18:41
  • Aside: please note that you should not try to print a pointer as an `int`. It should be `printf("p=%p\n", (void*)p);` – Weather Vane Aug 29 '18 at 18:46
  • Hi @CristiFati : Actually i wanted to know how does pointer internally works ? like if char variable can only store 1 byte --> 255 , then how does pointer variable works interanlly to store more than 255 ?? how does it work internally – Manoj Kumar Pandit Aug 29 '18 at 18:54
  • The pointer is a **different** variable (`p` which is 8 bytes long). The only link between the pointer and the pointee (in our case the `char` variable) is that the pointer variable value is the pointee's address. I'm sure *Google* would yield tons of tutorials regarding this matter. – CristiFati Aug 29 '18 at 19:09

2 Answers2

2

Addresses represent a location in memory where an object resides. It is distinct from the actual type it points to, and need not be the same size.

A pointer to a char wouldn't be much use if it could only hold 256 values. That would mean there are only 256 bytes worth of characters that could be addressed.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

A pointer to a type is not bound by the size restrictions of the type itself.

Simple proof:

if(sizeof(char *)==sizeof(char))
{
     printf("I\'ll never get anything done like this.\n");
}
else
{
     printf("Life is good!\n");
}
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Why did you escape the single quote? Also, you could prove that size of two pointers is the same regardless of their types. – Iharob Al Asimi Aug 29 '18 at 18:46
  • @IharobAlAsimi: 1. It's good practice 2. The C standard only guarantees that `char *` and `void *` have the same internal representation and size; other pointer types may not necessarily – Govind Parmar Aug 29 '18 at 18:48
  • @IharobAlAsimi good point except Govind is not comparing the size of different pointer types. – Weather Vane Aug 29 '18 at 18:48
  • @WeatherVane I know, but that could help the OP understanding ... I guess. – Iharob Al Asimi Aug 29 '18 at 18:49
  • @GovindParmar I am not 100% sure but ... If any pointer is convertible to `void *` and viceversa then I suspect they will all have the same size. Or else, the conversion would not be possible. In that case then casting `malloc()`'s return value would be mandatory, perhaps. To put an example of a very controvertial good practice not to cast `malloc()`'s return value. It's actually the most famous [tag:c] question on SO. – Iharob Al Asimi Aug 29 '18 at 18:51
  • @IharobAlAsimi C standard § 6.2.5.27: `A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements`. – Govind Parmar Aug 29 '18 at 18:54
  • @IharobAlAsimi [please see this](https://stackoverflow.com/questions/1241205/are-all-data-pointers-the-same-size-in-one-platform-for-all-data-types) and it is a good reason for the cast in `printf("p=%p\n", (void*)p);` – Weather Vane Aug 29 '18 at 18:56
  • @WeatherVane Very interesting, thanks. There was an example of a case where this is actually true. – Iharob Al Asimi Aug 29 '18 at 18:58