473,396 Members | 1,683 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,396 software developers and data experts.

Problem in pointer size!!

2
In my assigment,I have a question:

"Between a long pointer and a char pointer,which one consumes more momery.Explain."

But i have read that all pointer have same size as of 2 bytes.So what can be the answer of this question??
Aug 29 '06 #1
6 4554
Banfa
9,065 Expert Mod 8TB
All pointers are platform dependent, the is no constraint in C for pointers to different types to be the same size but similarly the is no constraint that they be different sizes.

If (as stated) on your system all pointers are 2 bytes in size then that is the answer to the question, "which one consumes more memory?". neither they both consume the same amount of memory.

It is a trick question, your teacher is testing to see if you have understood that pointers are a different type to the things they point at which I think you have.
Aug 29 '06 #2
hi banta
i believe pointer's size depends upon their type
like for following code

#include<stdio.h>

main(){

char *ptr;
long *p;
printf("%d\t%d\n",sizeof(*ptr),sizeof(*p));

exit (2);
}

the output is
1 4
Aug 29 '06 #3
Ratan
2
Hello ssehgal2010 ,
Your code helped me:

#include<stdio.h>

void main(){

char *ptr;
long *p;
printf("%d\t%d\n",sizeof(*ptr),sizeof(*p));
printf("%d\t%d\n",sizeof(char *),sizeof(long *));

}
Output comes out to be:
1 4
2 2

Can i know the reasons why this has happen?
Aug 29 '06 #4
What's the trouble?

int n;
char c;

int * pn = &n;
char * pc = &c;

n - integer value in memory that contain 32b - 4bytes
c - integer/symbol value in memory that contain 8b - 1bytes

To point to any value you must know address of its first byte.
When you addressing byte you shoudn't worry about which type of value placed there. Therefore you can use equal variable to hold address. (on my IA-32 its allocate 4bytes like an integer value)

That's why pn and pc occupy the same amount of memory.
Aug 29 '06 #5
Banfa
9,065 Expert Mod 8TB
char *ptr;
long *p;
printf("%d\t%d\n",sizeof(*ptr),sizeof(*p));
This is not printing the size of the pointers but the size of the types they are pointing to.

printf("%d\t%d\n",sizeof(char *),sizeof(long *));
this is printing the size of the pointers

When you addressing byte you shoudn't worry about which type of value placed there.
This is not actually true because C does not guarantee that pointers to different types have the same size or bit pattern. However on many (most?) systems today pointers to different types do have the same size and bit pattern. Sometimes programmers rely on this which in theory makes their code non-portable, however in practice this is not often a problem.

However just because it is not a problem doesn't mean you can ignore it, you should know it in case you happen to work on a system where pointers to different types are of non-equal size or bit pattern.
Aug 29 '06 #6
Hey.. U are printing sizeof (*ptr) which is the variable pointed by the pointer
try to print sizeof(ptr) then you will get correct answer.

main()
{
char *cptr = "Abc";
int *iptr = 10;
printf(" %d\t%d\n",sizeof(cptr),sizeof(iptr));
}

out put:

2 2
Mar 1 '08 #7

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

Similar topics

9
by: M O J O | last post by:
Hi, I have my windows-display-font-size set to "Large fonts" and I've created a new project with two forms. Both of them are 600 width and 400 height. One have autoscale=true and the other is...
6
by: Cheng Mo | last post by:
I tried in g++ sizeof(int*) siezof(long*) sizeof(Foo*)//Foo is a self-defined class They all has same size. Do all pointers have same size in a given platform with given compiler? Thanks &...
3
by: Micky G | last post by:
Hey Guys: I know there is a pragma (compiler directive) for pointer_size for gcc on unix; does anyone know if there one on Windows (Visual Studio) too? Thanks! MickyG
7
by: seema | last post by:
Hi all, I am new to C programming. I have doubt and I want to clarify here is the program , #include <stdio.h> struct windows { int i; char me;
4
by: kumar | last post by:
how can i identefy the pointer size means is taking 4 byte or 2 bytes , on what basis we can decide either on processor or register
1
by: sina jafarzadeh | last post by:
hi i want to write a program that give 2 binary number with until 32 digit and return the multiple of them. i consider a long for put multiple of them but maybe(with law probality) the multiple of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.