Hot answers tagged

44 votes
Accepted

Difference between char and char* in c

The difference between 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, it will perform exactly the same. They ...
user avatar
4 votes

Difference between char and char* in c

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 ...
user avatar
4 votes

about strings and arrays

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 ...
user avatar
  • 17.3k
4 votes

Main difference between char & string, can they be used randomly

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 ...
user avatar
  • 2,625
3 votes
Accepted

In breakout, why use 12 char to store an int?

The question is hinting at you to think about why the array is initialized with a size of 12.
user avatar
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 ...
user avatar
  • 483
2 votes

How to Check Whether a String Contains Non-Alphabetical Characters?

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 ...
user avatar
  • 3,316
2 votes
Accepted

strcat segmentation fault

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 ...
user avatar
  • 17.3k
2 votes
Accepted

printf is changing string contents

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 ...
user avatar
  • 17.3k
2 votes
Accepted

I have written this code for vigenere and it is giving 02 errors

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?
user avatar
  • 7,326
2 votes
Accepted

char* and read-only memory

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 ...
user avatar
2 votes

Why doesn't this code work?

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 ...
user avatar
2 votes
Accepted

What's the C equivalent for "string input = GetString();"

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 ...
user avatar
  • 18.3k
2 votes
Accepted

trail of strange characters after printing character array

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 ...
user avatar
  • 63.2k
2 votes
Accepted

Crypt() includes and compiling

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 ...
user avatar
2 votes
Accepted

Trying to Increment Characters in a String

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 ...
user avatar
  • 20.7k
2 votes
Accepted

Week 6: DNA sequence aligning example: Are running times iterative approach and reqursive same?

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 ...
user avatar
  • 20.7k
2 votes

i want the number of line in which the function is locate , like the function "func2" is in second line so the output shoud be 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 ...
user avatar
  • 18.3k
2 votes
Accepted

Printf doesn't print whole string "11111"

*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 ...
user avatar
  • 20.7k
2 votes
Accepted

vigenere output "incompatible pointer types"

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.
user avatar
  • 18.3k
1 vote
Accepted

What happens when you assign a character string to a char* instead of writing element by element?

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. ...
user avatar
1 vote
Accepted

This is my code for vigenere pset2 and it is giving 4 errors please help.Errors are as follows

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 ...
user avatar
  • 63.2k
1 vote

Parse function not returning error codes

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 ...
user avatar
1 vote
Accepted

pset1 credit: segmentation error

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 ...
user avatar
  • 63.2k
1 vote
Accepted

C prog - COUNTER to find the number of alphanumeric's character in a string?

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>; <...
user avatar
  • 7,326
1 vote
Accepted

String error #include <cs50.h>

Check your spelling of string. You spelt it as stirng it is spelled string
user avatar
1 vote

Assing argv[1] to a string variable (vigenere)

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.
user avatar
  • 186
1 vote

Assing argv[1] to a string variable (vigenere)

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]. ...
user avatar
  • 63.2k
1 vote
Accepted

How to check string argv[] for errors?

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 ` ...
user avatar
  • 63.2k
1 vote

How to check string argv[] for errors?

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 ...
user avatar

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