473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2240
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

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
9
by: wongjoekmeu | last post by:
Hello All, I am learning C++ at the moment. Going through the book of SAM of learning C++ in 21 days I have learned about pointers that it is good custome to always initialise them and to use...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.