Jump to content

C: Language: Pointers Fundamentals Doubt 02

- - - - -

  • Please log in to reply
7 replies to this topic

#1
gautham

gautham

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
I am learning the art of communicating in programming, Can any one clarify the following doubt:


int *ptr; <-- i came to know that this means "the variable 'ptr' is pointer to integer", how and why?

when we normally declare a variable: int k;<-- this means "k is a variable which is of integer type" but we don't say "k points integer", can we say that?


thank you.

#2
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
A pointer is a special kind of integer that holds the address of a variable. They're used for dynamic memory allocation, complex data structures, and passing by reference among other things.


int *ptr;


means that ptr holds the address of an unnamed integer.
Programming is a journey, not a destination.

#3
gautham

gautham

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts

@DarkLordofthePenguins


Thank you friend,


but i am asking for the "Terminology" we use, i have read that:

int *ptr;<-- here "*ptr is pointer to int" why? and how? it is pointing to integer? since it is a "data type"?


#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I'll try to answer:

Yes, it is pointing to an integer. Also, if you were to say 'char *ptr;' it would be pointing to a char. When you define
int *ptr;
you define a variable that points to, or references, a specific memory location, by holding that location's address. You can set this address to whatever you please:

int *ptr;

ptr = 100;

but your program would probably crashed as odds are it wouldn't be allowed to access that memory address. You can also do this:

int var = 100;

int *ptr;

ptr = &var;

What this does is it takes the address of the variable 'var' by using the & operator (which gets the memory address of a variable), then assigns this address to 'ptr'. Later on, you can access this memory address with the * operator, by using '*ptr'.

int var = 100;

int *ptr;

ptr = &var;


*ptr = 200;


printf("%d\n%d", var, *ptr);

Here, you set the data pointed to by 'ptr' to 200. In this case, since 'ptr' points to 'var', by changing '*ptr' you also change 'var'. If you compile/run that code (with of course the main function and includes) you will see that after declaring that the address pointed to by 'ptr' equals the address of 'var', changing '*ptr' will also change 'var'. The opposite would work too: changing 'var' also cause the output of '*ptr' to change.

Hope this helped :D
Latinamne loqueris?

#5
gautham

gautham

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
@mebob

thank you for the reply,


int a;


i have never seen people saying "a points to int" why?

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
It doesn't point to the variable type int; it points to a variable OF the type int.
Latinamne loqueris?

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
In C/C++ * operator has several meanings; it can be used as a multiplication operator, it can be used to create a pointer and can be used a dereferencing operator (when you want to get the pointer's value).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I forgot to mention that, sorry.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users