26
votes
How to solve "control may reach end of non void function" error
This problem comes from a misunderstanding of the flow of control in your algorithm. Here's a representation of that control flow in pseudocode:
if (outer-condition-1)
if (inner-condition-1)
...
14
votes
Memory overlap in C
I believe you mean memmove which takes care of memory overlapping as oppose to memset. but what is memory overlapping anyway?
suppose we have an array of 5 chars, where each char is a byte long
++++...
13
votes
Accepted
What's the difference between #include <abc.h> and #include "abc.h"
when you
#include <cs50.h>
the compiler looks for cs50.h in a standard list of system directories (e.g., /usr/include/).
on the other hand, when you
#include "cs50.h"
the compiler looks for ...
10
votes
What is the difference between bfSize and biSizeImage?
You are right! bfSize should store the size of the whole BMP file — the size of the BMP image + the size of the headers.
biSizeImage should store the size of the BMP image — the size of the ...
10
votes
Pset3: Plurality - Weird output!
You should rethink your logic for the print_winners function.
Here's the pseudocode I'd use.
Declare a maxvote int set to 0.
Iterate over the list of candidates
and check the number of votes. If ...
8
votes
Accepted
How do I share a variable between source files in C with extern?
From this really long answer:
Using extern is only of relevance when the program you're building
consists of multiple source files linked together, where some of the
variables defined, for ...
8
votes
Accepted
Can you print more than one char in C by using "x" * 5 like you can in Python?
In C, char is an integer type; if you multiply a character by an integer, you're really multiplying two integer values. But the result may not be what you expect! For instance, try running this code:
...
6
votes
Accepted
How to free a nested struct?
When you allocate memory by calling malloc(), and you want to free that memory, you have to call free() on every pointer that you initialized it with a call to malloc().
For example, if you have ...
6
votes
Accepted
Can't cast a float to an int?
Your code isn't working for the same reason that this code:
int x = 5;
x + 5;
printf("%i", x);
does not print the number 10.
In C, (int) is a type casting operator; much like the + operator, it ...
6
votes
Accepted
Pset2 Readabilty: Why are my grade levels calculating incorrectly?
This is integer division. When you divide two integers, the result is an integer again, truncating the result, ignoring its fractional part.
You can turn it into floating point division by making at ...
5
votes
Accepted
Can I write an "if" statement without an associated code block?
If statements need to have a body like so:
if (statement)
{
// Body
}
or
if (statement)
// Body
Note that in the second case, the body is strictly one line long, while the first case ...
5
votes
Accepted
recover.c recovers all images successfully, but check50 says it fails to recover the last. Any ideas why this is happening?
feof is only set after fread tries to read the file and fails. When you reach the end of the file, fread will run one more time and your loop will carry on and write one more block (the last one, ...
5
votes
Accepted
implicit declaration of function is invalid in C99
Sorry, beginner error:
But now if we try to print_name(s) in our main function, we still get
an error. And that’s because the compiler reads from top to bottom, in
order, so at the time main ...
4
votes
Accepted
Comparing arrays in C
Have a look at memcmp()!
Also, you can define your own function
bool compare(datatype array1[], datatype array2[], size_t size1, size_t size2)
{
// check whether they're both of the same size
...
4
votes
Accepted
recover.c cannot detect jpeg signatures?
Your program is not recovering the images because the cs50 appliance or linux in this case uses the little-endian convention to store or read the data. I really don't know very much about this myself, ...
4
votes
What is markup language?
what exactly a markup language is and how it is different from
programming language and scripting languages
in layman's terms, a markup language is a set of markup tags. according to this:
A ...
4
votes
understanding 3D arrays
A 2D array is an array of arrays. a 3D array is an array of arrays of arrays. in this case we have an array of 2 arrays each of which is of 3 arrays each of which is 2 elements long.
recall that, in ...
4
votes
Accepted
CS50 IDE undefined reference to `get_int'
On which version of the workspace are you? The current version as of 2017-11-13 is v125. If update50 doesn't end in an error message, you probably are at that version.
Also, how do you compile? The ...
4
votes
Accepted
pset4 recover: recovered images seems to still be corrupted
if (jpegsFound > -1)
Shouldn't that be an else if?
Your current code says:
read in 512 bytes
if it's a signature
close file (if open)
open new file
increment counter
...
4
votes
pset4 recover: recovered images seems to still be corrupted
I agree with Brenda's answer above. But also, you're initialising jpegsFound to 0. That's the value it will have before any signatures were found.
Are you sure you want to write to the file when ...
3
votes
Accepted
Why does this program output 0?
It actually prints 4,8,16,32,64 etc. until it hits the maximum integer value of 232-1 and from then on, it can only print 0 because you've overflowed the integer. It happens so quickly that you miss ...
3
votes
Why does accessing an out-of-bounds array element not cause a segmentation fault?
Accessing an array element that's beyond the boundaries of an array has an undefined behavior. This means that it could cause a segmentation fault, but it doesn't always have to.
3
votes
Accepted
How can I swap two values and return two integers?
This exact problem is reviewed in the lectures. In short, you have to use pointers in your swap function. I'm pretty sure it's week 4, 2nd lecture (lecture continued). Or you can check out the ...
3
votes
Accepted
GetString() giving me a segfault in C
The description of the GetString() function clearly states that it can return NULL on an error or an EOF.
If you pass the return value to strlen() without checking, your program will crash.
string s ...
3
votes
Accepted
Why do I get a segmentation fault when writing to a string?
See the C FAQ, Question 1.32
Q: What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new ...
3
votes
Greedy hangs after prompting for user input
I keep getting hung up right after the prompt for user input.
This is a clear indication that there is an infinite loop (at least once). The problem is that you have 4 infinite loops, the four while ...
3
votes
Accepted
Why is my `mario` pyramid height restriction not working?
The while condition in a do while loop is the condition that causes the loop to repeat.
do
{
//this
}
while (this is true)
Can you see why yours is incorrect? You have used the opposite ...
3
votes
Accepted
Do we need to return 0 in C?
Typically and according to the standard, the return value of main should be of type int which obviously means that you should return an int value from main.
However, you don't really have to do that ...
3
votes
Does only malloc allocated memory on the heap?
In the first case, I am clear that memory will be allocated on stack.
not necessarily. it depends on where the variable is defined. if it's a global variable for example, the memory will be allocated ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c × 581cs50x × 136
pset4 × 67
pset1 × 59
recover × 51
pset2 × 45
pset3 × 38
pset5 × 33
speller × 27
resize × 25
mario × 24
check50 × 22
vigenere × 22
cs50-ide × 20
pointers × 17
string × 17
credit × 16
array × 15
caesar × 12
segmentation-fault × 12
function × 12
pset6 × 11
error × 10
game-of-fifteen × 10
valgrind × 10