3. • 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
4. • 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;
}
5. 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;
}
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;
}
11. 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.";
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];
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;
}
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