By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

About pointer

P: 1
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
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 181
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
100+
P: 1,806
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
Expert Mod 5K+
P: 9,197
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

P: 1
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
Expert Mod 100+
P: 931
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.