Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c programming and only use the functions given nothing else 1. Try this program

ID: 3732732 • Letter: C

Question

c programming and only use the functions given nothing else

1. Try this program to see how you can use pointers. include void foo (int a, int "bp) *bp 5: int main(int argc, char argv) foola, &b;) printf ("a sd b-sdo", a, b) When run, you should get: s./a.out a-0 b-s By using pointers, the foo function is able to change the b variable from the main function. Create a function to capitalize the string. Because you are passing the string as a pointer, the function will change the contents of the original string instead of changing the contents of a copy 2. #include int main(int argc, char argy) char string( 100)"ThiS AS strcap (string) printf ("The capitalized stineNNATAHERHig Long istring" When run, you should get: $./a.out the capitalized string is TH1

Explanation / Answer

1)

#include <stdio.h>

void foo(int a,int *k)   

{

// This function has two arguments which was a and *k and those two are local to this function

// Which means a and k life time is end of foo function i.e "}". So they dont have life when it finishes foo(){.....} function

// As you passed a as a variable to foo function, it gets stored in some location let say 1000

// Location 1000 consists value '0' as you passed '0' in foo function in main

a=4; // Here you are changing value of a to 4 i.e location 1000 consists value 4 now

// You are passing address to foo in function call in main i.e you are passing 4000 to foo function

*k=5; // Now you are assigning value '5' to the address location 4000

  

} // Now we are end of the function and a and k doesn't have life.

int main() {

//code

int a=0,b=0; // Initialising two variables and let say address of a is 2000 and b is 4000

foo(a,&b); // Here you are passing two arguments and one is a value(a) i.e 0 and other is the address(&b) i.e 4000

// When foo function is completed the control returns and a value in foo function doesn't have scope here

// So while you print a it refers to current variablle of the main function

// Which means value at location 2000 address i.e 0

// But we passed &b which means 4000 address it got stored in some k location in foo function

// And you changed the value in 4000 address to 5 in foo and even k doesn't have life after foo function

// But when you print b here it means value at location 4000, so you already changed value at 4000 to 5 in foo function

// So it prints 5

printf("a=%d b=%d",a,b);

return 0;

}

2)

#include <stdio.h>

void strcap(char str[])   

{

// So the 3000 address passed in the strcap in main function enters and been copied to str[0]

// So whenever you are doing operations in str you are actually changing the address which has been copied to this function i.e 3000

int i=0;

while(str[i]!='') // Checking end of the char array

{

if(str[i]>='a' && str[i]<='z') // checking whether the letter is an alphabet or not

{

str[i]=str[i]-32; // if its an alphabet increment by 32 which means 'a'+32='A' based on ASCII values Similarly 'b'+32='B'

}

  

i++; // Increment for each index

}

  

} // Once the control completed strcap function str has no life time but indirectly it copied values to the original addresses like 3000, 3001 .......

int main() {

char string[100]="This is really strong"; // Initialising a char array where string[0] was located at some address location let say 3000, so 3000 has 'T' and 3001 has 'h' .....

strcap(string); // Now you are passing address of the string to strcap function i.e 3000

// Even though we str doesnt has life after strcap, it modified the values of 3000, 3001 etc....

printf("The capitalised string is %s ",string);  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote