April 16th, 2005, 09:59 AM
|
#1 (permalink)
| | Junior Member
Join Date: Apr 2005
Posts: 4
| Error in C pointers code
Hi friends!
When I'm debugging my C pointers sample code, TurboC IDE returns the next strange error message when I analyze &p2 in the watch window: &p2: Must take address of a memory location
The code I'm talking about is the following:
//------------- C pointers sample code ------------------------------
#include<stdio.h>
#include<conio.h>
void main(void)
{
int a, b, c, *p1, *p2;
int *p;
p1 = &a; // Step 1: "a" var's address is assigned to p1
*p1 = 1; // Step 2: p1 (a) is equal to 1. Equivalent to a = 1;
p2 = &b; // Step 3: "b" var's address is assigned to p2
*p2 = 2; // Step 4: p2 (b) is equal to 2. Equivalent to b = 2;
p1 = p2; // Step 5: Value of p1 = value of p2
*p1 = 0; // Step 6: b = 0
p2 = &c; // Step 7: "c" var's address is assigned to p2
*p2 = 3; // Step 8: c = 3
clrscr();
printf("%d %d %d\n", a, b, c); // Step 9: What's printed? ---> 1 0 3
p = &p1; // Step 10: p contains the address of p1
*p = p2; // Step 11: p1= p2
*p1 = 1; // Step 12: c = 1
printf("%d %d %d\n", a, b, c); // Step 13: What's printed? ---> 1 0 1
getch();
}
//-------------------------------- end ----------------------------------------------
I have no idea about the reason why the watch window doesn't show me the value and the memory address &p2 (and others pointers, as you can see if you debug the code).
Can you help me?
Thank you in advance!!  |
| |
April 16th, 2005, 10:36 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: Sep 2003 Location: Philadelphia
Posts: 1,462
|
Welcome to TIMO!
Does this code comile at all? Which line do you get the error in? Does the error come up only when you watch the variable?
Ive tried compiling it, and it reports an error in these two lines: Code: *p = p2; // Step 11: p1= p2
*p1 = 1; // Step 12: c = 1 |
| |
April 16th, 2005, 11:45 AM
|
#3 (permalink)
| | Junior Member
Join Date: Apr 2005
Posts: 4
| The code works, but the message is still there!
Hi, elmers!
Thank you, I discovered this web some days ago and I'm amazed! I love these forums! I'm sure we'll meet again in some conversation!
About the code:
I've compiled it again, but the IDE (TurboC++ 3.0) only returns two warnings at steps 10 and 11: "Suspicious pointer conversion" (step 10) and "Nonportable pointer conversion" (step 11). However, it works!
Do you know the meaning of the error message (Must take address of a memory location)?
I've executed the program and it works correctly, at least from the point of view of the concepts of pointers.
Thank you for your help! |
| |
April 16th, 2005, 01:07 PM
|
#4 (permalink)
| | Ultimate Member
Join Date: Sep 2003 Location: Philadelphia
Posts: 1,462
|
Warnings aren't a problem. Code: p = &p1; // Step 10: p contains the address of p1 Here you set p to the address of a pointer, its declared as a pointer to int thus the warning. Code: *p = p2; // Step 11: p1= p2 Here your putting p2 which is an address into whatever p is pointing. Since p is a pointer to int it gives you an warning message.
What warnings tell you is that altho the code comiles your doing something unusual. This is normal since youre doing all sorts of things with those pointers. |
| |
April 17th, 2005, 09:31 PM
|
#5 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
p = &p1 should be a valid statement
*p = p2 should not be a valid statement
Here is what I was taught when learning pointers. When you see *whatever, say to yourself "the data at". Technically called dereferencing a pointer.
In the second statment above, your're saying that "the data at p should equal a pointer" Pointer are addresses in memory and your trying to put a pointer address into an int location. |
| |
April 17th, 2005, 10:15 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Sep 2003 Location: Philadelphia
Posts: 1,462
| Quote: |
In the second statment above, your're saying that "the data at p should equal a pointer" Pointer are addresses in memory and your trying to put a pointer address into an int location.
| Pointers are addresses in memory, however on 32 bit machines they are represented by 32 bits. Because C has weak typing it castes the 32bit value into a 16 bit one in some way. While its almost always semanticly incorrect to do so it is syntacticly correct.
Last edited by elmers : April 17th, 2005 at 10:18 PM.
|
| |
April 18th, 2005, 03:24 PM
|
#7 (permalink)
| | Junior Member
Join Date: Apr 2005
Posts: 4
| Clearer,but what's the meaning of the "must take address of memory location" message?
Thank you, friends.
I've realized that the pointers are really "pointers to pointers". I think that the scheme is the next: p --points to--> p1 --points to--> p2 --points to--> c
stored data: p: ??? NULL? p1: 3 p2: 3 c: 3
...or something like that
I've tried it out writting **p instead of *p and it works, too. Is it "more formal"?
Well, but I don't know the reason of the "must take address of a memory location" message yet. Can you help me (again)?
Thank you! See you soon! |
| |
April 18th, 2005, 03:41 PM
|
#8 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Augsburg, Germany
Posts: 5,586
|
It's not showing an address because your C compiler probably optimized that into using CPU registers, and not actually storing that pointer in RAM. |
| |
April 18th, 2005, 03:44 PM
|
#9 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,792
|
Ran this through Visual C++...
here are the errors
error C2440: '=' : cannot convert from 'int **__w64 ' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C2440: '=' : cannot convert from 'int *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
here are the 2 lines corrected
p = p1; // Step 10: p contains the address of p1
(int *)p = p2; // Step 11: p1= p2 |
| |
April 18th, 2005, 07:24 PM
|
#10 (permalink)
| | Junior Member
Join Date: Apr 2005
Posts: 4
| The error message... at last!
I`ve just found the explanation about the error message I'm talking about, but it's in German!
Can any German friend try to translate it to English (or better, to Spanish)  ??Thank you vey much!!  ...DANKE!! Must take address of a memory location SUMMARY
Es wurde versucht auf Speicher zuzugreifen,der nicht zu einem Objekt gehört.Der Zugriff wäre nicht sicher,daher wird er als Fehler abgelehnt.
FULL VERSION
Dieser Fehler kann unterschiedliche Ursachen haben,wurde bis jetzt nur durch folgende Vorgehensweise erreicht:
Man versucht die Adresse des (temporären) Ergebnisses einer Operation zu bestimmen.Dies ist nicht erlaubt,weil die Variable nach verlassen des Ausdrucks nicht mehr besteht,und der Zeiger auf einen nicht definierten Speicherbereich weisen würde.
SAMPLE CODE
extern void tu_was(unsigned char* anf,unsigned char* ende);
int main(void)
{
char feld[10];
tu_was(&feld[0],&feld[9]++);//das Zweite Argument wird als &(feld[9]++) ausgewertet
return 0;
} |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |