Question
This a C programming question regarding pointers. How do I display an array of pointers from a method that's where I am currently stuck. The
This a C programming question regarding pointers. How do I display an array of pointers from a method that's where I am currently stuck. The code works and displays the contents but crashes after they are displayed. However, the other file reading methods do work its just the display one. Can you fix it up and explain how you did it? thanks
#include
#include
#include
#include
#include
// The name of the input file and output file
#define IN_FILE "quotes.txt"
#define OUT_FILE "output.txt"
// The maximum number of quotes we can read from input file
#define MAX_QUOTES 100
// The maximum size of a quote (string) in the IN_FILE file
#define MAX_LEN 1000
// This function will be used in sorting to swap strings ("pointers").
void swap(char**, char**);
// Selection Sort function here.
void selection_sort(char**, size_t);
// Read quotes from file and add them to an array. This functions adjusts the size of quotes!
// Note: second parameter (size_t) should reflect the number of quotes we will read from the file!
void read_in(char**, size_t*);
// Print the quotes using array of pointers.
void print_out(char**, size_t);
// Save the sorted quotes in the OUT_FILE file (size_t here is linked to MAX_QUOTES or actual number of quotes!)
void write_out(char**, size_t);
// Free memory!
void free_memory(char**, size_t);
int main() {
// Create array of pointers. Each pointer should point to heap memory where each quote is kept.
// I.e. quotes[0] points to first quote saved on the heap.
char *quotes[MAX_QUOTES];
//char** quotes = (char**)calloc(MAX_QUOTES, sizeof(char));
// Number of quotes in the file quotes.txt. We assume initially that file has MAX_QUOTES quotes.
// Must be adjusted inside the function read_in when the file is read!
size_t *size=MAX_QUOTES;
// Read quotes from file and place them into array quotes.
// Note: size reflects how many quotes we read from the file (between 0 and MAX_QUOTES).
read_in(quotes, &size);
write_out(quotes, size);
print_out(quotes, size);
free_memory(quotes, size);
/*selection_sort(quotes, size);
*/
return (0);
}
// You need to create functions according to prototypes defined above funciton main()!
void swap (char** p1, char** p2) //how to swap the pointers memory address
{
char* temp = *p1;
*p1 = *p2;
*p2 = temp;
}
//sort
void selection_sort(char**str, size_t size){
}
void read_in(char** array, size_t* size){ //done
char str[MAX_LEN];
char nextLine[5] = " ";
FILE* fp = fopen("quotes.txt", "r+");
if (fp == NULL){ //check if file exsists
perror("Error reading file could not open file");
exit (1);
}
//printf("%s", str);
for (int i = 0; i < MAX_QUOTES; i++){
if (fgets(str, MAX_LEN, fp) != NULL){
if (strcmp(str,nextLine)!= 0){
array[i] = malloc(MAX_LEN * sizeof(char)); //make it a "2d" array
strncpy(array[i], str, strlen(str));
//printf("%s", array[i]);
} else{
array[i] = "delete";
}
}
}
fclose (fp);
}
//print contents
void print_out(char** array, size_t size){
char nextLine[5] = " ";
char str[MAX_LEN];
size_t i = 0;
for (; i < size; i++){
if (strcmp(array[i],nextLine)!= "delete" ){
printf("%zu %s", (i+1),array[i]);
}
}
}
void write_out(char** array, size_t size){ //done
FILE * fp;
int i;
/* open the file for writing*/
fp = fopen ("output.txt","w+");
/* write 10 lines of text into the file stream*/
for(i = 0; i < 10;i++){
//fprintf("Write worked");
fprintf (fp, "%d. %s",i+1,array[i]);
}
/* close the file*/
fclose (fp);
}
void free_memory(char** array, size_t size){
for(int i=0; i { free(array[i]); } } When I run this code it crashes at print_out. I'm not sure why. I'm not allowed to change any parameters or anything in the main even with the type mismatch. I am confused on where to go next.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started