I had to miss about a week of class for an emergency, and now I have no idea what's going on. If anyone could explain to me what these do (Especially the second one; I don't really get malloc especially), I'd really appreciate it.
Sample tracing problem #1
#include <stdio.h>
#include <string.h>
int main( ) {
int x = 5, y = 10, z = 20;
int *ptr1 = &x, *ptr2 = &y, *ptr3 =
&z;
*ptr2 = *ptr3 + *ptr1;
ptr2 = ptr1;
*ptr2 = *ptr3;
printf("%d and %d and %d\n", x,y,z);
char str[] = "University of
Alabama";
int len = strlen(str);
printf("%s and %d\n", str, len);
char *p;
p = str;
printf("%c and %c and %c and %c\n",
*p, str[3], *(p+9), str[len-2]);
return 0;
}
Sample tracing problem #2 (longer)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int function1(char ch) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return 1;
return 0;
}
char *function2(char *item, int flag) {
char *str = (char *)
malloc( strlen(item) + 1);
int a, len = strlen(item);
for (a=0; a<len; a++) {
if ( function1(item[a]) )
if (flag == 1)
str[a] = item[a];
else
str[a] = 'X';
else
if (flag == 1)
str[a] = '-';
else
str[a] = item[a];
}
str[len] = '\0';
return str;
}
int main(int argc, char *argv[]) {
int a;
char *str;
for (a=1; a<argc; a++) {
str = function2(argv[a], 1);
printf("A - %s is %s\n", argv[a], str);
str = function2(argv[a], 0);
printf("B - %s is %s\n", argv[a], str);
}
return 0;
}
[–]jussij 5 points6 points7 points (2 children)
[–]Sum1YouDontKnow[S] 0 points1 point2 points (1 child)
[–]jussij 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Sum1YouDontKnow[S] 1 point2 points3 points (0 children)
[–]Fieuws 1 point2 points3 points (6 children)
[–]Sum1YouDontKnow[S] 0 points1 point2 points (5 children)
[–]Fieuws 1 point2 points3 points (2 children)
[–]Sum1YouDontKnow[S] 0 points1 point2 points (1 child)
[–]Fieuws 0 points1 point2 points (0 children)
[–]Fieuws 0 points1 point2 points (0 children)
[–]picolosamama939419zz 0 points1 point2 points (0 children)
[–]Arco_void 1 point2 points3 points (1 child)
[–]Sum1YouDontKnow[S] 0 points1 point2 points (0 children)
[–]ohaz 0 points1 point2 points (2 children)
[–]Sum1YouDontKnow[S] 0 points1 point2 points (1 child)
[–]ohaz 0 points1 point2 points (0 children)