Saturday, January 2, 2010

Pointers

Pointers

  • A variable that represents the location of a data item
  • Used to pass information between a function and its reference point
  • Provide a way to return multiple data items from a function
  • Every data item is stored in the computer memory and it occupies one or more bytes of contiguous memory cells
  • Example

char letter;

letter = 'A'

letter

5566

65

(Address Memory Location)

  • The address of the variable can be determined using the expression, &letter
  • The address can be stored to a new variable
    • pv = &letter
  • The variable pv contains the address of the variable (1188) "letter"
  • The new variable is called pointer to "letter" since it points to the location where the letter is stored in memory
  • The data item represented by "letter" can be accessed by the expression *pv where * is an unary operator called indirection operator
  • The indirection operator * can operate only on address
  • Example 1

#include

int main (void){

char letter;
char *pletter;
letter = 'A';

printf("The address of the variable letter is %x\n", &letter);
pletter = &letter
printf("The address of the variable letter is %x\n", pletter);
printf("The character stored in the letter is %c\n, letter);
printf("The character stored in the letter is %c\n, *pletter);

return 0;
}

Result:
The address of the variable letter is ABCD
The address of the variable letter is ABCD
The character stored in the letter is A
The character stored in the letter is A

  • Example 2

#include

int main (void){

int a, b;
int pta;
a = 10;
pta = &a;
b = 2 * (*pta + 15);
printf("The expression value is %d\n", b);
return 0;
}


Result:
The expression value is 50

%s format and %[ ] format specifier

  • The %s format and the square bracket specifiers are used to read string of characters
  • %s format will read all the character from the input buffer till it encounters a blank space
  • %[ ] format will read all the character from the input buffer till it encounters a character that is not found in the list within the square bracket
  • ^ symbol is used as first symbol in side the %[ ] format specifier then it represents compliment of characters
  • Example 1:

#include

int main (void){

char string[40];
char string1[40];

printf("Please enter the input string:");
scanf("%s", string);
printf("Please enter the input string1:");
scanf("%[1234567890abcdefghijklmnopqrstuvwxyz]", string1);
printf("The output for variable string is: %s\n", string);
printf("The output for variable string1 is: %s", string1);

return 0;
}

Result:
Please enter the input string: Today is very hot
Please enter the input string1: Today is very hot
The output for variable string is: Today
The output for variable string1 is: Today is very hot

Pointer and One Dimensional Array

1800

1802

1804

1806

1808

(2 bytes)

(2 bytes)

(2 bytes)

(2 bytes)

(2 bytes)

50

60

70

80

90

mark[0]

mark[1]

mark[2]

mark[3]

mark[4]

*mark

*(mark + 1)

*(mark + 2)

*(mark + 3)

*(mark + 4)

  • int mark [5] = {50, 60, 70, 80, 90}
  • The array of the first array element of mark can be represented as &mark[0] or simply by the mark itself
    • &mark[0] and mark represent the same memory address which is the 1800
  • The address of second array element can be written as &mark[1] or (mark + 1)
  • In general, the address of (i +1)th array element cab be written as &mark[i] or (mark + i)
  • Example:

#include

int main (void){

int i;
int x[5] = {50, 60, 70, 80, 90};
for(i=0;i<5;i++)
printf("i = %d, address is %x, value is %d\n", i, &x[i], x[i]);
printf("\n");
for(i=0;i<5;i++)
printf("i = %d, address is %x, value is %d\n", i, (x+i), *(x+i));
return;
}

Output:
0 address is 1800 value is 50
1 address is 1800 value is 60
2 address is 1800 value is 70
3 address is 1800 value is 80
4 address is 1800 value is 90

0 address is 1800 value is 50
1 address is 1800 value is 60
2 address is 1800 value is 70
3 address is 1800 value is 80
4 address is 1800 value is 90

No comments: