vintasoft barcode .net sdk C program illustrating pointers in Objective-C

Encoding QR Code JIS X 0510 in Objective-C C program illustrating pointers

C program illustrating pointers
Painting QR-Code In Objective-C
Using Barcode maker for iPhone Control to generate, create QR Code image in iPhone applications.
Bar Code Creation In Objective-C
Using Barcode creator for iPhone Control to generate, create barcode image in iPhone applications.
#include <stdioh> int main (int argc, const char * argv[]) { int avalue = 10; int *pavalue = &avalue; printf("address:%p value:%d", pavalue, *pavalue); return 0; }
Make QR Code ISO/IEC18004 In Visual C#.NET
Using Barcode creation for .NET Control to generate, create QR Code 2d barcode image in VS .NET applications.
Making QR Code ISO/IEC18004 In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
In the previous statement, avalue s value is 10, pavalue points to avalue s memory address, and the printf command prints avalue s address followed by avalue s value, as pavalue points to avalue s address while *pavalue is avalue s value
QR-Code Encoder In .NET Framework
Using Barcode generator for VS .NET Control to generate, create QR image in .NET applications.
Encoding Quick Response Code In VB.NET
Using Barcode maker for .NET framework Control to generate, create Denso QR Bar Code image in VS .NET applications.
NOTE
Code39 Generator In Objective-C
Using Barcode encoder for iPhone Control to generate, create Code39 image in iPhone applications.
DataMatrix Encoder In Objective-C
Using Barcode maker for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.
If you are following along in Xcode, realize your address values will be different from those listed in this chapter s example code results address:0xbffff628 value:10 4 Modify main so the first two lines appear as follows: //int avalue = 10; int avalue; 5 Add the following line to just before the method s return statement: printf("value's actual value:%d", avalue); 6 Compile and run Listing 2-14 contains the incorrect output
Bar Code Generation In Objective-C
Using Barcode creation for iPhone Control to generate, create barcode image in iPhone applications.
Making EAN13 In Objective-C
Using Barcode creation for iPhone Control to generate, create EAN-13 image in iPhone applications.
2:
EAN / UCC - 13 Generation In Objective-C
Using Barcode creation for iPhone Control to generate, create USS-128 image in iPhone applications.
Encode Bar Code In Objective-C
Using Barcode generator for iPhone Control to generate, create barcode image in iPhone applications.
A C Refresher
European Article Number 8 Creation In Objective-C
Using Barcode drawer for iPhone Control to generate, create EAN / UCC - 8 image in iPhone applications.
Barcode Maker In Java
Using Barcode creation for Java Control to generate, create barcode image in Java applications.
Listing 2-14
UPC Code Recognizer In VS .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
Encode Barcode In Java
Using Barcode generation for Eclipse BIRT Control to generate, create bar code image in BIRT applications.
Output from C command-line program
Code 128C Generator In None
Using Barcode generation for Microsoft Excel Control to generate, create Code128 image in Excel applications.
Creating Data Matrix 2d Barcode In None
Using Barcode maker for Online Control to generate, create Data Matrix ECC200 image in Online applications.
[Session started at 2008-09-05 21:35:14 -0400] address:0xbffff628 value:-1073744308 value's actual value:-1073744308 The Debugger has exited with status 0
Draw Code 39 In Objective-C
Using Barcode creation for iPad Control to generate, create Code-39 image in iPad applications.
Bar Code Maker In Java
Using Barcode creator for Android Control to generate, create bar code image in Android applications.
The results are not as expected Although the second printf statement works, the first doesn t Initializing a variable only reserves memory space; it does not assign the variable a value When you refer to an uninitialized variable using a pointer, you get an incorrect result You must initialize a variable with a value before using it
7 Change the method so that avalue is initialized to 10 and then click Build And Go The
debugger console echoes 10, as expected
Dereferencing a Pointer
You can also dereference a pointer by assigning the pointer s location a new value You do this through what s called dereferencing the pointer Consider the code in Listing 2-15 Listing 2-15 Dereferencing a pointer
int a = 10; int *b = &a; *b = 52; printf("value:%d value:%d",*b,a);
The third line sets the content of the memory at the address pointed to by the pointer b to the integer value 52 The address pointed to by pointer b happens to be the variable a, so changing the content changes a s value too, as a is the location pointed to by b Running this code results in both values printing as 52
Pointers and Arrays
One place, where pointers are useful in C programming is arrays A common technique is to iterate through an array using a pointer as an iterator to the array s elements The following project illustrates this technique
iPhone SDK Programming: A Beginner s Guide
Try This
Using an Array with Pointers
1 Create a new command-line application called C Pointer Array 2 Modify main in mainm to appear like Listing 2-16
Listing 2-16
A C program iterating through an pointer array
#include <stdioh> int main (int argc, const char * argv[]) { int values[10]; int *iterator; for(int i = 0; i < 10; i++) { values[i] = i * 2; printf("value: %d ", values[i]); } iterator = values; for(int i = 0; i < 10; i++) { printf("value(%d):%d ", i, *(iterator+i)); } *(iterator+4) = 999; printf("\nvalue of element at 4: %d", values[4]); return 0; } 3 Click Build And Go Listing 2-17 is the debugger s output
Listing 2-17
Debugger Console output
[Session started at 2008-09-05 21:57:35 -0400] value: 0 value: 2 value: 4 value: 6 value: 8 value: 10 value: 12 value:14 value: 16 value: 18 value(0):0 value(1):2 value(2):4 value(3):6 value(4):8 value(5):10 value(6):12 value(7):14 value(8):16 value(9):18 value of element at 4: 999 The Debugger has exited with status 0
What you did in this example was use a pointer to iterate through an array The iterator points to the array s address The iterator first points to the array s element at the zero position The iterator + 1 points to the first position s element The iterator + n points to the nth position s element As you iterate through the array s values, you can use the value at the address to which the iterator points
2:
A C Refresher
Summary
This chapter did not provide enough detail to completely learn C In fact, this chapter hardly scratched C s surface But it did provide you with enough information to understand the rest of this book You must know basic C to understand Objective-C and iPhone programming Hopefully this chapter refreshed your memory enough to begin the next chapter If you are familiar with Java s basic programming structures, C header files, and C pointers, you should have no trouble understanding the next two Objective-C chapters If you are still uncertain, you can find many free online C tutorials using Google But don t worry C is kept to a minimum in this book
Copyright © OnBarcode.com . All rights reserved.