An Introduction to Pointers
• A pointer(address variable) is a variable whose value is used to point to
another variable.
Ex:
long int x, y;
y = &x;
(assigns address of the long integer variable x to another variable, y.)
Ex:
#include <stdio.h>
main()
{
char c;
int x;
float y;
printf("c: address=0x%p, content=%cn", &c, c);
printf("x: address=0x%p, content=%dn", &x, x);
printf("y: address=0x%p, content=%5.2fn", &y, y);
c = `A';
x = 7;
y = 123.45;
printf("c: address=0x%p, content=%cn", &c, c);
printf("x: address=0x%p, content=%dn", &x, x);
printf("y: address=0x%p, content=%5.2fn", &y, y);
return 0;
}
NOTE: %p, %u, %lu
• Pointer has Left value and Right value.
– Left value: address its self
– Right value: which is the content of the pointer, is the address of another variable.
– data-type *pointer-name;
• Ex:
#include <stdio.h>
main()
{
char c, *ptr_c;
int x, *ptr_x;
float y, *ptr_y;
c = `A';
x = 7;
y = 123.45;
printf("c: address=0x%p, content=%cn", &c, c);
printf("x: address=0x%p, content=%dn", &x, x);
printf("y: address=0x%p, content=%5.2fn", &y, y);
ptr_c = &c;
printf("ptr_c: address=0x%p, content=0x%pn", &ptr_c, ptr_c);
printf("*ptr_c => %cn", *ptr_c);
ptr_x = &x;
printf("ptr_x: address=0x%p, content=0x%pn", &ptr_x, ptr_x);
printf("*ptr_x => %dn", *ptr_x);
ptr_y = &y;
printf("ptr_y: address=0x%p, content=0x%pn", &ptr_y, ptr_y);
printf("*ptr_y => %5.2fn", *ptr_y);
return 0;
}
Storing Similar Data Items
• array is a collection of variables that are of the same data type.
– data-type Array-Name[Array-Size];
EX:
#include <stdio.h>
main()
{
int i;
int list_int[10];
for (i=0; i<10; i++){
list_int[i] = i + 1;
printf( "list_int[%d] is initialized with %d.n", i, list_int[i]);
}
return 0;
}
One dimensionl Array
• One dimensional array
– Int a[10], float x[50], char str[30];
– Int a[10]={12,3,4,65,6,2,3}; int
a[]={1,2,3,4,5,6,7,8};
– Char str[30]=“Hello how well?”;
– ‘0’=null remain
Ex:
#include <stdio.h>
main()
{
char array_ch[7] = {`H', `e', `l', `l', `o', `!’};
int i;
for (i=0; i<7; i++)
printf("array_ch[%d] contains: %cn", i, array_ch[i]);
/*--- method I ---*/
printf( "Put all elements together(Method I):n");
for (i=0; i<7; i++)
printf("%c", array_ch[i]);
/*--- method II ---*/
printf( "nPut all elements together(Method II):n");
printf( "%sn", array_ch);
return 0;
}
Ex:
#include <stdio.h>
main()
{
char array_ch[15] = {`C', ` `,
`i', `s', ` `,
`p', `o', `w', `e', `r',
`f', `u', `l', `!', `0'};
int i;
/* array_ch[i] in logical test */
for (i=0; array_ch[i] != `0'; i++)
printf("%c", array_ch[i]);
return 0;
}
INSERT, SEARCH, SORT, DELETE in one
demensional Array
Multidimensional Arrays
• data-type Array-Name[Array-Size1][Array-
Size2]. . .[Array-SizeN];
– int array_int[2][3];
– int array_int[2][3] = {{1, 2, 3}, {4, 5, 6}};
Unsized Arrays
• int list_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90};
• char list_ch[][2] = { `7', `A', `b', `B',`c', `C',`d',
`D',`e', `E', `f', `F',`g', `G'};
Manipulating Strings
• a string is a character array terminated by a
null character (0).
• char array_ch[7] = {`H', `e', `l', `l', `o', `!', `0'};
• char str[7] = "Hello!";
• char str[] = "I like C.";
• char *ptr_str = "I teach myself C.";
• char ch = `x';
• char str[] = "x";
• char *ptr_str;
ptr_str = "A character string.";
#include <stdio.h>
main()
{
char str1[] = {`A', ` `,
`s', `t', `r', `i', `n', `g', ` `,
`c', `o', `n', `s', `t', `a', `n', `t', `0'};
char str2[] = "Another string constant";
char *ptr_str;
int i;
/* print out str2 */
for (i=0; str1[i]; i++)
printf("%c", str1[i]);
printf("n");
/* print out str2 */
for (i=0; str2[i]; i++)
printf("%c", str2[i]);
printf("n");
/* assign a string to a pointer */
ptr_str = "Assign a string to a pointer.";
for (i=0; *ptr_str; i++)
printf("%c", *ptr_str++);
return 0;
}
#include <stdio.h>
main()
{
char str[80];
int i, delt = `a' - `A';
printf("Enter a string less than 80 characters:n");
gets( str );
i = 0;
while (str[i]){
if ((str[i] >= `a') && (str[i] <= `z'))
str[i] -= delt; /* convert to upper case */
++i;
}
printf("The entered string is (in uppercase):n");
puts( str );
return 0;
}
char *str = "Hello World!";
• Alternatively one might think that a pointer to string in C is a pointer to a char array
/ a pointer to a pointer to a char.
• char *str = "Hello World!";
– str is a pointer to a string.
– str is a pointer to a char array.
– But str is a pointer to a char (not a pointer to a pointer to a
char). str is of type char * .(and here str points to H).
– Ended by 0 null
– char *v1=“A”; 2 bytes
– char v1=‘A’; 1 byte
– ‘A’ + 20 + ‘C’ but not “A” + 20 + “C”
H e l l o W o r l ! 0
A 0
A
Arr[6]=“12345”;
• char arr[6]=“gfhgf”;
• char arr[6]={‘1’,’2’,’3’,’4’,’5’};
• char arr[]=“3212432432”;
1 2 3 4 5 0

4. chapter iii

  • 1.
  • 3.
    • A pointer(addressvariable) is a variable whose value is used to point to another variable. Ex: long int x, y; y = &x; (assigns address of the long integer variable x to another variable, y.) Ex: #include <stdio.h> main() { char c; int x; float y; printf("c: address=0x%p, content=%cn", &c, c); printf("x: address=0x%p, content=%dn", &x, x); printf("y: address=0x%p, content=%5.2fn", &y, y); c = `A'; x = 7; y = 123.45; printf("c: address=0x%p, content=%cn", &c, c); printf("x: address=0x%p, content=%dn", &x, x); printf("y: address=0x%p, content=%5.2fn", &y, y); return 0; } NOTE: %p, %u, %lu
  • 4.
    • Pointer hasLeft value and Right value. – Left value: address its self – Right value: which is the content of the pointer, is the address of another variable. – data-type *pointer-name; • Ex: #include <stdio.h> main() { char c, *ptr_c; int x, *ptr_x; float y, *ptr_y; c = `A'; x = 7; y = 123.45; printf("c: address=0x%p, content=%cn", &c, c); printf("x: address=0x%p, content=%dn", &x, x); printf("y: address=0x%p, content=%5.2fn", &y, y); ptr_c = &c; printf("ptr_c: address=0x%p, content=0x%pn", &ptr_c, ptr_c); printf("*ptr_c => %cn", *ptr_c); ptr_x = &x; printf("ptr_x: address=0x%p, content=0x%pn", &ptr_x, ptr_x); printf("*ptr_x => %dn", *ptr_x); ptr_y = &y; printf("ptr_y: address=0x%p, content=0x%pn", &ptr_y, ptr_y); printf("*ptr_y => %5.2fn", *ptr_y); return 0; }
  • 5.
    Storing Similar DataItems • array is a collection of variables that are of the same data type. – data-type Array-Name[Array-Size]; EX: #include <stdio.h> main() { int i; int list_int[10]; for (i=0; i<10; i++){ list_int[i] = i + 1; printf( "list_int[%d] is initialized with %d.n", i, list_int[i]); } return 0; }
  • 6.
    One dimensionl Array •One dimensional array – Int a[10], float x[50], char str[30]; – Int a[10]={12,3,4,65,6,2,3}; int a[]={1,2,3,4,5,6,7,8}; – Char str[30]=“Hello how well?”; – ‘0’=null remain
  • 7.
    Ex: #include <stdio.h> main() { char array_ch[7]= {`H', `e', `l', `l', `o', `!’}; int i; for (i=0; i<7; i++) printf("array_ch[%d] contains: %cn", i, array_ch[i]); /*--- method I ---*/ printf( "Put all elements together(Method I):n"); for (i=0; i<7; i++) printf("%c", array_ch[i]); /*--- method II ---*/ printf( "nPut all elements together(Method II):n"); printf( "%sn", array_ch); return 0; } Ex: #include <stdio.h> main() { char array_ch[15] = {`C', ` `, `i', `s', ` `, `p', `o', `w', `e', `r', `f', `u', `l', `!', `0'}; int i; /* array_ch[i] in logical test */ for (i=0; array_ch[i] != `0'; i++) printf("%c", array_ch[i]); return 0; }
  • 8.
    INSERT, SEARCH, SORT,DELETE in one demensional Array
  • 9.
    Multidimensional Arrays • data-typeArray-Name[Array-Size1][Array- Size2]. . .[Array-SizeN]; – int array_int[2][3]; – int array_int[2][3] = {{1, 2, 3}, {4, 5, 6}};
  • 10.
    Unsized Arrays • intlist_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90}; • char list_ch[][2] = { `7', `A', `b', `B',`c', `C',`d', `D',`e', `E', `f', `F',`g', `G'};
  • 11.
    Manipulating Strings • astring is a character array terminated by a null character (0). • char array_ch[7] = {`H', `e', `l', `l', `o', `!', `0'}; • char str[7] = "Hello!"; • char str[] = "I like C."; • char *ptr_str = "I teach myself C.";
  • 12.
    • char ch= `x'; • char str[] = "x"; • char *ptr_str; ptr_str = "A character string.";
  • 13.
    #include <stdio.h> main() { char str1[]= {`A', ` `, `s', `t', `r', `i', `n', `g', ` `, `c', `o', `n', `s', `t', `a', `n', `t', `0'}; char str2[] = "Another string constant"; char *ptr_str; int i; /* print out str2 */ for (i=0; str1[i]; i++) printf("%c", str1[i]); printf("n"); /* print out str2 */ for (i=0; str2[i]; i++) printf("%c", str2[i]); printf("n"); /* assign a string to a pointer */ ptr_str = "Assign a string to a pointer."; for (i=0; *ptr_str; i++) printf("%c", *ptr_str++); return 0; }
  • 14.
    #include <stdio.h> main() { char str[80]; inti, delt = `a' - `A'; printf("Enter a string less than 80 characters:n"); gets( str ); i = 0; while (str[i]){ if ((str[i] >= `a') && (str[i] <= `z')) str[i] -= delt; /* convert to upper case */ ++i; } printf("The entered string is (in uppercase):n"); puts( str ); return 0; }
  • 15.
    char *str ="Hello World!"; • Alternatively one might think that a pointer to string in C is a pointer to a char array / a pointer to a pointer to a char. • char *str = "Hello World!"; – str is a pointer to a string. – str is a pointer to a char array. – But str is a pointer to a char (not a pointer to a pointer to a char). str is of type char * .(and here str points to H). – Ended by 0 null – char *v1=“A”; 2 bytes – char v1=‘A’; 1 byte – ‘A’ + 20 + ‘C’ but not “A” + 20 + “C” H e l l o W o r l ! 0 A 0 A
  • 16.
    Arr[6]=“12345”; • char arr[6]=“gfhgf”; •char arr[6]={‘1’,’2’,’3’,’4’,’5’}; • char arr[]=“3212432432”; 1 2 3 4 5 0

Editor's Notes

  • #9 #include<stdio.h> #include<conio.h> void main() { clrscr(); int k,n; int d[60]; printf("Enter n:"); scanf("%d",&n); //insert for(int i=0;i<n;i++) { printf("Enter d[%d]=",i); scanf("%d",&d[i]); } //duplicate for( i=0;i<n-1;i++) for(int j=i+1;j<n;j++) { if(d[i]==d[j]) { n=n-1; for(k=j;k<n;k++) { d[k]=d[k+1]; } j--; } } //sort DESC int t; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(d[i]<d[j]) { t=d[i]; d[i]=d[j]; d[j]=t; } //Delete int dn; printf("Enter number to delete:");scanf("%d",&dn); for(i=0;i<n;i++) { if(dn==d[i]) { n=n-1; for(j=i;j<n;j++) d[j]=d[j+1]; i--; } } //search int b=1; int sn; printf("Enter number to search:");scanf("%d",&sn); for(i=0;i<n;i++) { if(sn==d[i]) b=0; } if(b==1) { printf("Not Found"); } else { printf("I Found"); } //output for(i=0;i<n;i++) { printf("d[%d]=%d\n",i,d[i]); } getch(); }