Questions tagged [c]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2
votes
1answer
230 views

Why is my `mario` pyramid height restriction not working?

Can someone help me with problem set 1? I got the pyramid working, but my height restrictions seems to not work. When I type in letters and negative numbers for height, the program does not print out ...
1
vote
2answers
57 views

Reducing the size of my `filename` array produces strange behavior when using `sprintf()`

Why is it that if I reduce the size of the array in the code below from filename[4] to filename[3], the filename string is always 001? char filename[4]; // if i change 4 to 3, i get filename as 001 ...
0
votes
1answer
546 views

“make” not working (“undefined reference to GetString”)

I'm testing out a basic program called getname that prompts the user for their name to store it in a string variable. However make doesn't seem to work. This is make's output: jharvard@ubuntu:~$ ...
-1
votes
1answer
522 views

How to break out of nested for loops

I would like to stop only the nested for loop, not the do while loop, in the following code. I have tried all the possible positions of break without success. do { draw(d); printf("tile to ...
0
votes
1answer
70 views

Where is unistd being used in this CS50 example (is it superfluous)?

From what I see, the library is not used in this CS50 sample code and yet, it's included: #include <stdio.h> //#include <unistd.h> int main(int argc, char* argv[]) { if (argc != 2) ...
2
votes
1answer
2k views

Passwords Et Cetera, Problem Set 2: Crypto. Walkthrough

I'm wondering how can I do this pset without doing the fist thing that comes to mind when reading this pset. From Itsa Mario to this there is a really big jump. Please talk me about crypt and file I/O ...
2
votes
3answers
821 views

What stuff that's inside array[-1] or array[n + 1]?

Here a portion of the loop I used to implement insertion sort algorithm: while (j > 0 && values[j - 1] > actualElement) { // place the unsorted element inside the correct position ...
0
votes
3answers
157 views

Greedy hangs after prompting for user input

I'm a bit stumped at what I'm missing in my Greedy solution. The program keeps getting hung up right after the prompt for user input. Check50 doesn't give me much information to work from. What is ...
2
votes
1answer
890 views

What's the purpose of the 'salt' string? Isn't it contained in the hashed string anyway?

When checking the manual entry for man crypt, I read: The returned value points to the encrypted password, a series of 13 printable ASCII characters (the first two characters represent the salt ...
2
votes
1answer
2k views

Can't cast a float to an int?

For the life of me I cannot get this float to cast to an int, and I don't know why – I've tried a number of different things, including printing it as an int, which seems to have worked (printf("%d" , ...
1
vote
1answer
420 views

Writing solutions in C++ instead of C

I know C++ from before starting CS50. If I submit a solution written in C++ for problem sets that deal with the C language, will I get credit for my solution?
8
votes
1answer
8k views

What is the difference between bfSize and biSizeImage?

I'm working through pset5's resize right now and I'm at the point where I've got to update the headers for the new file. I'm having trouble computing them, mainly because I can't really tell the ...
1
vote
1answer
983 views

How to Install Stanford CPP Library for Windows (Visual Studio)?

I am using Visual Studio on windows and would like to use Stanford CPP library. Can anyone tell me what is the standard method to include the library header files to run for my visual studio project? ...
1
vote
0answers
2k views

Valgrind says “Invalid read of size 4” while freeing memory recursively in a trie

My PSET "mispelling" works fine and contains no memory leak but when I run the program with Valgrind it says Invalid read of size 4. My unload function calls a deleteTrie function which looks like ...
2
votes
1answer
15k views

How to initialize multiple types of variables in a FOR loop?

Is there a way to initialize two different types of variables in a for loop? For example: for (int i = 0, n = strlen(oldStr), char a, b; i < n; i++) So the variables i and n are ints while a and ...
-1
votes
1answer
425 views

Different methods for transforming a string to lower case in C

In the check() function I've figured that I should transform the input word to all lower case. To do that I wrote this: int wlen = strlen(word); char word1[wlen+1]; //transform the word to all ...
0
votes
0answers
1k views

What does clang's “undefined reference” error mean?

What do the following compilation errors mean and how can I fix them? jharvard@appliance (~/Dropbox/pset3/find): make helpers clang -ggdb3 -O0 -std=c99 -Wall -Werror helpers.c -lcs50 -lm -o ...
-2
votes
1answer
3k views

implicit declaration of function getint() is invalid in c99

#include <cs50.h> #include <stdio.h> int main (void) { printf("get the value of x \n"); int x = getint(); printf("y \n"); int y = getint(); printf("addition of %d and %...
1
vote
1answer
113 views

Accessing const int MAX value from helpers.c

I am trying to implement the hacker3 sorting algorithm. The one that requires a O(n). I think I have figured out the algorithm but I won't spoil it by posting any code here. My question is regarding ...
2
votes
1answer
755 views

Why does accessing an out-of-bounds array element not cause a segmentation fault?

I tried to access an array element greater than which was passed in the search function but it did not cause a segmentation fault. Here is the code in which I was trying to access the element: bool ...
1
vote
3answers
108 views

How do people usually look up syntax?

I've been doing problem set 1, and I seem to be running into blocks with syntax that I do not know, so I can come up with pseudocode and have a rough idea of what I want to do, but then when it comes ...
0
votes
2answers
124 views

How to submit a Pset without appliance?

I want to submit the source code for problem set 1 . But I don't have the cs50 appliance . So what should I do?
0
votes
3answers
2k views

Are const Local Variables Implicitly static?

From Programming in C (3rd edition), page 156 In the case of const variables, they can be stored in read-only memory. So they may not be initialized each time the function is entered. I Just ...
0
votes
0answers
353 views

floating point exception core dumped?

Whenever I try to get the encryption it shows floating point exception (core dumped). According to some Google links this error comes when dividing by 0 but in my code that is not the case. The only ...
0
votes
1answer
231 views

How do I load words in speller that are mixed letters and numbers?

I'm struggling with pset 6 (quite difficult logical and implementation wise, but I like this level of difficulty and this challenge). I'm implementing load and I just found out that I'm 34 words off ...
3
votes
2answers
33k views

How to solve “control may reach end of non void function” error

When I try to compile my code, I get an error message that says "control may reach end of non void function." What does this message mean and how can I avoid this error? Here is the function that's ...
1
vote
1answer
621 views

fifteen.c : printf() not printing anything to the screen

To check the execution sequence of the code, I have added printf statement just below the main(): int main(int argc, string argv[]) { // greet player printf("abc \n"); greet(); ...
0
votes
1answer
131 views

Comparing arrays in C

Is there a good way (or function) to compare all elements of two arrays (or selected elements) without hardcoding (i.e., while(a[1] == b[1] && a[2] == b[2] && ...)) and without using a ...
0
votes
1answer
965 views

what does '-Wall' flag in the clang mean?

I am reading a Makefile of one c file. clang -ggdb -std=c99 -Wall -Werror -o generate generate.c I am unable to understand what -Wall flag in the above line means?
1
vote
2answers
928 views

How can I swap two values and return two integers?

I wanted to write a function that swaps two values, but I'm not sure how to return 2 integers. This code doesn't work, as after swapping the values x is still one and y is still 2: void swap(int in_1,...
0
votes
1answer
119 views

Can I run C code in visual studio 2012?

I'm used to the development environment in visual studio. I was wondering if it would be alright if I ran the problem sets and examples in visual studio? I believe VS does support C but I'm not ...
0
votes
1answer
23 views

Why does this program output 0?

This is a simple program, running in a loop, it prints out x + 1 forever #include <stdio.h> int main (void) { x = 2; while(true) { x += 1; printf("%d\n", x); } }...
3
votes
1answer
15k views

How to free a nested struct?

In preparation for my final project, I am writing a C program that handles task lists. It utilizes a nested struct. For every line it reads from a file it creates a struct of type task. Each task then ...
6
votes
5answers
33k views

Can you print more than one char in C by using “x” * 5 like you can in Python?

In Python you can have a print statement print out a number of the same char by using print("X" * 5) which will give you XXXXX. Can this be done in C with printf?
4
votes
1answer
272 views

Computing speed difference between n++ and n=n+1 in C

I am curious to know the computing speed difference between n++ and n=n+1 in C. I found a rather lengthy discussion on Stack Overflow, where some people are literally contradicting each other. To ...
1
vote
1answer
564 views

where is the Debugger in Gedit for C

I have been having some trouble writing my programs. I have been trying to find the debugger for c to speed up the process, but I have not succeeded. If you have found it can you please tell me where ...
1
vote
4answers
109 views

Can I write an “if” statement without an associated code block?

Is this valid code? if (0 <= tower_height <= 23); If so, how will it be executed?
0
votes
1answer
75 views

Can I upgrade appliance for C++11?

I am an alumni of CS50. However, I still use the appliance. I was trying my hands on c++11 but the appliance I guess is configured for c++98 ( from what I have been able to judge from the errors ). So,...
3
votes
2answers
43k views

How do I share a variable between source files in C with extern?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source ...
0
votes
1answer
3k views

Why do I get a segmentation fault when writing to a string?

The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s", str); ...
1
vote
1answer
1k views

What's the difference between #include <abc.h> and #include “abc.h”

I know this might sound a little unrelated, but, while working on hacker3, I created my own implementation of A* and had decided to keep it seperate from the main program to ensure I can still mess up ...
0
votes
1answer
595 views

recover.c recovers all images successfully, but check50 says it fails to recover the last. Any ideas why this is happening?

I have a while (!feof(infile)) loop, so that exits only when the eof is reached. And it closes all files upon exit. Here's the code: // scan input file while (!feof(infile)) { // read four bytes ...
1
vote
2answers
2k views

GetString() giving me a segfault in C

I am running a program in C using a GetString function (not from the CS50 library). When I run the program I get a segmentation fault error. In GDB, when I backtrace it tells me: Program received ...

1
7 8 9 10
11