Hot answers tagged

31

The difference char* the pointer and char[] the array is how you interact with them after you create them. If you are just printing the two examples will perform exactly the same. They both generate data in memory, {h,e,l,l,o,/0}. The fundamental difference is that in one char* you are assigning it to a pointer, which is a variable. In char[] you are ...


4

Good question! GetString uses a slightly different technique, namely dynamic memory allocation. Using dynamic memory allocation, you can allocate and/or free memory at run time (i.e., as the program is running). You'll get exposed to that technique as you proceed with the course. For now, what you should probably know is that basically GetString allocates ...


4

string is a variable type defined by the CS50 library - it does not normally exist in C. Looking at the code for CS50.h will show this: /* * Our own data type for string variables. */ typedef char *string; This line could be read that a new type is defined, as a char *, called string. In other words, string is defined as exactly the same thing as a char ...


3

C99 N1256 draft There are two different uses of character string literals: Initialize char[]: char c[] = "abc"; This is "more magic", and described at 6.7.8/14 "Initialization": An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (...


3

The question is hinting at you to think about why the array is initialized with a size of 12.


3

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 = GetString(); int stringlength = 0; if (s != 0) stringlength = strlen(s); This at least won't crash. I also observe that the code fragment: // ...


2

The string argv[] refers to string of strings. So when you are trying to do argv[1] then it means that you are looking for a string, but you cannot use isalpha(string), because the argument passed to isalpha() is char. It should be like isalpha(argv[1][0]), where argv[1][0] refers to the first character in the second string in the list of strings.


2

First, strcat doesn't work because of the fact that the string literal "a" that's stored in s is stored into a read-only-data section in memory. So you cannot modify its contents. A simple fix to that is to use a char array that's big enough to fit the concatenated strings as well as the null terminator. So char dest[6]; strcat(dest, "hello"); should work ...


2

It may be hard for you to figure out the cause of the problem assuming that you just finished week 2. The problem deals with concepts like local variables, scope, stack, etc. You may probably know about local variables and scope and you'll get exposed to the rest as you proceed with the course. I'll try to simplify things as much as I can. If you feel like ...


2

The problem is that argv is an array of strings, and you try to use it as a single string. See this line: if (!isalpha (argv[1][i])) and this: if (isupper (argv[j])) Can you spot the difference?


2

Yes and no. Yes it is stored to memory, but the way in which it is declared has no memory leaks (try making a test file and running valgrind to see for yourself). What does cause a memory leak is when you allocate memory for a variable using malloc (or similar) and don't subsequently free that memory. For example: void print_hello_twice() { char* hello ...


2

Because you are creating c as an array of strings. I think you meant to make c an array of n chars. Remember that a string is an array of chars in which the last char is a null terminator \0. I suggest you change this line: string c [n]; to: char c[n]; Since you are printing c character by character you don't need to add the null terminator at the end. ...


2

If you know in advance how big of a string you are willing to accept, you can put the limit into your scanf argument itself. Try this small program: #include <stdio.h> int main(void) { char words[15]; scanf("%14s", words); printf("%s\n", words); } You're telling scanf to only take in 14 characters (with the 15th reserved for the null ...


2

Uhhuh. I see that it is doing a nice job of encoding and storing the encoded characters in an array. But have you also stored the end of string marker '\0' at the end of the string? printf will keep printing whatever it finds in memory until it finds that null character which marks the end of the string. If this answers your question, please click on the ...


2

It will work when constructed as in the man page. From man feature_test_macros: NOTE: In order to be effective, a feature test macro must be defined before including any header files. This can be done either in the compilation command (cc -DMACRO=value) or by defining the macro within the source code before including any ...


2

One way would be to interpret characters 'a' to 'z' as digits in a number with base 26, start at "aaaa" (representing 0), and then adding 1 to the last string. Add or subtract 'a' for conversion between character and "digit" value. Instead of storing the string as your state, you could also store the values, and convert to the string for passing to crypt ...


2

Recursive with memoization and non-recursive do the same number of computations. The recursive version however is more complex, memoization requires some check whether the value already has been calculated (the loops guarantee that), the additional function calls add some overhead, memory and time wise. The most important thing in recursion is a) defining ...


2

The CS50 StackExchange is for questions about the CS50 course (and problem sets, etc.). It is not a general "I need help with my homework from another course."-forum. I'd suggest that if this is not CS50-related, and I see no indication that it is, that you choose another forum. Perhaps Stack Overflow?


2

*a is same as a[0], pointing to the first element of a. Generally, *(a + i) is same as a[i]. I don't think you want an array of strings (string is a typedef of char*) all pointing to the same variable (and it's dangerous to use %s with &b, as you don't know where the next zero byte is!), I assume you want an array of char instead (don't forget space for ...


2

string out[n]; You've declared an array of strings with this declaration. What you probably want is one string (ie, an array of chars). Change that to char out[n]; to get one string.


1

I'll let you visualize this for yourself. Go here http://www.pythontutor.com/c.html#mode=edit and paste your code, then hit "visualize execution". You'll see graphically what's happening in memory. EDIT: I'm sorry, the server seems to be down, so I'll try to use Python to show you what the site would have shown: And finally I'll direct you to a question ...


1

string key[klen]; You declared key as an array of strings. Instead, declare it as a char array and it will compile. There are other problems, but since you haven't successfully compiled the program yet, I'll let you have a chance to work on them. Besides, those would be new questions. If this answers your question, please click on the check mark to ...


1

Your code compiles, but does it process a request through the browser or curl? I think it seg faults here strcpy(method, word); every time. From man strcpy: the destination string dest must be large enough to receive the copy. You haven't allocated any memory for method (or Httpversion or requesttarget). Your approach is spot on. break the line ...


1

Classic error. The function isdigit() takes a single character as input. You are attempting to feed it an entire string, so it chokes, causing a seg fault. If you want to verify that every character in the string is a digit, you need to loop through it and test each character, one at a time. If this answers your question, please click on the check mark to ...


1

Your problem is pretty simple. In a for loop, after the end of each loop, the third parameter (<increment command>) of the loop is executed, every time. for (<initialization>; <condition>; <increment command>) In your case your <increment command> is i++, so i gets incremented by 1 in each iteration. But, in your loop, after ...


1

Check your spelling of string. You spelt it as stirng it is spelled string


1

The best and most efficient way is to out grow CS50.h use char* newString = argv[1]; argv will always be there so you might as well point to it or use it.


1

string keyword = argv[1]; will work because you are assigning the address of string argv[1] to keyword. You can now treat keyword as a char array and access the individual characters as keyword[i]. You could also do this: char keyword[strlen(argv[1])+1]; strcpy(keyword, argv[1]); Instead of keyword pointing at the same memory location as argv[1], this ...


1

Your program is actually working as designed. What you're probably overlooking is how the OS shell is handling the inputs to the program. Single quotes, double quotes and the single back quote (the ` and ~ key below the escape key on non-apple keyboards) are treated differently. They would be used, for instance, to identify a complete string. As an example, ...


1

If you're trying to printf an apostrophe, or other special characters in C, you need to enter the proper escape sequence. You've already seen an example of this...the \n that you have to type in order to enter a newline character. For an apostrophe, the character is \' To do a check on an apostrophe, you would want to do something like the following: if ( ...


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