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.
7 replies to this topic
#1
Posted 09 December 2011 - 03:31 AM
|
|
|
#2
Posted 09 December 2011 - 05:39 AM
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.
means that ptr holds the address of an unnamed integer.
int *ptr;
means that ptr holds the address of an unnamed integer.
Programming is a journey, not a destination.
#3
Posted 10 December 2011 - 11:14 AM
@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
Posted 10 December 2011 - 11:42 AM
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
Hope this helped :D
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
Posted 10 December 2011 - 12:24 PM
@mebob
thank you for the reply,
int a;
i have never seen people saying "a points to int" why?
thank you for the reply,
int a;
i have never seen people saying "a points to int" why?
#6
Posted 10 December 2011 - 01:01 PM
It doesn't point to the variable type int; it points to a variable OF the type int.
Latinamne loqueris?
#7
Posted 10 December 2011 - 06:01 PM
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
Posted 10 December 2011 - 08:36 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









