C Questions 1. Consider the following C method header and line of code: void cap
ID: 3763537 • Letter: C
Question
C Questions
1. Consider the following C method header and line of code:
void capitalize(char* a)
char c = ‘c’;
What is the correct way to call capitalize on c?
A. capitalize(c);
B. capitalize(*c);
C. capitalize(&c);
D. capitalize(“c”);
2. This code doesn’t work. It does not swap. Rewrite it so that the swap takes place. Make sure you make changes in main if needed.
#include<stdio.h>;
void swap(int a, int b){
int t = a;
a = b;
b = t;
}
int main(void){
int a = 3;
int b = 4;
swap(a,b);
printf("a value is %d and b value is %d", a, b);
return 0;
}
3. What happens when this is run? Why?
#include <stdio.h>
void killYourPointer(int* ptr){
// NOW IT'S NULL! Mwa ha ha ...
ptr = 0;
}
int main(void) {
int x = 4;
int* ptr = &x;
killYourPointer(ptr);
printf("%d",*ptr);
return 0;
}
Explanation / Answer
capitalize(&c);
#include<stdio.h>;
void swap(int *a, int *b){
int t = *a;
*a = *b;
*b = t;
}
int main(void){
int a = 3;
int b = 4;
swap(&a,&b);
printf("a value is %d and b value is %d", a, b);
return 0;
}
Nothing will happen because you are sending the ptr by call by value meaning changes will be local to functions not to the actual ptr.hence code will print 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.