Amazon.com Widgets Pointer Questions
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 23, 2014, 05:16:13 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 13 - Underlying C Language Features
| | | |-+ Pointer Questions
Pages: [1] Go Down
Print
Author Topic: Pointer Questions (Read 972 times)
kevinsmak
Newbie
*
Posts: 4






on: August 24, 2009, 12:23:31 PM

So I'm really trying to understand pointers and I guess I just have some basic questions (chapter 13).

First of all, why even use a pointer?  Why not just use the variable instead?  I asked someone that knows this stuff and they weren't even sure, I think that will help me grasp the concept behind it.

Also I'm thinking *ptr is more of a getter and &ptr is more of a setter, is that correct?
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







Reply #1 on: August 24, 2009, 05:50:41 PM

Have you read through the entire chapter? The last section "How Things Work" explains the relationship between pointers and objects.

Quote
First of all, why even use a pointer?  Why not just use the variable instead?
You would use pointer mainly for indirection. This is a very efficient way to deal with data.

With indirection you are saying "do something with the data located at this memory address".
Without it you would need to supply the data directly (i.e. copy). This may not sound like a big deal until you consider the difference between working with megabytes of data at memory location x or copying over megabytes of data and then working with it.

Edit:
Also, pointers are really the only way you can deal with data of varying size. A perfect example is a string. A standard C-string is just a bunch of characters stored in memory and terminated by a NULL character.

If you want to pass a string to a method, the only way to do it is by using a pointer to the string. Otherwise you would need a different method for each possible size of string that could be passed as an argument.


So why use a pointer for a variable that is only an int, float, char, etc... ?

When a variable is passed as an argument for a method, only the variable's value is passed. The method is essentially working on a copy of the variable.
If you pass a pointer to a variable it is the memory address the variable is stored at that the method receives. This way any modification the method does to the variable's value will be done on the original variable and not a copy.

Quote
Also I'm thinking *ptr is more of a getter and &ptr is more of a setter, is that correct?

Think of '&' as the address of...
When declaring a variable '*' means a pointer to...
When used to 'dereference' a pointer '*' means the value at the address pointed to by...

Code: (Objective-C)
int intValue;         // an integer
int anotherValue;     // another integer
int *intPtr;          // Read it from right to left. 'intPtr' is a pointer to an 'int'

intPtr = &intValue    // Make intPtr point to 'the address of' intValue.

*intPtr = 10;         // set the 'value at the address pointed to by' intPtr to 10. This would be the same as intValue = 10;
anotherValue = *ptr;  // make anotherValue equal to the 'value at the address pointed to by' intPtr.
                      // This would be the same as anotherValue = intValue;

To illustrate the point...
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface MyClass : NSObject
{
}
@end

@implementation MyClass

+(void)indirectModifyInt:(int *)valuePtr
{
  *valuePtr += 10;
  NSLog(@"*valuePtr + 10 = %i", *valuePtr);
}

+(void)modifyInt:(int)value
{
  value += 10;
  NSLog(@"value + 10 = %i", value);
}

@end

int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  int intValue;
  int *intPtr;
    
  intValue = 5;
  intPtr   = &intValue;
  
  NSLog(@"intValue before modifyInt = %i", intValue);
  [MyClass modifyInt:intValue];
  NSLog(@"intValue after modifyInt = %i\n\n", intValue);
  
  NSLog(@"intValue before indirectModifyInt = %i", intValue);
  [MyClass indirectModifyInt:&intValue];
  NSLog(@"intValue after indirectModifyInt = %i\n\n", intValue);
  
  NSLog(@"*intPtr before indirectModifyInt = %i", *intPtr);
  [MyClass indirectModifyInt:&intValue];
  NSLog(@"*inPtr after indirectModifyInt = %i", *intPtr);
  
  [pool drain];
  return 0;
}
Last Edit: August 24, 2009, 06:48:45 PM by rgronlie Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: August 26, 2009, 12:47:47 PM

Historically, pointers have enabled C programmers to produce extremely efficient code, particularly when a pointer to an array of elements is used.  Pointers avoid index calculation in loops, which can save many cycles if multiplication would be otherwise needed each time through the loop to calculate the address of the next element's location in memory.

This code

Code: (Objective-C)
int values [MAXVALUES], sum = 0;

for ( i = 0; i < MAXVALUES; ++i)
    sum += values[i];

can be done with pointers like so:

Code: (Objective-C)
int values [MAXVALUES], *vPtr, *vEnd, sum = 0;

vEnd = &values[MAXVALUES - 1];

for (vPtr = values; vPtr <= vEnd; ++vPtr)
    sum += *vPtr;

and can produce much better code due to the reasons noted.  In this case no indexing is done as the pointer is sequenced through the array using simple addition.

In the popular super-dense case example, this is the classic example of C code that can be used to copy one null-terminated C character string to another location in memory:

Code: (Objective-C)
char *src, *dest;

while ( *dest++ = *src++ )
   ;

Please forgive me for showing this example, but it does illustrate the power of pointers and produces extremely compact machine code.   Smiley   And yes, that's an assignment that's being done inside the while and it's the result of the assignment that's being tested (after the null terminating character is copied, the result of the assignment will be zero and the loop will terminate).

Cheers,

Steve Kochan
Last Edit: August 26, 2009, 04:03:36 PM by skochan Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.