Initial commit

This commit is contained in:
2023-05-22 23:28:51 -03:00
commit 5c1403aa91
467 changed files with 18649 additions and 0 deletions

BIN
Labs/Lab4/Lab4.pdf Normal file

Binary file not shown.

Binary file not shown.

BIN
Labs/Lab4/Lec10src.zip Normal file

Binary file not shown.

View File

@ -0,0 +1,19 @@
#ifndef STRINGS_H
#define STRINGS_H
// a cover function for malloc()
// malloc and return memory for a string of stringsize characters
// return (char*)NULL on failure
char* mallocString(int stringsize);
// just a cover function for free()
void freeString(char* s);
// create a duplicate string of s
// return it
// return (char*)NULL on failure
// should call mallocString(), and then strcpy()
char* duplicateString(char* s);
#endif

View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]){
char** stringList;
// allocate an array of pointers to strings (each string is a char*)
stringList = (char**) malloc(sizeof(char*) * argc);
if(stringList == (char**)NULL){
fprintf(stderr,"Memory failure, terminating");
return EXIT_FAILURE;
}
for(int i=0; i<argc; i++){
// allocate the string
stringList[i] = (char*) malloc( sizeof(char)* (strlen(argv[i]) +1) );
if(stringList[i] == (char*)NULL){
fprintf(stderr,"Memory failure, terminating");
for(int j=0; j<i; i++){
free(stringList[j]);
}
free(stringList);
return EXIT_FAILURE;
}
// copy commandline arguement
strcpy(stringList[i], argv[i]);
}
// Did we do it? Can we print them out?
for(int i=0; i<argc; i++){
printf("%s\n", stringList[i]);
}
// Cleaning up after ourselves, from the end to the beginning
for(int i=0; i<argc; i++){
free(stringList[i]);
}
free(stringList);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#define MAXVALS 4
void arrayVals(int iN, int* arr, int val);
int main(void){
int iErr;
int iVal;
int a[MAXVALS];
char* prompt = "value: ";
printf("%s",prompt);
iErr = scanf("%d", &iVal);
if(iErr != 1){
fprintf(stderr, "Unable to read value\n");
return EXIT_FAILURE;
}
arrayVals(MAXVALS, a, iVal);
printf("-------------------------------------------\n");
printf("main prompt (char*):\t%p\n", &prompt);
printf("main a (int[MAXVALS]):\t%p\n", a);
printf("main iVal (int):\t%p\n", &iVal);
printf("main iErr (int):\t%p\n", &iErr);
for(int i=0; i<MAXVALS; i++)
printf("\t%d", a[i]);
printf("\n");
return EXIT_SUCCESS;
}
void arrayVals(int iN, int* arr, int val)
{
printf("arrayVals arr (int*):\t%p\n",&arr);
printf("arrayVals val (int):\t%p\n",&val);
printf("arrayVals iN (int):\t%p\n",&iN);
for(int i=0; i<iN; i++)
arr[i]= val;
}
/*
value: 3
arrayVals arr (int*): 0x7ffc99b277d0
arrayVals val (int): 0x7ffc99b277d8
arrayVals iN (int): 0x7ffc99b277dc
-------------------------------------------
main prompt (char*): 0x7ffc99b27808
main a (int[MAXVALS]): 0x7ffc99b27810
main iVal (int): 0x7ffc99b27824
main iErr (int): 0x7ffc99b27828
3 3 3 3
*/

View File

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#define MAXVALS 4
int main(void){
// allocate space for a float on the heap,
// return a pointer to it,
// cast it to a float* ,
// store it in f;
float* pf = (float*) malloc( sizeof(float) );
if(pf == (float*)NULL){
fprintf(stderr, "Memory allocation failed. Program terminating.");
return EXIT_FAILURE;
}
printf("Please enter a float value: ");
scanf("%f",pf); // pf is a pointer, with space allocated to it
printf("%f it is!\n", *pf);
free(pf); // all done with fp
// allocate space for an integer [] that hold MAXVALS ints on the heap,
// return a pointer to it, cast the void* to a int* , and store it in arr;
int iErr;
int* arr = (int*) malloc( sizeof(int)*MAXVALS );
if(arr == (int*)NULL){
fprintf(stderr, "Memory allocation failed. Program terminating.");
return EXIT_FAILURE;
}
printf("Please enter %d integer values: ", MAXVALS);
for(int i=0; i<MAXVALS; i++){
iErr = scanf("%d",&arr[i]); // arr is a pointer, with space allocated
if(iErr != 1){
fprintf(stderr, "Read failed. Program terminating.");
free(arr);
return EXIT_FAILURE;
}
printf("%d it is!\n", arr[i]);
}
free(arr); //all done with arr
// allocate space for an char [] of that hold MAXVALS chars on the heap
// (with room for the NULL), return a pointer to it, cast it to a char* ,
// and store it in s.
// Note that although s isn't YET a string, that's the intention.
char* s = (char*) malloc( sizeof(char)*(MAXVALS+1) );
if(pf == (float*)NULL){
fprintf(stderr, "Memory allocation failed. Program terminating.");
return EXIT_FAILURE;
}
printf("Enter up to four characters: ");
getchar(); // flushes newline feed from last response
char c = getchar();
int i = 0;
while(c != '\n' && i < MAXVALS){ // leave room for the terminating NULL!
s[i] = c;
i++;
c = getchar();
}
s[i] = (char) NULL; // terminate the string
printf("%s it is!\n", s);
free(s); // all done with s
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include "Strings.h"
// copies the program name (argv[0]) to a new string, prints it out
int main(int argc, char* argv[]){
char* programName;
programName = duplicateString(argv[0]);
if(programName == (char*)NULL){
fprintf(stderr,"Memory failure, terminating");
return EXIT_FAILURE;
}
// Did we do it? Can we print it out?
printf("%s\n", programName);
free(programName);
return EXIT_SUCCESS;
}