Lecture%2004%20-%20%20Pointers%20and%20Strings

Lecture%2004%20-%20%20Pointers%20and%20Strings - Lecture 4...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2008 Ananda Gunawardena Lecture 4 Strings and Pointers In this lecture we will discuss the following topics Introduction to Strings Reading a String Writing a String Passing a String to/from a function Swapping strings Understanding strings.h library Tokenizing a String Introduction to pointers Initializing and dereferencing pointers Further references Exercises Learning how to manipulate strings is quite important in any programming language. In Java string is an object and inherits its object properties. However, in C string is an object with no inherited properties (such as length). First we will begin with the concept of a pointer or address. We will discuss in detail what pointers mean shortly, but for now we want to start with a definition as follows. char* s; The above statement simply indicates that s is a pointer to (or address of) a character. A String is simply defined as an array of characters and s is the address of the first character (byte) of the string. We recall that in Java, a String is an object that inherits many methods. [A complete description of Java strings can be found in its API]. But in C, a string is just an array of characters that does not have any inherited properties. A valid C string ends with the null character ‘\0’ [slash zero] . Therefore the amount of memory required for a C string is 1 + length of the actual string. Failure to make sure that a string ends with ‘\0’ may result in unpredictable behavior in your code. Please note that some IO library functions automatically adds a null character to the end of each string and we will state those instances as we discuss them.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyright @ 2008 Ananda Gunawardena Initializing a String A constant string s can be simply initialized as char* s = “guna\0”; However no memory is allocated for s. If we try to write to s, then it may cause a segmentation fault since memory has not been allocated explicitly. For example, fscanf(stdin,”%s”,s); would cause a problem So it is important to allocate memory first and then copy the string to the location. To allocate a block of memory to hold a string, we use the malloc function from <stdlib.h>. To read more about malloc type: % man malloc The malloc(int n) returns a pointer to (or an address of) a block of n bytes. Note that a string with n characters requires n+1 bytes (n for the string AND 1 byte to store the ‘\0’ character). Therefore, to store the input string “guna”, we would require 5 characters. The following code allocates 5 characters to store the string “guna” + ‘\0’. char *S = malloc(5*sizeof(char)); strcpy(S,”guna”); It is important to note that malloc allocates memory inside what is called the “dynamic heap” and unless memory is explicitly freed using free function ( we will discuss this later – a very important topic ), the malloced block stays even after leaving the scope of the code. Alternatively we can also write
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Lecture%2004%20-%20%20Pointers%20and%20Strings - Lecture 4...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online