1

In int *x x is a pointer to integer.

If we initialize with a statement something like this: int *x = &p now x is pointing to the address of "p".
The value that it produces is: 6422036

Does that mean in int *x x is a pointer to the type int or it's a pointer to an integer value?
Is it conceptually something like int *x = &int as default?

When I compile this:

#include <stdio.h>

main ()
{
    int *x;
    printf("%d", x);
    return 0;
}

It gives the value 16.
What does it mean?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 3
    "*It gives the value 16.*". You have not set the variable value so it is an uninitialized garbage value. `int *x` declares a pointer to an integer value. But you need to set it to something before it is a valid value. It's not very clear what your exact doubt it. – kaylum Aug 02 '21 at 04:31
  • A pointer is basically some memory address. You should not generally be concerned with whether it's a positive or negative value. Think of an `int *` type like it is some memory address, and whatever is at the address is treated like it is an `int`. You should enable compiler warnings, and try to understand what the compiler is yelling to you about. – Cheatah Aug 02 '21 at 04:39
  • @kaylum I wrote this code just as a test. but my doubt is about how x points to, I'm not sure what it's pointing to. and if x is considered as a garbage value, how come it's giving me 16. –  Aug 02 '21 at 04:40
  • In your example, `x` is pointing to whatever address. You convert the pointer to an integer, which it isn't, and the value that is printed is probably garbage. What did you expect and why? – Cheatah Aug 02 '21 at 04:44
  • *"how come it's giving me 16"*. If you draw something out of a box that you don't own would you be surprised that it contains "X". No, because you didn't put anything into that box so you can't have any expectation of what comes out. This is a class of Undefined Behaviour in C - the results are unpredictable. [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) – kaylum Aug 02 '21 at 04:47
  • `x` is a pointer, in the code you wrote it has not a value assigned then `printf` prints garbage! A pointer is a value indicating a memory address and if you print its value (when a value is assigned) you obtain the value of such as address. The kind of data pointed by the memory address specified by the pointer `x`, as declared in your code, is an integer. – Sir Jo Black Aug 02 '21 at 04:48
  • `x` is *potentially* a pointer to an object of type `int`. It can hold the address of an actual object of type `int`, a null pointer value, or an invalid pointer value. In the code you posted, it is holding an invalid pointer value because it has not been initialized. Also the `printf` call is using the wrong specifier. It should be `printf("%p", (void *)x);`. (The cast to `(void *)` is because the `%p` specifier expects a `void *`, not an `int *`.) – Ian Abbott Aug 02 '21 at 09:48

3 Answers3

2

I think your question is based on a misunderstanding, so I am going to do some bean-counting on your phrasings. (Not contradicting ikegami by the way, just trying to provide a different approach or viewpoint.)

In int *x x is a pointer to integer.

Yes.

if we initialize the statement with something like this: int *x = &p now x is pointing to the address of p.

Not exactly, now x contains the address of p (which hopefully is an int). It is considered to point to p.

The value that it produces is: 6422036

If you say so, but that is not reproducable and you should probably never think or know about that value. Any other value at this point would means the same - or nothing.

Does that mean in int *x x is a pointer to the type int or it's a pointer to an integer value?

Yes.
To be precise, for a C programmer those are one and the same thing.
Whatever it is pointing to is an integer value, i.e. it is of int type. (Skipping the differences of unsigned int, long int, short etc., I am convinced you are not asking about that.)
If you see a difference between those two, then your understanding is not C (I suspect it comes from a different language, see introspection as discussed by ikegami.)

is it conceptually something like int *x = &int as default?

No. There is no such thing as &int in C. It is more like

int *x; /* Don't you dare dereference this before you assigned a decent address.
           and be sure to assign an address of something you are actually allowed
           to at least read - or expect little demons flying out of your nose. */

Note that "demons flying out of your nose" is practically a technical term used among programmers to describe that absolutely anything can happen, usually undesired things. Most undesired possible result is that it passes all tests and then fails catastrophically in the most inconvenient situation.
Actually it is called undefined behaviour.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

C doesn't have reflection. There's no int type in memory, just values of that type. As such, x isn't a pointer to the type int. It is indeed a pointer to an integer value.

Given int *x, we can say:

  • *x is an int
  • x is an int*, which is to a pointer to an int.

That assumes x has been properly initialized and isn't NULL. This isn't the case in your program. Reading x before giving it a value is a bug.

Also, providing a pointer to %d is a bug. To display a pointer, you need

printf("%p\n", (void*)x);   // One of the weird times when a `void*` cast is needed.

Again, this is only legit after you initialize x.


A valid program:

#include <stdio.h>

int main(void)
{
    int i = 123;
    int *p = &i;
    printf("%p\n", (void*)p);
    printf("%p\n", (void*)&i);
    printf("%p\n", (void*)&p);
    printf("%d\n", i);
    printf("%d\n", *p);
    return 0;
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

Does that mean in int *x x is a pointer to the type int or it's a pointer to an integer value?

x is an object stores values of type int *; that is, it stores the address of an int object.

It gives the value 16. What does it mean?

It means you've invoked undefined behavior - you're using the wrong format specifier for the type.

The proper way to print a pointer value is

printf( "%p\n", (void *) x );

In the declaration

int *x;

the initial value of x is indeterminate - it could be anything, from 0x00000000 to 0x00000010 (16) to 0xDEADBEEF to anything else.

There's nothing magic about pointer variables - they store values of a specific type, like an int variable stores integer values and double variable stores floating-point values.

Pointer declaration syntax and operations on pointers are a little non-intuitive and hard to grasp at first, but pointer values themselves are relatively simple things to understand; they're just addresses1 of objects (or functions) in memory.

There's no single pointer type - an int * is a distinct type from a double *, which is a distinct type from a char *, etc. Different pointer types may have different sizes or representations, but on platforms like x86 they all have the same representation.

In a declaration, the presence of a unary * in the declarator means the variable has pointer type:

T *p;       // p is a pointer to T
T *ap[N];   // ap is an array of pointers to T
T (*pa)[N]; // pa is a pointer to an array of T
T *fp();    // fp is a function returning a value of type pointer to T
T (*pf)();  // pf is a pointer to a function returning a value of type T

T **pp;     // pp is a pointer to pointer to T - it stores the address of
            // an object of type T *

In an expression, the presence of the unary * operator means we want to dereference the pointer and obtain the value of the thing it points to:

int x = 10;
int *p = &x;  // save the address of x in p

printf( "%d\n", *p ); // print the value stored in x by dereferencing p

  1. More properly, they're abstractions of addresses. Whether those addresses are physical or virtual depends on the environment you're operating in.
John Bode
  • 119,563
  • 19
  • 122
  • 198