5 votes
Accepted

Modulo not working in caesar

Modulo is working correctly, but you've overlooked something. The idea behind using modulo is this. When you represent letters as numbers between 0 and 25 inclusive, and add a number to shift the ...
user avatar
  • 63.4k
3 votes
Accepted

Void init(void) or void init(int d)?

The int variable named d is a global variable. Global variables are variables that are declared outside of any functions. They can be accessed from anywhere in the source code file and that's why we ...
user avatar
  • 17.4k
3 votes
Accepted

Why can we pass a struct variable to a function by value but not an array?

As taught we can pass a whole "struct" by value to a function but we can't pass an array by value. Why? the short answer is: because structs and arrays are implemented differently in C. longer ...
user avatar
  • 17.4k
3 votes
Accepted

How to list the functions of a library in the terminal?

if you want to "list the functions in a library", you should look at the header file(s) aka .h file(s). if you wanna search for functions in the standard library, you could execute man -f keyword ...
3 votes

Why do macros take more space than an equivalently defined function?

The plain and simple truth is: Macros are NOT stored directly in memory.(during program execution and/or compiling) What happens is, that, the values are copied from the macro to where you used the ...
user avatar
  • 1,146
3 votes
Accepted

Why do macros take more space than an equivalently defined function?

A function is identified by an entry point (address) inside the executable code. You use a function from different parts of your code. If you use a macro, the object code of that macro is substituted ...
user avatar
  • 1,156
3 votes
Accepted

PSET5 Speller Load function skips the words from the dictionary

This while (fscanf(input, "%s", buffer) != EOF) executes an fscanf, followed immediately by this fscanf (input, "%s", buffer);, therefore it is processing every other word.
user avatar
2 votes

Is there any difference between foo(void) and foo()? (is ´void´ just a convention?)

First we've been taught to use (void) when declaring a function and use () when calling, using a function... Apart from that, void foo() means "a function foo taking an unspecified number of ...
user avatar
  • 201
2 votes
Accepted

Why does this recursive factorial function hit segmentation fault?

The fact() function is a recursive function that takes a pointer to an int as input. However, the internal recursive call, return *a * fact(a - 1); is not passing a valid pointer to an int. It's clear ...
user avatar
  • 63.4k
2 votes
Accepted

is that a function pointer and how can it be used?

typedef void (*sighandler_t)(int); defines sighandler_t as a pointer type to a function whose return type is void and takes an int as an argument. the name of a function, just like the name of an ...
user avatar
  • 17.4k
2 votes

Pseudocode for Load Function in PSet6

For steps 2-3, the data structure (e.g., the hash table in your case) that's gonna be used to store the words in the dictionary and the array of pointers to variables of that type both should be ...
user avatar
  • 17.4k
2 votes

PSET3:Find: Code works without the function, doesn't work with the function

C, by default, passes parameters to functions by value. That means when the function swap is called, two new variables, x and y are created, and the values of the integers are copied into them. When ...
user avatar
2 votes
Accepted

space between function name and variables e.g. int main (void)

Nope, won't hurt a thing. It's just a matter of style, but has no effect on functionality. The common practice though is not to have a space between main and the first parenthesis. If this answers ...
user avatar
  • 63.4k
2 votes
Accepted

Lecture 2 Arrays

The error you are getting is not saying that you are passing a double instead of a float, but that a double is expected. You are passing average, which is not a variable but a function. Your prototype ...
user avatar
1 vote
Accepted

Pset6 - Analyzer.py - Says Undeclared Function

Based on how you write that, I assume both load_words and __init__ to be methods. As you made load_words a method, you'd have to call it like self.load_words instead of just load_words.
user avatar
  • 20.7k
1 vote
Accepted

Could we declare the main function as "main () {}" rather than "int main (void) {}"?

Yes, it's wrong. According to the c99 standard, the return type of main is "int" and not writing the return type of a function is bad practice. The compiler will assume it's int, but you should write ...
user avatar
  • 1,677
1 vote
Accepted

scratch global functions

No I don't think this is possible. Each script is for the sprite it was written for.
user avatar
  • 7,326
1 vote
Accepted

What does "using" a variable entail and why does this inconsistency come up? (pset1, greedy, general question)

The root of this problem lies in the order of operations. In C, postfix increment (++) binds more tightly than dereference (*). Thus, what the code *coins++; does is not to dereference coins and then ...
user avatar
  • 188
1 vote

Pset4 recover.c compiles, but can't seem to print Jpgs at all

I tried to isolate some of the codes in modular form and created a file just to check this code. It seem to be the problem. Did I declare the function within the if statement wrongly? #include <...
user avatar
1 vote
Accepted

Pset4 recover.c compiles, but can't seem to print Jpgs at all

Just to be crystal clear: if (data[3] != 0xe0 || data[3] != 0xe0 || data[3] != 0xe1 || data[3] != 0xe2 || data[3] != 0xe3 || data[3] != 0xe4 || data[3] != 0xe5 || data[3] != 0xe6 || data[3] != 0xe7 ||...
user avatar
  • 3,035
1 vote
Accepted

(PSET 1) Creating functions for water.c. Dont understand how this function worked. (Contains pset answer)

A real answer is further down Take note to how I formatted your code according to CS50's style guide. Well formatted code is important if you expect others (or even yourself at a later date) to read ...
user avatar
  • 1,223
1 vote
Accepted

PSet 3 - Game of fifteen - draw tile gives weird output

The board[][] array is a global variable. In both functions, the code is creating a shadow array called board[][]. int board[DIM_MAX][DIM_MAX]; By redeclaring the board array in each function, it ...
user avatar
  • 63.4k
1 vote

Use of functions and variable declarations outside main() on pset1 greedy

The difference between where a variable is declared is scope. Strictly for this discussion, consider main to be a function, even though it is a special one. A local variable is a variable declared ...
user avatar
  • 63.4k
1 vote
Accepted

CS50 pset 5 cannot load

No words are loaded because the while loop never executes. The fscanf() call is attempting to store a word in dict_word, but the pointer dict_word was never initialized. It wasn't set to null and it ...
user avatar
  • 63.4k
1 vote
Accepted

pset6 parse: char* pointers are right in parse but return wrong value

Notice the difference between the declaration of abs_path/query in main char abs_path[LimitRequestLine + 1]; char query[LimitRequestLine + 1]; vs. the declaration in parse. bool parse(const char* ...
user avatar
1 vote
Accepted

Why does PrintName return void?

Good question. There is no requirement to capture a return value. Just because a function returns a value doesn't mean that the calling code needs to do anything with it. For many functions, like ...
user avatar
  • 63.4k
1 vote

Difference between Pass by Value and Pass by Reference (a practical example)

For set_int() function, it's not complicated. You understand it correctly. However, things are confusing for C arrays. Actually, to me, accessing array member via bracket notation is just a syntactic ...
user avatar
1 vote

How to end with an unsuccesful memory allocation?

Here are my thoughts on your interesting question. It touches on the broader category of error handling. Any return value != 0, in the main function signals an error. Whatever return (error) value ...
user avatar
1 vote
Accepted

Pointers in a function

Nothing in the C standard guarantees how function arguments are read (meaning in which order) or in what order they're going to be stored in memory. In the case in main, the memory address where n ...
user avatar
  • 1,677
1 vote
Accepted

Using function as a statement without using its return value

just because a function has a return values, this doesn't mean that you have to use it. per the man page of printf, the value returned represents the number of characters printed (excluding the null ...
user avatar
  • 17.4k

Only top scored, non community-wiki answers of a minimum length are eligible