# Understanding pointers in C

• (2 Pages)
• 1
• 2

## 17 Replies - 1825 Views - Last Post: 25 October 2008 - 05:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=68733&amp;s=4b1a5ed7c697a22e9d4712299d6c96e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Understanding pointers in C

Posted 24 October 2008 - 03:11 PM

Wrong... well right.. but no... I mean you are basically correct, in that pointers are generally stored in a space that occupies 32 bits. Though actually that is not correct either but I will get to that in a bit.

What I meant by "size" was the increment size: when you say ptr++ what is the increment in the address. So I mean "size" as in the "size of the type pointed to" (as explained in your example). (sorry if my choice of the word "size" was confusing).

Now as for the space used to allocate a pointer. It is generally a superlatively bad idea to assume a size for a pointer. A great deal of software has been broken by doing this.

Typically it is assumed that a pointer is the word-size of the given platform. So for example on a 16 bit computer a pointer is 16 bits, ona 32 bit computer, 32bits, and on a 64 bit computer it would be 64 bits! BUT this is not TRUE!!!

On 16 bit computers we had near and far pointers, near pointers were 16bit, and far pointers were 32... On 32 bit computers we finally reach a basic consensus -- 32bit pointers could work for both (though when compiling for a 16bit mode you still have segmented memory and still have 16bit near pointers). So for most programming over the last few years pointers were 32 bits....

But now we have 64 bit processors... and once again things have been complicated. 64bit programs may have 32 or 64 bit pointers depending upon the memory model chosen at compile time.

Most compilers have options for compiling to various memory models (often just for "backward compatibility"). Most people using 32 bit compilers have long since forgotten about memory models -- but those of us who lived though 8 and 16 bit computers and laughing at the confusing caused by the 64bit processors.

Note that C/C++ DOES NOT GUARANTEE the size of pointers (or chars, ints, longs, etc. -- C99 does have int8_t, int16_t etc). So if you ever are trying to rely upon the size of a pointer you must use sizeof() to determine its size. Or ensure you check with the compiler documentation to select the proper memory model.
Was This Post Helpful? 0

### #17 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Understanding pointers in C

Posted 24 October 2008 - 06:52 PM

Quote

So if you ever are trying to rely upon the size of a pointer you must use sizeof() to determine its size. Or ensure you check with the compiler documentation to select the proper memory model.

It is always good to use sizeof ... but the sizeof any pointer will reflect exactly the number of bits for that addressing mode.

Thus, the following IS TRUE in a 32 bit addressing environment:

"In a 32 bit environment, pointers always hold a 32 bit address ...

so there ... they will ALWAYS have size 4, i.e. 4 bytes = 32 bit." (Note that this is true always in a 32 bit environment only.)

For some free help with beginning computer programming, you might also like to see ...

http://developers-he...index.php/topic,46.0.html

Shalom shalom and Shabat Shalom,

David

This post has been edited by David W: 24 October 2008 - 06:55 PM

Was This Post Helpful? 0

### #18 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Understanding pointers in C

Posted 25 October 2008 - 05:35 AM

Some more code to play with, if you like ...

Note my compiler complained when I tried to do this:

int *p = &a;

But it liked ok ...

int *p = a;

because 'a' already holds the address to the array 'a' (of integers)

```#include <stdio.h>

int main()
{
int a[] = {1, 2, 3};
printf
(
"address of array 'a' in hex = %p\n"
"address of array 'a' in dec = %d\n"
"value at that address interpreted as an 'int' = %d\n"
"value at that address interpreted as an 'int' = %d\n\n",
a, a, a[0], *a
);

/*
recall in C/C++, 'a' above holds the address of the array of int's

so next we declair that 'p' is a pointer to
(i.e. holds the address of) an int
*/

int *p = a; /* now we have 2 (pointer) variables that hold the same address */
printf
(
"address held in 'a' in hex = %p\n"
"address held in 'p' in hex = %p\n\n",
a, p
);

/* so to increase, by 5, the (int) value at an address to an int ... */
*p = *p +5;
printf
(
"value at an address interpreted as an 'int' = %d\n"
"value at an address interpreted as an 'int' = %d\n\n",
a[0], *a
);

/* But ... notice what happens here ... */
printf
(
"value at address %p interpreted as an 'int' +1 = %d\n"
"value at address %p interpreted as an 'int' = %d\n",
a, *a+1, a+1, *(a+1)
);

getchar();
return 0;

}
```

Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }