470,625 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,625 developers. It's quick & easy.

About pointer

Hello,
I am Sandeep Patil. I have one question about pointer in c and also c++.
What is function of pointer? it is address operator. We use pointer to store address of variable to refer that variable. Then why we use char pointer, int pointer as a different pointers. Why should we not use one type of pointer to refer all types of variable?
Jun 21 '07 #1
5 1910
svlsr2000
181 Expert 100+
Hello,
I am Sandeep Patil. I have one question about pointer in c and also c++.
What is function of pointer? it is address operator. We use pointer to store address of variable to refer that variable. Then why we use char pointer, int pointer as a different pointers. Why should we not use one type of pointer to refer all types of variable?
Pointer types ie character or integer is to indicate what they are pointing to.
for example u can store a character in space allocated to integer

int * p = new int;
*p = (int)'c';

But according to compiler still *p contains int and not character.
I think this answered your question
Jun 21 '07 #2
DeMan
1,806 1GB
The pointer is still the same irrespective.
The difference between a character pointer and an integer pointer, is that a character pointer treats the VALUE where it is pointing as a character, while an integer pointer treats the VALUE as an integer.....
:
Expand|Select|Wrap|Line Numbers
  1. int *int_pointer;
  2. char *char_pointer;
  3.  
  4. *int_pointer = 0xc0ff;  /* Set the value at the integer pointer to be larger than a character */
  5. char_pointer = (char*)int_pointer;
  6.  
What is the difference (in this context) between *int_pointer and *char_pointer ?
Jun 21 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Hello,
I am Sandeep Patil. I have one question about pointer in c and also c++.
What is function of pointer? it is address operator. We use pointer to store address of variable to refer that variable. Then why we use char pointer, int pointer as a different pointers. Why should we not use one type of pointer to refer all types of variable?
I would like to talk about why you can't have one kind of pointer for all variables. The answer has to do with address calculations. If you have pointer, ptr:
Expand|Select|Wrap|Line Numbers
  1. int* ptr;
  2. int  array[5];
  3. ptr = array;
  4.  
Here the address of element 0 (an int) is assigned to the int pointer ptr.

ptr = ptr + 2 tells the compiler to calculate an address that adds 2 times the sizeof an int to the address in ptr.

1000 array[0]
1004 array[1]
1008 array[2]
1012 array[3]
1016 array[4]

So the new address is ptr = 1000 + 2 *4

The sizeof(int) is 4.

That is, ptr now contains 1008 which is the address of array[2].

The compiler knows to use sizeof(int) because the pointer is a pointer to an int. If the compiler were to use any other value, like sizeof(char) or sizeof(double), then the calculated address for the array element will be wrong.

That is why the compiler is so picky about how you use your pointers.
Jun 22 '07 #4
Pointer types ie character or integer is to indicate what they are pointing to.
for example u can store a character in space allocated to integer

int * p = new int;
*p = (int)'c';

But according to compiler still *p contains int and not character.
I think this answered your question
can function return more than one value.
Jul 11 '07 #5
Meetee
931 Expert Mod 512MB
can function return more than one value.
You can search for your answer here
http://www.thescripts.com/forum/thread573080.html

You will have to use pointers for that

Regards
Jul 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by lawrence | last post: by
11 posts views Thread by Dmitry D | last post: by
9 posts views Thread by wongjoekmeu | last post: by
7 posts views Thread by sunglo | last post: by
7 posts views Thread by Yuri_Юрий | last post: by
68 posts views Thread by James Dow Allen | last post: by
4 posts views Thread by Deep | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.