Go to Post make sure you are having fun and developing yourself, not just learning - gixxy [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Spotlight this post!  
Unread 03-01-2010, 01:09
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Kind of off topic c programming question.

Also, there's another bit of a small discrepancy here that you made logically, but doesn't actually work. First, a short description of pointers...

Pointers are basically variables that don't take up any space. They don't store data, they store a memory address that points to something that does store data. So if I had an unsigned character variable Var, and it had a value of 123, I could make a pointer Pnt of the same type (unsigned char), that wouldn't take up the full 8 bits of memory space, but would instead redirect the computer to read from wherever Var is stored. There are two things you have to deal with (and they get very tricky) when working with pointers; lvalues and rvalues.

lvalues (left values) are what you see on the left side of an equal sign, you're taking an actual quantity and telling the computer where to put it. The lvalue is just the address of the variable. rvalues (right values), are the actual quantity value that you're reading or putting into another variable. When working with pointers, you have to specify, because it changes whether you are reading or writing to the variable which it points to. You specify an address (lvalue) by an ampersand (&), and you specify the value (rvalue) as an asterisk (*).

This brings us to scanf. You call scanf as "scanf("%c", &Var);" This is because you're writing a value to Var, and you need to know where to put it. However, when you want to display the same variable with printf, it needs to know what it's showing, not where to look, so you use the rvalue, but there's no asterisk needed, because normal (non-pointer) variables are assumed to either be an rvalue or an lvalue depending on where they show up. As a parameter to a function (something in between parentheses, no joke intended), it's assumed you want to give that function data, so the computer assumes an rvalue. However, with scanf, you really want to know where to put the data, so you have to specify with an &. You don't for any other common function.

This was a long way of pointing out that you don't need an & in the printf() statement at the end. Trust me, I've made this mistake plenty of times. But the address is actually a really big number that's stored in the register, not memory, which is why you get such a weird and huge value (which is also different every time) if you try to read data from that. Otherwise, just slips and typos here and there, stuff every good programmer does every once in a while.
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not 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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Off Topic Brett Elflord Chit-Chat 1 30-11-2007 18:05
OFF TOPIC: av switch matt111 Electrical 2 21-03-2004 15:34
Threads getting off topic Mike Soukup CD Forum Support 29 20-08-2001 04:08
chat/off-topic Brandon Martus General Forum 6 06-08-2001 10:10
off topic thread mike o'leary Chit-Chat 4 31-07-2001 22:55


All times are GMT -5. The time now is 04:14.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi