Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

How strings are stored in memory as character arrays in C programming, how to access and modify elements in a character array, and how to use pointers to manipulate strings. It also covers standard library string functions and how to use them in C programs.

Typology: Lecture notes

2019/2020

Uploaded on 09/30/2020

sai-kalyan-1
sai-kalyan-1 🇮🇳

1 document

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Programming
Continued
Pointers and Strings
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

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.