What is the result of this code ?

Can somebody help me about the result of this part of code?

if we have a pointer ptr as integer (int * ptr).
What will be the result of the following command ?
printf("%d", ptr);

I made the following part of code:
#include <stdio.h>
#include <stdlib.h>

int main(){
int *ptr;

printf("%d", ptr);
return 0;
}

then the result is:
4199264
ptr is not pointing to a valid location
closed account (S6k9GNh0)
That's the decimal representation of the address ptr is located in. It's random everytime you assign a variable (not just a pointer). Try doing this:

1
2
3
4
5
6
7
#include <cstdio>

int main()
{
   int aVar;
   printf("%d", &aVar);
}


This will print something similar to what you have there.
Because the address is random, you can't make a function that access a specific location in memory unless it's during runtime. The above is an example of accessing the adress of a variable at runtime.

This is one of the few keys of pointers. Hope ya learned something!
Topic archived. No new replies allowed.