14 Jo P Feb 08
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
409
On Slideshare
409
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. The Joy of Programming Understanding Pointers in C S.G. GaneSh Pointers are the forte of C, are the most difficult to master, and programming with them is prone to errors. But pointers are fun too! This month, we’ll look at some puzzles to understand some interesting aspects of pointers in C. In the following programs, assume that necessary header to decide, so check the answers first: files are included. A1. This program will run fine without an assertion failure. Q1. Will this program result in an assertion failure? Sizes of all pointer types are equal! This might be surprising to many programmers, but it is easy to understand. Pointers int main() { signify an address. In general, for a given implementation, assert(sizeof(void *) == sizeof(int *)); the storage space required for storing an address is the same, assert(sizeof(int *) == sizeof(int **)); irrespective of the type of pointer used. } A2. This program results in a compiler error for the expression ‘i + j’. Why? Pointers signify the address and it Q2. What will this program print? is illogical to add two addresses. However, you can add an integer value to an address; for example, an array-based int main() { address is a pointer and to locate an array element, it is int iarr[10]; enough to simply add an integer to that address. Pointer int *i = &iarr[2], *j = &iarr[5]; subtraction is allowed; in this case, for example, ‘i – j’ int *k = i + j; indicates the number of array elements between them, which int diff = j – i; is equivalent to the expression (&iarr[5] - & iarr[2]), and is printf(“%d”, diff); always 3, irrespective of the size of int. } A3. Old C compilers or modern C compilers in K&R C mode (which refers to pre-ANSI C -- the original C language Q3. Will this program work? defined by Dennis M. Ritchie) do not have strong type checking and, hence, they will compile this fine. int main() { In the underlying implementation, if the size of int and int i = “C is often unpredictable!”; the size of the pointer are the same, then there is no problem printf(i); in storing the address of the string literal in integer i. printf } is a dumb routine and it will interpret the first argument as a string (in fact, i has an address of a string literal). So, this Q4. What does the following program print? program might compile and print: “C is often unpredictable!” A4. Yes, this program will print “Joy”! Note that strncpy int main() { returns a char* which is the address of the copied string. char string[10]; Here, strncpy copies three characters and returns that string. printf(strncpy(string ,”Joy of C”,3)[3] = ‘0’); Then, we do indexing on that returned char* and put the null } terminator ‘0’ for that string in the index position [3]. The printf gets “Joy” as the argument and prints it. Q5. What does this following program print? A5) This program results in a compiler error for the expression ‘&&i’. The ‘&&’ operator is a logical ‘and’ operator int main() { and requires two operands. Ignoring this syntax issue, the // assume that address of i is 0x1234ABCD more important problem is that the attempted expression int i = 10; is illogical. It is possible to take ‘address of i’ with &i; but int * ip = &i; address of ‘address of i’ cannot exist! int **ipp = &&i; By: S G Ganesh is a research engineer at Siemens printf(“%x, %x, %x”, &i, ip, *ip); (Corporate Technology), Bangalore. His latest book is ‘60 } Tips on Object Oriented Programming’ published by Tata McGraw-Hill in December 2007. You can reach him at Well, they don’t seem too difficult, do they? It is too soon sgganesh@gmail.com www.openITis.com | LINUX For YoU | FebrUarY 2008 127 cmyk
  • 2. 128 FebrUarY 2008 | LINUX For YoU | www.openITis.com cmyk
  • 3. www.openITis.com | LINUX For YoU | FebrUarY 2008 129 cmyk