4

I am learning C programming language these days. I have a question about pointer.

The textbook said pointer stores memory address, and using printf("%p",pointer) we can show where this pointer points in our memory.

But every pointer alse has a type, like int *pointer, long *p and so on. int *pointer means "p is a pointer to int".

My question if we write

int *p,i;
p=&i;
*p=99;

if the pointer only contains the address information, how could the programme know how many digits should be used for storing integer 99? Because an integer could be 16 bits int or 32 bits long.

So I was wondering if an int pointer in memory not only stores address information, but also stores the type information?

user15964
  • 2,507
  • 2
  • 31
  • 57

4 Answers4

5

Because an integer could be 16 bits int or 32 bits long.

An integer could, but an int could not. Regardless of how large that is exactly in your environment, its size is set in stone (within that environment) and doesn't change at run time. An int * only points to an int, not to a long. Note that if there were any such problems, they would affect int x; equally.

So a pointer really only stores the memory address. Information about the size of the pointee is in the type (just as a non-pointer variable's type tells the compiler how large that variable is).

4

Make sure you don't confuse what the hardware does with what the compiler does. A pointer is an address to a memory location as far as the hardware is concerned. What that memory location contains, and how long that data is, does not matter. A pointer does not store anything either. It points to a location and that's all. In assembly, this would be similar to using a register that points to a memory location.

The compiler is what tracks the type of data contained at that pointed to location. It is the compiler's job to save you from making type errors. This is where some people complain that you can shoot yourself in the foot with C. It's possible to have a pointer point to a data location where that data can be most anything and any length.

Rob
  • 14,746
  • 28
  • 47
  • 65
  • _It points to a location and that's all._ Not always, it seems: https://stackoverflow.com/a/15423304/1949418 – jinawee Aug 31 '18 at 12:29
  • @jinawee Yes...**always**. Read your link carefully. Every example mentioned is about a memory address of some sort, directly or indirectly. – Rob Aug 31 '18 at 12:33
  • If I understand correctly they can store permissions too, so it wouldn't just store an address. – jinawee Aug 31 '18 at 13:48
  • @jinawee No. Pointers don't store anything. They are addresses of memory locations. Nothing more. Nothing less. They point to memory. That's why they're called "pointers". – Rob Aug 31 '18 at 14:09
0

So I was wondering if an int pointer in memory not only stores address information, but also stores the type information?

No, a pointer is a memory address (or in some cases, a value analogous to a memory address). A pointer doesn't contain the data -- it points to the data. In your example, the data is stored in another location (given the name i), and p contains the address of i.

how could the programme know how many digits should be used for storing integer 99?

All the bits (binary digits) of the value type are used to store the stored value. Any given type (like int) has a fixed size. int can have different sizes depending on the system being used, but the size is always determined when the code is compiled. That is, the type int could be 16 bits, 32 bits, 64 bits, or some other size, but the compiler will always use a single size for compiling an entire program.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

The type information is retained in the source code code and is used by the compiler to perform type checking and to generate appropriate code, the type is implicit in the generated code rather than explicitly stored as data.

Clifford
  • 88,407
  • 13
  • 85
  • 165