You are on page 1of 68

CSD101: Introduction to

Computing and Programming


Lecture 14
Covered
• Pointer
• Declaration
• Assignment
• l-value and r-value
• Example programs with pointer
Today’s Lecture…
• Pointer
• NULL and Void pointer

• Strings
• Characters and Strings
• Pointers and Strings
Pointer Declaration

int *j;

• The ‘*’ in declaration tells the compiler that the variable is a pointer
• Operator *: value at address
• int *j : value at the address contained in j is an int.
• The int says that the pointer variable can point to an integer variable
Pointer

j = &i;
• j is a variable that contains the address of another variable i.
• Being a variable j also gets memory allocated to it.
i j
*j
5 62334
62334 62366
• Special name for j is pointer variable.
• Can hold address of other variable but one at a time.
Operator Precedence and Associativity
Null Pointer
• A pointer may be initialized to NULL, 0 or an address.
int *p = NULL;
• A pointer with the value NULL points to nothing.
• Initializing a pointer to 0 is equivalent to initializing a pointer to
NULL, but NULL is preferred.
• The value 0 is the only integer value that can be assigned directly
to a pointer variable.
• Constant NULL defined in <stdio.h>
• No comparison of Null pointer to general pointer.
Example: NULL Pointer
Example: NULL Pointer
Void Pointer

void *p;

• A generic pointer, can point any variable type.


Example: can assigned to char/int/float variable.
• Uses type-casting to change a pointer from one type to another
Example: Void Pointer
Example: Void Pointer
Strings
Strings

5
\0

Image source: web


Strings
• A group of characters forming an array i.e., a character array, also
called a string.
• A string constant is a one-dimensional array of characters, terminates
by a null character ‘\0’, enclosed in double quotes.
Initialization:
char name[]={‘S’,’W’,’E’,’T’,’A’,’\0’};
char name[6]=“SWETA”;
char name[]=“SWETA”;
• Last character is always ‘\0’.
S W E T A \0
1001 1002 1003 1004 1005 1006
Example
Example
Difference between String and Character
• char c = ‘A’; A 1-byte

• char c[] = “A”; A \0 2-bytes

• char c[] = “”; \0 1-byte


Character Pointer
• We can assign the address of a string in a char pointer as follows:

char *s = “Namaste!”

Constant string

• No space allocated to the string as s is a pointer.


• Content of s can not be changed so use such assignments when
modification is not required.
• String is stored in read-only memory so is a constant string.
Character Pointer: Useful when?
• We cannot assign a string to another
char s1[]= “Namaste!”
char s2[10];
s2 = s1; Invalid!

• Solution:
char *s1, *s2= “Namaste!”;
s1 = s2;
Character Pointer: Useful when?
• We cannot change the value once a string is being assigned a value
char s1[]= “Namaste!”
s1[]= Hello!; Invalid!

• Why???
Character Pointer: Useful when?
• We cannot change the value once a string is being assigned a value
char s1[]= “Namaste!”
s1[]= Hello!; Invalid!

• s1 is constant pointer to the string


Character Pointer: Useful when?
• We cannot change the value once a string is being assigned a value
char s1[]= “Namaste!”
s1[]= Hello!; Invalid!

• Solution:
char *s1= “Namaste!”;
s1 = “Hello!”;
Example
Example
Example
Example
Example

Pointer to a constant string

Invalid! Trying to change the value of a constant string


Example
Example
Example

Constant pointer to a string


Example

Constant pointer to a string


Example: Character Pointer and Strings
Constant Pointer

Constant String
Example: Character Pointer and Strings
Constant Pointer

Constant String

assignment using pointer


Example: Character Pointer and Strings

String format specifier


Example: Character Pointer and Strings
Pointers and Strings
• A string is accessed via a pointer to the first character in the string.
• The value of a string is the address of its first character.
• Thus, in C, it’s appropriate to say that a string is a pointer—in fact, a
pointer to the string’s first character.
• In this sense, strings are like arrays, because an array is also a pointer
to its first element.
Example: malloc()
• malloc() requires the number of bytes to be allocated and returns the
base address of the chunk of memory that it allocates

char *a= (char *) malloc (num_items * sizeof(char));

• The address returned by this function is always of type void*


• So type casting using the expression (char *)
Example
Passing a String to a function
• A string or an array of strings can be passed as an argument to a
function.
Passing a String to a function
• A string or an array of strings can be passed as an argument to a
function.
Returning a String from a function
Returning a String from a function
Example: String & Pointer
Example: String & Pointer
Example: String & Pointer
Example: String & Pointer
Example: Limitation of Scanf()
Example: Limitation of Scanf()
Example: Solution gets()
String Functions

• strlen()
• strcpy()
• strcat()
• strcmp()
String Function: strlen()
Example: strlen()
Example: strlen()
Example: User Program to Compute strlen
Example: User Program to Compute strlen
String Function: strcpy()
• We cannot assign a string to another
char s1[]= “Namaste!”
char s2[10];
s2 = s1; Invalid!

• Solution:
char *s1, *s2= “Namaste!”;
s1 = s2;
String Function: strcpy()

strcpy (char *t, const char *s);


Example: strcpy()
Example: strcpy()
String Function: strcat()
Example: strcat()
Example: strcat()
String Function: strcmp()
Example: strcmp()
Example: strcmp()
Pointer Arithmetic
• Assignment between pointers of same type;
• Addition between a pointer and an integer;
• Subtraction between a pointer and an integer;
• Comparison between two pointers pointing to the elements of same
array
• Subtraction between two pointers pointing to elements of same
array
• A pointer may be incremented (++) or decremented (--)
References
• C How To Program, Paul Deitel and Harvey Deitel
• Let Us C, Yashavant Kanetkar
• https://nptel.ac.in/courses/106/106/106106133/

You might also like