Understanding Pointers and Strings in C: Methodology Tutorial
This tutorial explores critical concepts in C programming, focusing on pointers and strings. It includes practical examples, such as how to manipulate character pointers, swap strings, and understand variable scope in functions. We will cover multiple scenarios illustrating how pointer arithmetic works, including accessing and swapping elements in arrays. Additionally, we examine common pitfalls to avoid, such as dereferencing null pointers and the implications of string manipulation in memory. Ideal for beginners looking to strengthen their understanding of C programming fundamentals.
Understanding Pointers and Strings in C: Methodology Tutorial
E N D
Presentation Transcript
CS1010E Programming MethodologyTutorial 8Pointers and Strings C14,A15,D11,C08,C11,A02
Question 1 list1 list2
Question 1 (a) list1 list2 char*ptr; ptr= list1[2]; printf("%c",ptr); ‘U’ 0x000000055 ptr takes in an address, it will treat ‘u’ as address of ‘0x00000055’. This may be some protected location in your system!! ‘U’ ptr
Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]);
Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]); Scope of swap ptr1 ptr2
Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]); Scope of swap ptr1 ptr2 dummy
Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]); Scope of swap ptr1 ptr2 dummy
Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]); Scope of swap ptr1 ptr2 dummy
Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]); Scope of swap printf("%c %c\n", list1[2], list1[4]); u e ptr1 ptr2 dummy
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4])
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4]) Scope of swap2 ptr1 ptr2
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4]) Scope of swap2 dummy ptr1 ptr2
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4]) Scope of swap2 ‘U’ dummy ptr1 ptr2
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4]) Scope of swap2 ‘U’ dummy ptr1 ptr2
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4]) Scope of swap2 ‘U’ dummy ptr1 ptr2
Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4]); Scope of swap2 ‘U’ dummy printf("%c %c\n", list1[2], list1[4]); E U ptr1 ptr2
Question 1 (d) • printf("%s", list1); • student • printf("%s", &list1[0]); • student • printf("%s", list2[3]); • project • printf("%s", &list2[3][0]); • project list1 list2 “%s” will print everything until the first ‘\0’.
Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr
Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr
Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr
Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr UTORIAL
Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr
Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr
Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr “++” has high priority than “*”!
Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr “++” has high priority than “*”! U D
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 0
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 0
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group Count = 0
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group, about to leave Count = 1
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 1
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group Count = 1
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group, about to leave Count = 2
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 2
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group Count = 2
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group, about to leave Count = 3
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually End of string, return Count = 3
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually We increase our counter when: leaving a duplicate group! How do we detect when this? str[i] == str[i-1] && str[i] != str[i+1]
Question 2 • Counting duplicating groups • intrepeat(char str[]); • Algorithm • Count = 0; • For every str[i] in string str[0… len], • if(str[i] == str[i-1] && str[i] != str[i+1]) count++; • Return count; How do we detect when this? str[i] == str[i-1] && str[i] != str[i+1]
Question 2 int repeat(charstr[]){ intcount =0,i=1; for(;i<strlen(str);i++){if(!isspace(str[i]) &&str[i]==str[i-1] &&str[i]!=str[i+1]){ count++; } } returncount; } Code for counting duplicate groups
Question 3 • Delete a substring from main string • Analysis: • To delete a substring, we need to find a string • To find a string, we need to check every character of string for match • After find a string, delete it from string • Suppose we have three functions: • findstr(mainstr, substr); • Find first occurrence of substr in mainstr • delstr(mainstr, index, sublen); • delete from main string at position index
Question 3 char*delete(char*mainstr,char*substr){ intindex,sublen=slength(substr);//find the position of sub string in the main string index =findstr(mainstr,substr); if(index==-1){//no sub string found printf("No sub-string found! Task not successful!\n"); return0; }else{//delete the substring delstr(mainstr, index,sublen); returnmainstr; } } Skeleton of delete function
Question 3 • Compute string length • will be used during deletion • Algorithm: • scanning from string head • count until you find ‘\0’; intslength(char*str){ inti=0; while(str[i]!='\0') i++; returni; }
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Don’t match
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Don’t match
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Don’t match
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Don’t match
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Don’t match
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Don’t match
Question 3 • Find the first occurrence of substring • Algorithm: • Scan from head of string • Check whether there is a substring match substr at each position • Return the matching position Match & return
Question 3 intfindstr(char*mainstr,char*substr){ inti=j=equal=0; intstart=-1, mainlen=slength(mainstr), sublen=slength(substr); intmaxstartindex=mainlen-sublen; //find the first character of substring in the main string for(i=0;i<=maxstartindex&&!equal;i++) if(substr[0]=='*'||mainstr[i]==substr[0]){ equal=1; start=i; //compare the following characters for(j=0; j<sublen; j++) if(substr[j]!='*'&&mainstr[i+j]!=substr[j]){ equal=0; start=-1; } } returnstart; } Code for findstr