Created
February 5, 2019 17:54
-
-
Save cegme/42d120ac1fd037b8fa9731bc544b7f63 to your computer and use it in GitHub Desktop.
Class notes from 2/5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
struct _book { | |
int bookid : 3; | |
}; | |
typedef struct _book book; | |
union _magazine { | |
int id; | |
float value; | |
char title[20]; | |
}; | |
typedef union _magazine magazine; | |
int intcmp(const void *x, const void *y){ | |
return -(*(int*) x - *(int*)y); | |
} | |
int main(int argc, char *argv[]) { | |
printf("Book size: %ld\n", sizeof(book)); | |
book textbook; | |
textbook.bookid = 8; // 1000 | |
printf("Textbook.id: %d\n", textbook.bookid); | |
textbook.bookid = 2; // 10 | |
printf("Textbook.id: %d\n", textbook.bookid); | |
textbook.bookid = 7; // 111 | |
printf("Textbook.id: %d\n", textbook.bookid); | |
textbook.bookid = 7; // 111 | |
printf("Textbook.id: %ud\n", (unsigned) textbook.bookid); | |
// -------------- | |
/* | |
* 0x 01234567 | |
* Big endian | |
* [01] 0x100 | |
* [23] 0x101 | |
* [45] 0x102 | |
* [67] 0x013 | |
* | |
* Little endian | |
* [67] 0x100 | |
* [45] 0x101 | |
* [23] 0x102 | |
* [01] 0x013 | |
* | |
*/ | |
// --------------- | |
// | |
printf("Union size: %ld\n", sizeof(magazine)); | |
magazine time; | |
time.id = 1234; | |
printf("id: %d, value: %f, title: '%s'\n", time.id, time.value, time.title); | |
time.value = 19.99; | |
printf("id: %d, value: %f, title: '%s'\n", time.id, time.value, time.title); | |
strcpy(time.title, "Person of the year"); | |
printf("id: %d, value: %f, title: '%s'\n", time.id, time.value, time.title); | |
printf("%p, %p \n", &time.id, &time.value); | |
//------------------------------ | |
int array[] = {10,23,256,5,32}; | |
qsort(array, 5, sizeof(int), intcmp); | |
for(int i =0; i < 5; ++i) { | |
printf("=> %d\n", array[i]); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Class activity solution | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct _course | |
{ | |
int student_id; | |
int marks; | |
char subject[20]; | |
//char *subject; | |
}; | |
typedef struct _course course; | |
int main() | |
{ | |
// Task1: Create a pointer variable ptr for the structure course | |
course * ptr; | |
int i, no_of_records; | |
printf("Enter number of records: "); | |
scanf("%d", &no_of_records); | |
// TODID Task 2: Allocate the memory for no_of_records structures with pointer ptr pointing to the base address. | |
ptr = calloc(no_of_records, sizeof(course)); | |
// Iterate through the no_of_records to get information from the user for each record | |
for(i = 0; i < no_of_records; ++i) { | |
printf("Enter the student id,name of the subject and marks respectively:\n"); | |
//TODID Task 3: Get the info id, name of the subject followed by the student mark | |
/*course tempcourse; | |
scanf("%d %s %d", &tempcourse.student_id, tempcourse.subject, &tempcourse.marks ); | |
ptr[i].student_id = tempcourse.student_id; | |
ptr[i].marks = tempcourse.marks; | |
strcpy(ptr[i].subject, tempcourse.subject);*/ | |
//scanf("%d %s %d", &ptr[i].student_id, ptr[i].subject, &ptr[i].marks ); | |
course tempcourse; | |
scanf("%d %s %d", &tempcourse.student_id, tempcourse.subject, &tempcourse.marks ); | |
// ptr[i] = tempcourse; // changing what ptr[] is pointing to BAD | |
memcpy(&ptr[i], &tempcourse, sizeof(course)); // Copy into the allocated memory | |
} | |
// If stuff happened here we would get strange errors | |
printf("Displaying Student Information:\n"); | |
for(i = 0; i < no_of_records ; ++i) { | |
printf("Student ID: %d\n",(ptr+i)->student_id); | |
printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks); | |
} | |
free(ptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment