home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1679
Discussions: 188,402, Posts: 2,243,609, Members: 232,632
Old April 16th, 2005, 09:59 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Apr 2005
Posts: 4
Question
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!!

pepote is offline   Reply With Quote
Old April 16th, 2005, 10:36 AM     #2 (permalink)
Ultimate Member
 
elmers's Avatar
 
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

elmers is offline   Reply With Quote
Old April 16th, 2005, 11:45 AM     #3 (permalink)
Junior Member
 
Join Date: Apr 2005
Posts: 4
Unhappy
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!

pepote is offline   Reply With Quote
Old April 16th, 2005, 01:07 PM     #4 (permalink)
Ultimate Member
 
elmers's Avatar
 
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.
elmers is offline   Reply With Quote
Old April 17th, 2005, 09:31 PM     #5 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
Send a message via AIM to Rootstonian
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.
Rootstonian is offline   Reply With Quote
Old April 17th, 2005, 10:15 PM     #6 (permalink)
Ultimate Member
 
elmers's Avatar
 
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.
elmers is offline   Reply With Quote
Old April 18th, 2005, 03:24 PM     #7 (permalink)
Junior Member
 
Join Date: Apr 2005
Posts: 4
Arrow
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!
pepote is offline   Reply With Quote
Old 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.
Peter M is offline   Reply With Quote
Old April 18th, 2005, 03:44 PM     #9 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,792
Send a message via AIM to Rootstonian
Post

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
Rootstonian is offline   Reply With Quote
Old April 18th, 2005, 07:24 PM     #10 (permalink)
Junior Member
 
Join Date: Apr 2005
Posts: 4
Question
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;
}
pepote is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Access code error [Neo770] Applications and Operating Systems 7 March 11th, 2005 12:42 AM
error code 12 dadie11 Technical Support 2 November 9th, 2004 05:53 PM
Please help me!! I can't understand this error code... giusepp Technical Support 10 July 15th, 2004 08:26 PM
Error code 10 mozzdog Technical Support 1 October 3rd, 2003 02:06 PM
What is this Error Code? nodnerb2 Technical Support 9 January 11th, 2003 07:07 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Unarmed man on his stomach shot by .. (6)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Futronix has water features? (0)
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:26 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28