Why create a pointer in C when you can just point directly to the variable as a pointer? -
i'm learning c , came across example kind of seems creates unecessary step, again i'm new this.
he created variable, , dedicated pointer point variable. understanding simple put * in front of variable , serve pointer it...so why use line of code create pointer? i'm talking why created pointer "*p" refer "x" vs saying *x point it. below sample code:
#include <stdio.h> int main() { int x; /* normal integer*/ int *p; /* pointer integer ("*p" integer, p must pointer integer) */ p = &x; /* read it, "assign address of x p" */ scanf( "%d", &x ); /* put value in x, use p here */ printf( "%d\n", *p ); /* note use of * value */ getchar(); }
that's right don't need pointer object.
you program equivalent in behavior to:
int x; scanf("%d", &x); printf("%d\n", x); getchar();
the use of pointer in example didactic reasons: explain how declare , use pointers.
Comments
Post a Comment