1

When writing int * x, is there an implicit int for the type of the pointer, so that implicitly it is int * int x?

Randomblue
  • 112,777
  • 145
  • 353
  • 547

6 Answers6

3

A pointer is a type. It's not an integer, it's not a float, it's not a double, it's not a char, it's not a long. What it holds depends on the architecture of the machine for which you have compiled your code.

When you do arithmetics on pointers (+=, -=) the result is not the same as when you do it with integers.

You'll also notice that char * and int * are distinct types. You can probably cast between both, but when you increment them, unless sizeof int == 1 you get a different result.

What you probably want to know is what size the pointer occupies. Try then the following:

printf("%u %u %u %u\n", sizeof(char), sizeof(int), sizeof(int *), sizeof(long));

Note that different types of pointers can have different sizes.

Community
  • 1
  • 1
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Could you do `sizeof(*)`, or something similar, instead of `sizeof(int *)`? [Because `int` in `sizeof(int *)` is redundant/useless.] – Randomblue Mar 16 '12 at 17:15
  • @Randomblue: no. You can `typedef int *int_ptr` and use `sizeof(int_ptr)`. – Benoit Mar 16 '12 at 17:16
  • 1
    @Randomblue: the int is *not* redundant or useless; pointers to int may well be different sizes from pointers to other types. There is no single "pointer" type; there are multiple "pointer to T" types. – John Bode Mar 16 '12 at 17:21
  • @Brian Roach: [Pointers can have different sizes](http://stackoverflow.com/questions/3941793/what-is-guaranteed-about-the-size-of-a-function-pointer) – Benoit Mar 16 '12 at 17:26
  • I hate "can" vs. "do" arguments, so I'll retract that statement since I can't edit it :) – Brian Roach Mar 16 '12 at 17:36
2

To expand on my ealier comment...

There is no single pointer data type in C. Instead, you have multiple "pointer to T" datatypes - an int * is a distinct data type from a char *, which is a distinct data type from a double *, which is a distinct data type from a void (*)(...), which is a distinct data type from a long (*)[N], etc.

Even if all pointer types have the same size and representation (which is the case on most platforms, but not guaranteed), the semantics will differ. Assume the declarations

int  *ip =  (int *) 0x00004000;
char *cp = (char *) 0x00004000;

Both ip and cp start out with the same value (pointing to address 0x00004000, which is probably not valid). If I execute the statements

ip++; // or ip = ip + 1;
cp++; // or cp = cp + 1;

the resulting values of ip and cp will be different; ip will contain the address of the next integer (either 0x00004002 for a 16-bit integer or 0x00004004 for a 32-bit integer), but cp will contain the address of the next character (0x00004001). Pointer arithmetic is dependent on the base type of the pointer, so operations on integer pointers will yield different results from operations on pointers to other types.

It is possible for different pointer types to have different sizes. Think of a word-addressed architecture, where the basic unit of memory could be 16 bits wide, or 24, or 32, or 36. Any pointer to a type that occupied a full word would be N bits wide, but char * types would require some extra bits to point to specific characters within the word (unless you decided char values should also occupy the full word).

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

No. int * x declares x as a variable of type "pointer to objects of type int".

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • 6
    Why am I suddenly having flashbacks to "Who's on first?". – Brian Roach Mar 16 '12 at 17:08
  • @Randomblue: A pointer is not an `int`. Pointer arithmetics differ from `int` arithmetics (`p + 1` ≠ `i + 1`), etc. Pointers are types by themselves. If you wonder how many bytes they are stored on, then use `sizeof x`. Maybe you get the same as `sizeof int` but that's not guaranteed. – Benoit Mar 16 '12 at 17:10
  • 1
    Thanks Brian, another 8 minutes lost to laughing at You Tube clips. – Duck Mar 16 '12 at 17:32
  • @Duck - amazing how well that sketch holds up, isn't it? – Brian Roach Mar 16 '12 at 17:33
1

No. Pointers are not necessarily the same size as an int. This is particularly relevant on some architectures and if you also take into account things like function pointers.

hugomg
  • 68,213
  • 24
  • 160
  • 246
1

There is no implicit anything when you write int *x. x is a pointer, but it doesn't point to anything. The type of x is int *.

int * int x isn't legal syntax at all.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

Something which tends to be confusing when learning C is the * character. It actually has two entirely different uses:

  1. To create a Pointer Variable
  2. Used as an Indirection Operator

You can get a Pointer Address (memory address) of a variable, and assign it to a Pointer Variable, by using the Address-of & operator.

// Using * to create a Pointer Variable (pointer to an int):
int * intPtr;

// Using the & Address-of Operator to get the Pointer Address (memory address) of a variable, and assign it to a Pointer Variable:
int x = 10;
intPtr = &x;

// Printing the value of `x`, which is 10:
printf("%d\n", x);

// Using the Indirection Operator *, on a Pointer Variable, to perform indirection:
*intPtr = 20;

// Above, we modified the value of `x` using indirection via `intPtr`. So now `x` is equal to 20, and not 10:
printf("%d\n", x);

printf() output:

10
20

As others have mentioned, int * int x; is not valid in C.

Dave
  • 12,408
  • 12
  • 64
  • 67