![]() |
| | #16 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Quote:
Given this: Code: int n = 1; int *p = &n; Regardless, you shouldn't care about the exact value that p holds -- you know that it "points" to n, and that's good enough. *p, on the other hand, tells the compiler to look at p and find the address it holds; and then look at the memory at that address. In effect, it's the same thing as looking at n directly. And thus, *p == 1. Code: #include <stdio.h>
int main() {
int n = 1;
int *p = &n;
printf("n: %d\n", n);
printf("p: %p\n", p);
printf("*p: %d\n", *p);
return 0;
}
Code: n: 1 p: 0x7fff6017f514 *p: 1
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort | |
| dwks is offline | |
| | #17 |
| Registered User Join Date: Oct 2007
Posts: 242
| Doesn't (int *)&n (assuming n is an int) supposed to return a pointer to a pointer (**)? I mean, you are casting the address of n into an int pointer - or what exactly happens if I cast something into a pointer? Last edited by eXeCuTeR; 07-02-2008 at 04:24 PM. |
| eXeCuTeR is offline | |
| | #18 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| Casting doesn't do anything most of the time (except in some circumstances). All it does is tell the compiler you know that you're treating an object of one type as an object of another one, when in fact you know such treatment is considered breaking some safety rules, but you want to do it anyway.
__________________ |
| MacGyver is offline | |
| | #19 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
Consequently, float* is not int*, therefore I use a cast to turn that float* into int*. Casting turns the type on the right side into the type on the left side. It does not take the address. Look at this: Code: #include <stdio.h>
int main()
{
float f = 1.0f;
int* n;
memcpy(&n, &f, sizeof(int*));
printf("%p\n", n);
n = (int*)&f;
printf("%p\n", n);
printf("%i\n", *n);
printf("%f\n", *(float*)n);
return 0;
}
3F800000 0012FF60 1065353216 1.000000
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #20 |
| Registered User Join Date: Oct 2007
Posts: 242
| Why are you passing &n and not n? &n is a pointer to a pointer, isn't it? --- So what's up with (int *)&f? |
| eXeCuTeR is offline | |
| | #21 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| If you're referring to... memcpy(&n, &f, sizeof(int*)); ...then what you don't seem to understand is that a pointer is a variable, and by taking its address, I can put a value into it. The memcpy is the same as... int* n = (int*)f; ...although the compiler will not allow it. And give yourself a break and go back to the books. We've already explained the last one.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #22 | |
| Registered User Join Date: Oct 2007
Posts: 242
| Quote:
I know these stuff but my point is the memcpy gets a pointer in it's first parameter while you are passing a pointer to a pointer! | |
| eXeCuTeR is offline | |
| | #23 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| I'm telling you, I want to copy the data into the variable itself, and not what it points to.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| struct |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I'm suppose to use this library but I'm confused on what exactly the data structure | mr_coffee | C Programming | 1 | 12-03-2008 03:10 AM |
| Global Variables | Taka | C Programming | 34 | 11-02-2007 03:25 AM |
| Struct with a ptr to a dynamically allocated array of other structures :( | michael- | C Programming | 10 | 05-18-2006 11:23 PM |
| What's wrong with my search program? | sherwi | C Programming | 5 | 04-28-2006 09:57 AM |
| Tutorial review | Prelude | A Brief History of Cprogramming.com | 11 | 03-22-2004 09:40 PM |