I'm trying to create two lists, pros and cons and then print them.
But I can't figure out what am I doing wrong.
I tried to debug the program with gdb online and I found out that the error is in function fgets().
#include <stdio.h>
#include <string.h>
typedef struct list{
char ** reason;
} list;
void printMenu();
void printList(list * myList, int len1);
int main(void)
{
int keepGoing = 0;
int choice = 0;
int i = 0;
int j = 0;
list * pros;
list * cons;
while (!keepGoing){
printMenu();
scanf("%d", &choice);
pros = (list*)malloc(sizeof(list));
cons = (list*)malloc(sizeof(list));
switch (choice){
case 1:
i++;
printf("Enter a reason to add to list PRO: ");
pros = (list*)realloc(pros, i*sizeof(list));
fgets(pros->reason[i], 50, stdin);
pros->reason[strcspn(pros->reason[i], "\n")] = 0;
break;
case 2:
j++;
cons = (list*)realloc(cons->reason, j*sizeof(list));
printf("Enter a reason to add to list CON: ");
fgets(cons->reason[j], 50, stdin);
cons->reason[strcspn(cons->reason[j], "\n")] = 0;
break;
case 3:
printf("PROS:\n");
printList(pros, i);
printf("CONS:\n");
printList(cons, j);
break;
case 4:
keepGoing = 1;
break;
default:
printf("Invalid value.");
keepGoing = 1;
}
}
free(pros);
free(cons);
getchar();
return 0;
}
void printList(list * reasons, int len1){
int i = 0;
for (i = 0; i < len1; i++){
printf("%s\n", reasons->reason[i]);
}
}
void printMenu(){
printf("Choose option:\n");
printf("1 - Add PRO reason\n");
printf("2 - Add CON reason\n");
printf("3 - Print reasons\n");
printf("4 - Exit\n");
}
There is no need to allocate these dynamically: list * pros; list * cons;. Code like pros = (list*)realloc(pros, i*sizeof(list)); doesn't make any sense.
Instead, declare them as plain variables. list pros.
What you instead need to allocate dynamically is the member pros.reason. You need to allocate an array of pointers that it points to, and then you need to allocate the individual arrays.
You have a problem in
fgets(pros->reason[i], 50, stdin);
as the memory you want to use is not valid. pros->reason does not point to a valid memory, so you cannot dereference it, this causes undefined behavior.
Before you can index-into pros->reason, you need to make pros->reason point to a valid memory location.
After that, you need to make pros->reason[i]s also to point to valid memory if you want them to be used as the destination of fgets().
Apart from this issue, you have another issue which makes this code nonsense, that is calling malloc() on every iteration of the loop. You need to call malloc() only once, to get a pointer (to memory) allocated by memory allocator function and then, use realloc() inside the loop to adjust that to the required memory.
There are many issues. Previous comments and answers do still apply.
Here is a clean solution.
The list structure is now self contained, no need to track the number of strings in separate variables
added selfcontained AddString function
no more unnecessary mallocs
all allocated memory is freed correctly
some logic errors removed (inverted logic of keepGoing)
There is still room for improvement. Especially there is no error checking for the memory allocation functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list {
int size; // number of strings
int chunksize; // current of chunk
char ** reason;
} list;
void printMenu();
void printList(list * reasons);
void freeList(list * l);
void AddString(list *l, const char *string);
int main(void)
{
int keepGoing = 1;
int choice = 0;
list pros = { 0 }; // = {0} initializes all fields to 0
list cons = { 0 };
while (keepGoing) {
printMenu();
scanf("%d", &choice);
char input[50];
fgets(input, sizeof(input), stdin); // absorb \n from scanf
switch (choice) {
case 1:
printf("Enter a reason to add to list PRO: ");
fgets(input, sizeof(input), stdin);
AddString(&pros, input); // Add string to pros
break;
case 2:
printf("Enter a reason to add to list CONS: ");
fgets(input, sizeof(input), stdin);
AddString(&cons, input); // Add string to cons
break;
case 3:
printf("PROS:\n");
printList(&pros);
printf("CONS:\n");
printList(&cons);
break;
case 4:
keepGoing = 0;
break;
default:
printf("Invalid value.");
keepGoing = 1;
}
}
freeList(&pros);
freeList(&cons);
getchar();
return 0;
}
#define CHUNKSIZE 10
void AddString(list *l, const char *string)
{
if (l->size == l->chunksize)
{
// resize the reason pointer every CHUNKSIZE entries
l->chunksize = (l->chunksize + CHUNKSIZE);
// Initially l->reason is NULL and it's OK to realloc a NULL pointer
l->reason = realloc(l->reason, sizeof(char**) * l->chunksize);
}
// allocate memory for string (+1 for NUL terminator)
l->reason[l->size] = malloc(strlen(string) + 1);
// copy the string to newly allocated memory
strcpy(l->reason[l->size], string);
// increase number of strings
l->size++;
}
void freeList(list * l) {
for (int i = 0; i < l->size; i++) {
// free string
free(l->reason[i]);
}
// free the list of pointers
free(l->reason);
}
void printList(list * l) {
for (int i = 0; i < l->size; i++) {
printf("%s\n", l->reason[i]);
}
}
void printMenu() {
printf("Choose option:\n");
printf("1 - Add PRO reason\n");
printf("2 - Add CON reason\n");
printf("3 - Print reasons\n");
printf("4 - Exit\n");
}
Related
I prefer to create a Dictionary object and add 3 words to it.
My program has no compilation error but gets a run time error in the second for loop, is the problem in addNewWord function? Do I need pass a pointer to the DictionaryWord object ?
Please help me.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char* name;
char* mean;
} Words;
typedef struct{
Words* word;
int size;
} Dictionary;
Dictionary createNewDictionary();
Words createNewWord();
void addNewWord(Words newword, Dictionary dic);
Dictionary createNewDictionary(){
Dictionary dic;
dic.size = 0;
dic.word = (Words*)malloc(dic.size*sizeof(Words));
return dic;
}
Words createNewWord(){
Words newword;
newword.name = (char*)malloc(30*sizeof(char));
newword.mean = (char*)malloc(30*sizeof(char));
printf("============================\n");
printf("Enter word: ");
scanf("%[^\n]", newword.name);
fflush(stdin);
printf("\nEnter meaning: ");
scanf("%[^\n]", newword.mean);
return newword;
}
void addNewWord(Words newword, Dictionary dic){
dic.size++;
dic.word = (Words*)realloc(dic.word,dic.size*sizeof(Words));
strcpy(dic.word[dic.size-1].name, newword.name);
strcpy(dic.word[dic.size-1].mean, newword.mean);
}
int main(){
Dictionary d = createNewDictionary();
for (int i=0;i<3;i++){
addNewWord(createNewWord(), d);
}
return 0;
}
There are lots of problem with your code:
Given the longest word in English is around 30 characters, this size allocation is realistic for the word, but not for the defintion:
newword.name = (char*)malloc(30*sizeof(char));
newword.mean = (char*)malloc(30*sizeof(char));
This makes little obvious sense:
dic.size = 0;
dic.word = (Words*)malloc(dic.size*sizeof(Words));
you called malloc() on zero! You're only spared by your later realloc(). Even if intentional, it really deserves a comment.
This doesn't really work as fflush() is for output streams:
fflush(stdin);
see: How to clear input buffer in C? And whatever fix you use has to apply to both scanf() calls, not just one!
Per #Jarvis, this doesn't work:
dic.word = (Words*)realloc(dic.word,dic.size*sizeof(Words));
strcpy(dic.word[dic.size-1].name, newword.name);
strcpy(dic.word[dic.size-1].mean, newword.mean);
as you didn't allocate any space for name and mean in dic so you're copying into random memory.
Per #Jarvis, doesn't work:
void addNewWord(Words newword, Dictionary dic){
dic.size++;
dic.word = (Words*)realloc(dic.word,dic.size*sizeof(Words));
You're passing dic by value so inside addnewWord() you've a copy of dic so the original dic's size will be the same as it was before the call!
Memory leak:
addNewWord(createNewWord(), d);
you dropped your handle onto what createNewWord() returned so you can never free the memory it malloc()'d
You malloc() memory but provide no means to eventually free it.
Passing and returning structs by value is a disaster in a situation like this, as the data keeps getting copied. At the least it's inefficient, at worst its buggy like the size issue above. Rather than risk it, pretend they can only be passed and returned by pointer and you'll be playing it safe and get a better result.
Below is a rework of your code (in C) with fixes, style tweaks and an attempt at a consistent terminology. It also provides some minimal test code and the ability to free your data:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LENGTH 30
#define MAX_DEFINITION_LENGTH 1024
typedef struct entry {
char *word;
char *definition;
} Entry;
typedef struct dictionary {
Entry *entries;
int num_entries, max_entries;
} Dictionary;
Dictionary *createNewDictionary() {
Dictionary *dictionary = malloc(sizeof(*dictionary));
dictionary->num_entries = 0;
dictionary->max_entries = 1;
dictionary->entries = calloc(dictionary->max_entries, sizeof(*dictionary->entries));
return dictionary;
}
void freeEntry(Entry *entry) {
free(entry->word);
free(entry->definition);
free(entry);
}
void freeDictionary(Dictionary *dictionary) {
for (--dictionary->num_entries; dictionary->num_entries >= 0; --dictionary->num_entries) {
// we can't call freeWord() here -- why.
free(dictionary->entries[dictionary->num_entries].word);
free(dictionary->entries[dictionary->num_entries].definition);
}
free(dictionary->entries);
free(dictionary);
}
void purgeInput() {
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
}
Entry *requestNewEntry() {
Entry *entry = malloc(sizeof(*entry));
entry->word = malloc(MAX_WORD_LENGTH);
entry->definition = malloc(MAX_DEFINITION_LENGTH);
printf("============================\n");
printf("Enter word: ");
scanf("%[^\n]", entry->word);
purgeInput();
printf("\nEnter definition: ");
scanf("%[^\n]", entry->definition);
purgeInput();
return entry;
}
void addNewEntry(Entry *entry, Dictionary *dictionary) {
if (dictionary->num_entries == dictionary->max_entries) {
dictionary->max_entries *= 2;
dictionary->entries = realloc(dictionary->entries, dictionary->max_entries * sizeof(*dictionary->entries));
// check if realloc returns NULL and if so, handle the error.
}
dictionary->entries[dictionary->num_entries].word = strdup(entry->word);
dictionary->entries[dictionary->num_entries].definition = strdup(entry->definition);
dictionary->num_entries++;
}
int main() {
Dictionary *d = createNewDictionary();
for (int i = 0; i < 3; i++) {
Entry *e = requestNewEntry();
addNewEntry(e, d);
freeEntry(e);
}
printf("\nRead: ");
for (int i = 0; i < d->num_entries; i++) {
printf("%s (%lu chars) ", d->entries[i].word, strlen(d->entries[i].definition));
}
printf("\n");
freeDictionary(d);
return 0;
}
CREATING A PUN DICTIONARY
> ./a.out
============================
Enter word: silkworm
Enter definition: Two silkworms had a race but ended up in a tie.
============================
Enter word: horse
Enter definition: A horse is a stable animal.
============================
Enter word: termite
Enter definition: A termite walks into a pub and asks, "Is the bar tender here?"
Read: silkworm (47 chars) horse (27 chars) termite (62 chars)
>
I see what's wrong with your code. First of all, you need to pass your Dictionary object by pointer to the function, addNewWord, and in the function addNewWord, you again need to allocate memory to each of the char* fields, name and mean, of the dic object. Here is the corrected code :
void addNewWord(Words newword, Dictionary *dic){
dic->size++;
dic->word = (Words*)realloc(dic->word, dic->size*sizeof(Words));
dic->word[dic->size-1].name = (char*)malloc(30*sizeof(char)); //added
dic->word[dic->size-1].mean = (char*)malloc(30*sizeof(char)); //added
strcpy(dic->word[dic->size-1].name, newword.name);
strcpy(dic->word[dic->size-1].mean, newword.mean);
}
Pass the dictionary's address as :
addNewWord(createNewWord(), &d);
and change the definition as well as prototype of the function as well :
void addNewWord(Words newword, Dictionary *dic)
Find the complete code here : http://pastebin.com/ZN69hevj
My task is to initialize a struct in a header with some data. Then using pointers, add/remove data by malloc-ing bigger/smaller chunks and copying the data over.
Currently, my addRecord function doesn't work as it always seems to add the same crap (Part of record number 1):
Name =
Fire Number = attan (Seems to be part of Manhattan)
Street = ork (Seems to be part of New York)
City = cret (Seems to be part of Secret)
State = tan (Seems to be part of Manhattan)
What am I doing wrong?
My header:
#include <stdio.h>
#include <stdlib.h>
struct structPointer{
char name[51];
char fireNumber[11];
char street[26];
char city[26];
char state[26];
};
My c file:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "myData.h"
struct structInit *sP;
struct structInit *sSP;
int recordNumber = 0;
int numberOfAccesses = 0;
int main(void) {
sP = (struct structInit *) malloc(5 * sizeof(struct structInit));
sSP = sP;
memcpy(sP->name,"Adam Baum",51);
memcpy(sP->fireNumber,"N1234",11);
memcpy(sP->street,"Top Secret",26);
memcpy(sP->city,"Manhattan",26);
memcpy(sP->state,"New York",26);
sP++;
recordNumber++;
memcpy(sP->name,"Adam Zapel",51);
memcpy(sP->fireNumber,"S4321",11);
memcpy(sP->street,"Throat",26);
memcpy(sP->city,"Manhattan",26);
memcpy(sP->state,"New York",26);
sP++;
recordNumber++;
memcpy(sP->name,"Al Bino",51);
memcpy(sP->fireNumber,"W1234",11);
memcpy(sP->street,"White",26);
memcpy(sP->city,"Anchorage",26);
memcpy(sP->state,"Alaska",26);
sP++;
recordNumber++;
memcpy(sP->name,"Anne Teak",51);
memcpy(sP->fireNumber,"E4321",11);
memcpy(sP->street,"Oak",26);
memcpy(sP->city,"Woodville",26);
memcpy(sP->state,"Wisconsin",26);
sP++;
recordNumber++;
memcpy(sP->name,"Barb Dwyer",51);
memcpy(sP->fireNumber,"N1234",11);
memcpy(sP->street,"Keepout",26);
memcpy(sP->city,"Kilgore",26);
memcpy(sP->state,"Texas",26);
recordNumber++;
sP = sSP;
int sel;
while (1){
printf("MENU\n");
printf("=====\n");
printf("1. Print All Records\n");
printf("2. Print Number of Records\n");
printf("3. Print Size of Database\n");
printf("4. Add Record\n");
printf("5. Delete Record\n");
printf("6. Print Number of Accesses to Database\n");
printf("7. Exit\n");
printf("=====\n");
printf("Enter selection: ");
scanf("%d", &sel);
printf("\n");
switch(sel){
case 1:
numberOfAccesses++;
printAllRecords(sP);
break;
case 2:
numberOfAccesses++;
fprintf(stderr,"There are a Total of %d records.\n\n", recordNumber);
break;
case 3:
numberOfAccesses++;
printSizeOfDatabase(sP);
break;
case 4:
numberOfAccesses++;
sP = sSP;
addRecord(sP);
break;
case 5:
numberOfAccesses++;
deleteRecord(sP);
break;
case 6:
numberOfAccesses++;
fprintf(stderr,"The total number of Accesses is %d\n\n", numberOfAccesses);
break;
case 7:
exit(0);
default:
printf("Error: Input was not a valid selection.\n\n");
break;
}
}
return 0;
}
int printAllRecords(struct structInit *structPointer){
int i;
structPointer = sSP;
printf("All Records: \n");
for(i=1;i<=recordNumber;i++){
printf("Record Number: %d\n", i);
fprintf(stderr, "Name = \%s\n", structPointer-> name);
fprintf(stderr, "Fire Number = \%s\n", structPointer-> fireNumber);
fprintf(stderr, "Street = \%s\n", structPointer-> street);
fprintf(stderr, "City = \%s\n", structPointer-> city);
fprintf(stderr, "State = \%s\n\n", structPointer-> state);
structPointer++;
}
return 1;
}
int printSizeOfDatabase(struct structInit *structPointer) {
int size = 0;
int i;
for (i=1;i<=recordNumber;i++) {
size += sizeof(structPointer->name);
size += sizeof(structPointer->fireNumber);
size += sizeof(structPointer->street);
size += sizeof(structPointer->city);
size += sizeof(structPointer->state);
structPointer++;
}
fprintf(stderr, "The size of the database is %d bytes.\n\n", size);
return size;
}
int addRecord(struct structInit *structPointer){
char entryName;
char entryFireNumber;
char entryStreet;
char entryCity;
char entryState;
recordNumber++;
struct structInit *theStruct;
theStruct = (struct structInit *) malloc ((recordNumber+1) * sizeof(struct structInit));
int i;
for (i=1;i<recordNumber;i++){
memcpy(theStruct->name,structPointer->name,51);
memcpy(theStruct->fireNumber,structPointer->fireNumber,11);
memcpy(theStruct->street,structPointer->street,26);
memcpy(theStruct->city,structPointer->city,26);
memcpy(theStruct->state,structPointer->state,26);
/*if(i==recordNumber){
theStruct++;}
else{
theStruct++;
structPointer++;}*/
theStruct++;
structPointer++;
}
theStruct++;
printf("Enter the Name of the New Record: \n");
scanf("%s",&entryName);
memcpy(theStruct->name,&entryName,51);
printf("Enter the Fire Number of the New Record: \n");
scanf("%s",&entryFireNumber);
memcpy(theStruct->fireNumber,&entryFireNumber,11);
printf("Enter the Street of the New Record: \n");
scanf("%s",&entryStreet);
memcpy(theStruct->street,&entryStreet,26);
printf("Enter the City of the New Record: \n");
scanf("%s",&entryCity);
memcpy(theStruct->city,&entryCity,26);
printf("Enter the State of the New Record: \n");
scanf("%s",&entryState);
memcpy(theStruct->state,&entryState,26);
structPointer=theStruct;
printf("Record has been added.\n\n");
return 0;
}
int deleteRecord(struct structInit *structPointer){
struct structInit *anotherStruct;
anotherStruct = (struct structInit *) malloc ((recordNumber+1) * sizeof(struct structInit));
int i;
for(i=0;i<5;i++){
memcpy(anotherStruct->name,structPointer->name,51);
memcpy(anotherStruct->fireNumber,structPointer->fireNumber,11);
memcpy(anotherStruct->street,structPointer->street,26);
memcpy(anotherStruct->city,structPointer->city,26);
memcpy(anotherStruct->state,structPointer->state,26);
structPointer++;
}
structPointer=anotherStruct;
recordNumber--;
printf("Record has been deleted.\n\n");
return 0;
}
At a glance:
The %s format specifier for scanf specifies a pointer to the first element of an array of char.
You're passing a pointer to one char.
It's just bad luck that the program doesn't crash completely.
You want
char entryName[51];
/* ... */
printf("Enter the Name of the New Record: \n");
scanf("%s", entryName);
and similar for the rest of the inputs.
Replace memcpy with strncpy - you shouldn't copy anything from outside the source object.
If you use realloc you can get rid of the entire copying loop when you expand your table.
Assigning to a parameter doesn't modify the value of the variable whose value you passed in.
This is as true for pointers as it is for everything else.
If you want to modify a variable, pass a pointer to it:
int addRecord(struct structInit **structPointer)
{
/* ... */
*structPointer = theStruct;
/* ... */
}
You don't need to do a memberwise memcpy between structs - in fact you don't need memcpy at all, since assignment works:
struct structInit a = /* ... */;
struct structInit b = /* ... */;
a = b; /* OK */
You need to make up your mind about whether to use global variables or parameters.
In particular, printAllRecords disregards the value of its parameter completely.
deleteRecord assumes that there are five records in your table.
You can also replace printSizeOfDatabase with this:
int printSizeOfDatabase() {
int size = recordNumber * sizeof(struct structInit);
fprintf(stderr, "The size of the database is %d bytes.\n\n", size);
return size;
}
You could use realloc in the add function.
A typedef would make the program a lot easier to read.
Please add this line to the end of mydata.h
typedef struct structPointer structInit;
Then before main() it becomes
structInit *sP;
structInit *sSP;
Then printAllRecords become :
int printAllRecords(structInit *structPointer)
Then turn ON your debugger and READ the messages.
I have to create a program which adds records to a simple phone book. The code is below, but it doesn't work - function ends and then it stucks on declaring struct record x and doesn't want to display my added record - the program breaks down. When I put this part of code on the end of the function (but instead of "struct record x = array[0];" I put "struct record x = (*array)[0]") it works - record is printed. So I guess the problem is something about pointers, but I'm struggling and I really couldn't find out what's wrong. I remember that few weeks ago I created a program which was very similar but it was adding a new record to an array of integers, with fixed values and it was working well, so maybe there's something with structures that I don't know about. Thanks for any help!
I know the program isn't done yet and I know that I didn't make any action for temp_array == NULL, it'll be done after I found out what's going on.
struct record {
char f_name[SIZE];
char name[SIZE];
long int phone;
};
int add_record(struct record** array, int n)
{
struct record* temp_array = malloc((n+1) * sizeof(struct record));
if (temp_array == NULL)
{
free(temp_array);
return -1;
}
int i;
for (i=0; i < n; i++)
{
temp_array[i] = (*array)[i];
}
struct record new_record;
printf("\nAplly data.");
printf("\nFirst name: "); /*fgets(new_record.f_name, SIZE, stdin);*/ scanf("%s", &new_record.f_name);
printf("Surname: "); /*fgets(new_record.name, SIZE, stdin);*/ scanf("%s", &new_record.name);
printf("Phone number: "); scanf("%d", &new_record.phone);
temp_array[n] = new_record;
free (*array);
*array = temp_array;
//struct record x = (*array)[0];
//puts(x.f_name); puts(x.name); printf("%d", x.phone);
return 0;
}
main()
{
struct record* array; int n = 0;
int choice;
printf("\n1. Add record\n2. Delete record\n3. Find record\n0. Exit\n\nChoose action: ");
scanf("%d", &choice);
switch(choice) {
case 0: printf("\nKsiazka zostala zamknieta.\n"); return;
case 1: add_record(&array, n); n++; break;
case 2: return;
case 3: return;
default: printf("Wrong choice.\n\n"); return;
}
struct record x = array[0];
puts(x.f_name); puts(x.name); printf("%d", x.phone);
}
struct record* array=NULL;, and use %ld for long int – BLUEPIXY
Program specifications:
Read questions from a data file in the following format:
Question
Number of choices
N-amount of choices
Correct answer
Example:
What is the capital of France?
3
Madrid
Sydney
Paris
Paris
Present the user a question at a time and keep track of their score, etc, etc.
What I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LINE_SIZE 60
#define MAX_LIST_SIZE 15
#define MAX_QUIZ_SIZE 10
typedef struct question {
char *question;
char **choices;
int n_choices;
char *correct_answer;
} QUESTION;
typedef struct quiz {
struct question *questions;
int n_questions;
} QUIZ;
char *dupString(const char *s) {
// copies a string
char *dup = malloc(strlen(s) + 1);
strcpy(dup, s);
return dup;
}
void free_choices(QUESTION *q) {
// free memory
for(int i = 0; i < q->n_choices; i++) {
free(q->choices[i]);
}
free(q->choices);
}
int ask(QUESTION *q) {
// Return 1 for correct guess, 0 for incorrect guess.
int choice;
// Print the question
printf("\n%s\n", q->question);
// Print the choices
for (int i = 0; i <= q->n_choices-1; i++) {
printf("%d : %s", i+1, q->choices[i]);
}
// Get user guess
do {
printf("Select an answer [1-%d]: ", q->n_choices);
scanf("%d", &choice);
/* Not sure how to structure here*/
if (strcmp(q->choices[choice-1], q->correct_answer) == 0) {
// if correct return 1
return 1;
}
} while (choice < 1 || choice > q->n_choices);
// Incorrect
return 0;
}
struct question parseQuestion(FILE *pData) {
int qIndex, numChoices;
char question[MAX_LINE_SIZE], temp[MAX_LINE_SIZE], choices[MAX_LINE_SIZE], correctAns[MAX_LINE_SIZE];
QUESTION q = {NULL, NULL, 0, NULL};
// Eat first line = QUESTION
fgets(question, MAX_LINE_SIZE, pData);
q.question = question;
// Eat second line = NUMBER OF CHOICES
fgets(temp, MAX_LINE_SIZE, pData);
numChoices = atoi(temp);
q.n_choices = numChoices;
// Allocate memory
q.choices = calloc(q.n_choices, sizeof(char*));
// Eat nth lines = CHOICES
for (qIndex=0; qIndex<=numChoices-1; qIndex++) {
fgets(choices, MAX_LINE_SIZE, pData);
q.choices[qIndex] = dupString(choices);
}
// Eat nth + 1 line = CORRECT ANSWER
fgets(correctAns, MAX_LINE_SIZE, pData);
q.correct_answer = correctAns;
return q;
}
int main() {
int num = 0; // question being asked
int strikes = 0; // incorrect guesses
FILE* pData;
char *filename = "tickle.txt";
char c;
if ((pData = fopen(filename, "r"))) {
printf("Welcome to the 2014 Quiz-festival!\n\n");
printf("Are you ready to begin? [Y/y]\n");
c = getchar();
if (c == 'Y' || c == 'y') {
QUESTION question = parseQuestion(pData);
ask(&question);
free_choices(&question);
} else {
printf("Come back again.\n");
return 0;
}
} else {
printf("File failed to open.");
}
fclose(pData);
return 0;
}
Thank you to #alk how picked up my error, that is resolved.
What I still can't get is how to loop through the data file and populate the quiz structure with question structures.
So this is where I'm struggling at the moment. From what I can tell I'm pretty close to finishing this little program as long as I can get this to work.
parseQuestion() duplicates the choices but misses to duplicate the question as well as the answer.
Instead it simply copies the two arrays' addresses to the locally defined variable QUESTION q which is copied on return.
The memory for the question and answer strings is free'd on returning from the function, accessing it afterwards invokes undefined behaviuor.
I want to pass the contents of an array to another method and have that method print out the entire array - how would i do this?
Currently:
I'm returning an array from a function.
char* search_value(struct PDB *llist)
{
int realID = -7;
int x = 0;
int task = 0;
char *received;
char theMessage[100];
theMessage[0] = '\0';
printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &task);
while(llist->data1 != NULL)
{
if(task == llist->taskID)
{
realID = llist->taskID;
strcpy(theMessage, llist->data1);
break;
}
}
return theMessage;
}
i'm getting the return value:
void getMessage(const int GET_MESSAGE)
{
char * received = NULL;
int x = 0;
received = search_value(llist);
printf("%s", received);
}
I want to somehow print the entire value (rather than just the first value to which the pointer is pointing at - how would i do this?
A few corrections and it should work:
// - struct contents shouldn't be changed by the function, make its pointer const.
// - pass a pointer to an allocated array as parameter
char* search_value(const struct PDB *llist, char* theMessage)
{
int realID = -7;
int x = 0;
int task = 0;
char *received;
theMessage[0] = '\0';
printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &task);
while(llist->data1 != NULL)
{
if(task == llist->taskID)
{
realID = llist->taskID;
strcpy(theMessage, llist->data1);
break;
}
}
return theMessage;
}
void getMessage(const int GET_MESSAGE)
{
char received[100]; // allocate the array outside the function
int x = 0;
search_value(llist, received); // pass a pointer to the first element
printf("%s", received);
}
You have an issue with variable scope here: theMessage is local to the function search_value, so you're returning a pointer to an array which no longer exists once the function completes.
Instead you should use malloc() to allocate the space for theMessage and then subsequently free() it later on outside of the function when you're finished with it — however this can often lead to memory leaks if you're not diligent about cleaning up after yourself.
You can allocate the memory like so:
char * message = malloc(100);
One alternative would be to allocate the buffer in getMessage() and pass a pointer to the buffer into search_value which could then write into it:
void getMessage(const int GET_MESSAGE)
{
char received[100];
int x = 0;
search_value(llist, received);
printf("%s", received);
}
void search_value(struct PDB *llist, char * buffer)
{
// write to buffer
}
Another option is to declare a char * pointer inside getMessage(), pass a pointer to a pointer into search_value() and again use malloc() to allocate space for the buffer.
Finally, this is a minor style criticism, but you'd do well to learn to stick to one convention for naming your functions, search_value and getMessage are not consistent names, and this will irk many a coder that you work with.
You have several problems with your code. I'm guessing that you want to search a list for some value, then return that value.
The first problem is that you do not actually iterate over the list, but only check the same item over and over again. The other problem is that you return a pointer to a local variable. This is undefined behavior, because as soon as the function returns the memory the pointer points to can be used for something else.
I suggest you change your code as follows:
char *search_value(struct PDB *llist, char *theMessage, size_t theMessageMaxLength)
{
int realID = -7;
int task = 0;
printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &task);
while(llist != NULL && llist->data1 != NULL)
{
if(task == llist->taskID)
{
realID = llist->taskID;
strncpy(theMessage, llist->data1, theMessageMaxLength);
theMessage[theMessageMaxLength] = '\0';
break;
}
llist = llist->next; /* Assuming the field is named "next" */
}
return theMessage;
}
void getMessage(const int GET_MESSAGE)
{
char *received = NULL;
char theMessage[100];
/* Subtract 1 from the size, for the terminating '\0' */
received = search_value(llist, theMessage, sizeof(theMessage) - 1);
printf("%s", received);
}
the array you are returning is local to that function. Either the calle function shall provide the array in which it expects the values or use static array.