1

I had a question for dictionary.c. First, I'm still confused at why load and check imput is const char*. I understand that const char* means that the value that char* points to will never change and that load and check should not be changing the value. But would it not still not work you make the parameter char* only? Can someone explain what could go wrong/ give an example? My second question is when I am declaring a hashtable, why do I need to declare it like node* hashtable[SIZE]? Wouldnt node hashtable[SIZE] still point to the memory address of the first node because arrays automatically do that?

Also can someone explain what this line means: while(fscanf(fp,"%s\n",word) != EOF). I understand EOF is end of file, and fscanf takes in file to be read, what to read, and where to output what is read. But I'm confused on how fscanf works in this case. First, does it = EOF when fscanf reads /0? Also how does it know when to stop/ why does it only take a word with %s\n (instead of the whole dictionary). What is "word" anyways. Is this saving it into the char* word i declared when i created the struct for a node? Sorry if this is a rudimentary question but if you can provide a thorough explaination on fscanf or provide a helpful video, thatd be appreciated.

Also, one more random question, but for http://www.cplusplus.com/reference/cstring/strcpy/ the example here: char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful");

Why is it declaring an array and setting the whole array equal to a string? That's what char str1[] char str2[40] is right? Arrays and the second on being size 40. Does char str1[] = "Sample string" set each array index a character from Sample String? Why does it do that if yes?

Thanks

1 Answer 1

1

Regarding const

Exact reason you're putting const there is to notify

1) the compiler that you have no intentions to modify that variable. Allowing it to optimize or throw an error just in case you accidentally modify it.

2) you or other programmers that this function won't mess with the argument it's given.

Nothing should go wrong if you take const out. You might have to make sure the header file is also changed accordingly, though.

Regarding fscanf

EOF acts similarly to '\0' in normal strings. However, EOF itself doesn't necessarily have to be a character at the end of the file. In modern file systems, end of file can be recognized by keeping track of file length (they're recorded somewhere else). C library just makes your life easier by making it seems like there is EOF for the function to check. (Source)

fscanf is a function that reads data from file stream and stores them in the given location if the it matches the format given. In this case, word which is hopefully a char array. You're reading just one line each time ("%s\n") so you can store them in your nodes easily.

Regarding strings

Sadly, C doesn't actually have a built-in type called string. They only have char arrays with '\0' to denote end of string for function to manipulate it more easily. (strlen works by reading each character until it hits '\0'.)

In order to save some strings, you have to prepare an array. That's what you just did in char str1[] = "Sample string";. You created an array with elements: 'S', 'a', 'm', ..., 'g', '\0'. (Instead of specifying each character, C language gives you nice quotation mark syntax to make your life easier.)

Here's the fact. GetString function actually does this. It dynamically allocates a char array as it reads your input character by character, growing the size as required. So, get used to life with C string soon. Things will get easier when you use other languages.

Good luck!

If this answers your question, please consider clicking the check mark button for forum maintenance.

1
  • That was a great answer. :) Aug 29, 2016 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .