Docsity

C Programming: Pointers and Strings, Lecture notes of Computer Science

Partial preview of the text

Download C Programming: Pointers and Strings and more Lecture notes Computer Science in PDF only on Docsity!

C Programming

Continued

Pointers and Strings

Strings

  • (^) Group of integers form integer array
  • (^) Group of floats form float array
  • (^) Similarly group of characters form character array or string
  • (^) Strings are used to manipulate text such as words or sentences

Strings

The terminating character '\0' is important because there are several in-built functions that work on strings, and this character is the only way these functions can know that the end of string has been reached. However, while initializing a character array the term '\0' is not mandatory. All the following expressions are equivalent: char a[] = {'H','E','L','L','O','\0'}; char a[] = {'H','E','L','L','O'}; char a[] = "HELLO";

Sample Programs

#include<stdio.h> int main() { char name[] = "Klinsman"; int i = 0; while(i<=7) { printf("%c",name[i]); i++; } return 0; }

Sample Programs

#include<stdio.h> int main() { char name[] = "Klinsman"; char ptr; ptr = name; //Stores base address of the string while(ptr!='\0') //Can also be written as while(ptr!=0)* { printf("%c",*ptr); ptr++; } return 0; }

Accessing Elements from Character

Array

Suppose we want to access the i th entry of a character array a We can write any of the following expressions:

  • (^) a[i]
  • (^) *(a+i)
  • (^) *(i+a)
  • (^) i[a] But all these are rarely used. printf() directly prints the string excluding the โ€˜\0โ€™, if the format-specifier is given as %s.

Examples Using Format Specifier %s

#include<stdio.h> int main() { char name[20]; printf("Enter name: "); scanf("%s",name); printf("Hello %s!",name); return 0; }

Max Array Size to Specify

char name[25] sets aside 25 bytes under the array name[], whereas the scanf() function fills in the characters typed at keyboard into this array until the enter key is hit. Once enter is hit, scanf() places a โ€˜\0โ€™ in the array. Keep in mind

  • (^) The length of the string should not exceed the dimension of the character array. If we carelessly exceed the bounds, there is a danger of overwriting something important
  • (^) scanf() is not capable of receiving multi-word strings. Therefore, strings such as โ€œsee youโ€ would not be acceptable. gets() can be used to get around this limitation
  • (^) Both printf() and puts(str_name) can be used to print a multi-word string

How to make scanf() accept a multi-

word string?

Following method can be followed: #include<stdio.h> int main() { char name[25]; printf("Enter full name: "); scanf("%[^\n]s",name); puts(name); return 0; }

puts() function

  • (^) puts() can display only one string at a time. A statement like puts("
    nHello %s!",name); is invalid. Correct statement is puts(name);
  • (^) On displaying a string, unlike printf(), puts() places the cursor on next line

Pointers and Strings

There is difference in the meaning of these two forms: str is a character array or string p is pointer to the first character in the array ****We cannot assign a string to another, whereas we can assign a character pointer to another**

Pointers and Strings

#include<stdio.h> int main() { char n[] = "HELLO"; char *p = "HELLO"; printf("\n%u %u",n,p); printf("\n%u %u",&n,&p); printf("\n%u %u",&n+1,&p+1); return 0; }

Pointers and Strings

#include<stdio.h> #include<string.h> int main() { char *str1 = "fafasf"; char str2[] = "dsfssfs"; *str1='p' ;// Segmentation fault occurs since we are trying to access a read-only segment of the memory *str2 = 'p' //This is allowed return 0; }

What is the minimum size of a

character array?

It is length of the string plus one since '\0' is placed at last of any string. Thus, if the program segment is: char n[] = "HELLO"; printf("%d",sizeof(n)); Output will be 6.