

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 33
This page cannot be seen from the preview
Don't miss anything!
Pointers and 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";
#include<stdio.h> int main() { char name[] = "Klinsman"; int i = 0; while(i<=7) { printf("%c",name[i]); i++; } return 0; }
#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; }
Suppose we want to access the i th entry of a character array a We can write any of the following expressions:
#include<stdio.h> int main() { char name[20]; printf("Enter name: "); scanf("%s",name); printf("Hello %s!",name); return 0; }
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
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; }
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**
#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; }
#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; }
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.