Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is this statement correct? Can any "TYPE" of pointer can point to any other type?
Because i believe so , still have doubts.
So why are then pointers declared for definite types? eg. int or char ones?
The one explanation i could get was because if an int type pointer was being pointed to char array, then when pointer will be incremented , the pointer will jump from 0 position to directly 2 position , skipping 1 position in between( because int size=2).
And maybe because pointer just holds the address of a value , not the value itself eg. the int or double one.

So at what i am wrong? Was that statement correct? As i read that at someone's answer to stackoverflow question?
Posted
Updated 31-Oct-13 4:51am
v2

Yes and no. All pointers are the same - they contain an address in memory. That is why it is legal in C and C++ to convert any pointer, regardless of type, to a void*

But when you work with pointers, their type does make a difference. Both when iterating over elements in an array (for this to work, the size of the elements must be known to the compiler, and the way we tell the compiler is through the type of the pointer), and when dereferencing a pointer to get to the data it points to. That is why casts between different types is dangerous.

<br />
int a = 1234; // a value on the stack<br />
int* b = &a;  // points to a on the stack<br />
char* c = (char*)b; // the same address,<br />
                    // but the contents <br />
                    // will be interpreted<br />
                    // differently<br />
char d = *c; // What does d contain?<br />
long* e = (long*)b; // again<br />
long f = *e; // What does f contain?<br />
             // (This reads more than was <br />
             // originally put on the stack<br />
             // so will lead to undefined<br />
             // behaviour.)<br />
 
Share this answer
 
Comments
Abhinav Gauniyal 31-Oct-13 11:18am    
Actually , i am at the same part only , The 'Yes and No'.
But what would happen if i dont want an arithmetic operation on that pointer? means just a int* b , no array. And if i try to point the same pointer to a different variable of other type just abandoning the first one? Will that be an error?
I think not , because the pointer is still holding the address , inspite of some other data type?
Orjan Westin 31-Oct-13 11:58am    
You are correct, it is not an error to give the pointer a different address to point to (although you probably need to make a cast to make the compiler accept the assignment of a pointer to a different type).

If the abandoned data was created on the heap (with new or malloc), and you don't have another pointer to it which you can use to release the memory, it will be lost (= a memory leak). But that isn't an error, as such, just unwanted behavior.
Abhinav Gauniyal 31-Oct-13 12:03pm    
So is it possible to point a char* pointer to a int type variable?
Orjan Westin 31-Oct-13 12:15pm    
Yes, like I did in the example, by using a cast to "lie" to the compiler. But you can't use the char pointer to get to the int variable, only one byte of it. And which byte you get depends on the CPU architecture.



Abhinav Gauniyal 31-Oct-13 12:26pm    
Thanks sir , Got it :)
All pointers hold a (possibly invalid) address. However the C++ programming language is strongly typed: the compiler detects pointer type mismatches.
And, yes, pointer arithmetic relies on pointed type size.

Please see this page: "Pointers" at cplusplus.com[^], you may learn also about the special rule of the void pointer.
 
Share this answer
 
v4
Comments
Abhinav Gauniyal 31-Oct-13 11:02am    
So can i say that any type of pointer is able to point to anything? And i have also listened about that due to this characteristic , we can convert them from one data type to another?

And just pointer arithmetic relies only or some other functioning too?

Sorry for asking too many questions , but i want to get a strong hold of this topic :)
C#
#include <stdio.h>
#include<string.h>

char f[10000];
char factorial[1010][10000];

void multiply(int k)
{
int cin,sum,i;
int len = strlen(f);
cin=0;
i=0;
while(i<len)
{
sum=cin+(f[i] - '0') * k;
f[i] = (sum % 10) + '0';
i++;
cin = sum/10;
}
while(cin>0)
{
f[i++] = (cin%10) + '0';
cin/=10;
}
f[i]='\0';
for(int j=0;j<i;j++)
factorial[k][j]=f[j];

factorial[k][i]='\0';
}
void fac()
{
int k;
strcpy(f,"1");
for(k=2;k<=1000;k++)
multiply(k);
}
void print(int n)
{
int i;
int len = strlen(factorial[n]);
printf("%d!\n",n);
for(i=len-1;i>=0;i--)
printf("%c",factorial[n][i]);
printf("\n");
}
int main()
{
int n;
factorial[0][0]='1';
factorial[1][0]='1';
fac();
while(scanf("%d",&n)==1){
print(n);
}
return 0;
}
 
Share this answer
 
Comments
Abhinav Gauniyal 31-Oct-13 12:01pm    
My brain is blown out :( , just want a hint probably !
Matt T Heffron 31-Oct-13 13:54pm    
This "solution" has nothing to do with the question asked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900