Skip to main content
8 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 ...
Ciro Santilli OurBigBook.com's user avatar
3 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 ...
Cliff B's user avatar
  • 69k
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?
ChrisG's user avatar
  • 7,396
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 ...
NullityNull's 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 ...
curiouskiwi's user avatar
  • 18.7k
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 ...
Ricardo David Antonetti's user avatar
2 votes
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. ...
Yuri Laguardia's user avatar
2 votes
Accepted

Split a sentence ito an array of words without using strtok

I'll try to give you a visualization of what anfauglit explained. This is what your program is doing: https://goo.gl/4MfFx4 (click the link, wait some seconds for it to load and then keep clicking ...
Yuri Laguardia's 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 ...
Blauelf's user avatar
  • 21k
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 ...
DinoCoderSaurus's user avatar
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 ...
Blauelf's user avatar
  • 21k
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 ...
curiouskiwi's user avatar
  • 18.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.
curiouskiwi's user avatar
  • 18.7k
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 ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

Caesar : Extra random characters at the end of a char ciphertext array

You have to understand how printf and strlen work. Both of them (and several other functions) depend on the existence of the end of string marker, \0 at the end of the target string. If the EOS marker ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

check function in speller pset5, not quite understanding the apostrophe

n's is the first entry starting with n in dictionaries/large. Apostrophes are significant in the trie version of this pset, since that must store every character of word separately; they are not ...
DinoCoderSaurus's user avatar
1 vote
Accepted

While defining a string using char*,how does it know where to stop?

Character pointers are normally used to build literal strings, the most usual way is to make a statement of the following type: char *name = "my name is very long"; Some things you need to know ...
MARS's user avatar
  • 5,169
1 vote

Printf doesn't print whole string "11111"

Since a is a pointer, *a dereferences the pointer. I would fumble over the explanation, but found this post (cited here) a great resource. The variable is named p in this example, not a, with the ...
DinoCoderSaurus's user avatar
1 vote
Accepted

Why string variable has no square bracket?

The reason why a string variable does not have brackets, is because it is not a string array. (It is a char array or a char pointer.) In fact, string is a new type, defined in the cs50 library. It ...
Peter Pesch's user avatar
  • 1,009
1 vote

Similarities (More) sometimes fails but works in most cases

You seem to confuse Operation.DELETED and Operation.INSERTED. Operation.DELETED is used when you advance in the source string a (so you change i) without advancing in target string b, i.e. the source ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

difference between int* and char*?

int * is a pointer to an int, so you'll get two errors there: error: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int' [-Werror,-Wint-conversion] ...
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

string A is assigned to B. why does A change now, when B is changed?

string type vars are actually pointers to char arrays. When you do the following assignment, string ciphertext = plaintext; you are actually assigning the address of the string plaintext to ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Editing two similar strings

It happens because a "string" is actually a pointer to a char array that ends in a null terminator, so when you do "s1 = s2" you are simply pointing s2 to the same array as s1. This is covered in the ...
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

Help with converting 2 or more chars to a single string

Create an array of char, or a pointer to char with enough heap memory allocated using malloc (remember strings require a termination character '\0', so are longer than their content by one character). ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Arranging Strings using pointers

Here I see a problem with the swaps function and the indexes of a [i], as I believe on the return of the swaps function is lost control of the indexes i. Your swaps function ends with the assignment ...
MARS's user avatar
  • 5,169
1 vote

How could you create a dynamic array of strings in C?[Beginner]

You can ask user to separate names with comma or space or some other delimiter. You can find strchr() function useful for parsing names in such an input. Another option is to ask further names in a ...
t.m.'s user avatar
  • 158
1 vote

C random string generator help? cs50

This string word = ""; sets word to a "string literal", and as such it is stored in read-only memory, ie it's immutable; therefore you cannot do this word[i] = set[rand() % 52];. That's what is giving ...
DinoCoderSaurus's user avatar
1 vote
Accepted

strcpy doesnt work

As I see it, path does not change, yes change path_match that you redefine, I think your function is quite correct, Maybe your problem is the statement malloc (sizeof (path) + 11) I would change it ...
MARS's user avatar
  • 5,169
1 vote

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

Funny thing is that I can't get much to work right in cs50.h so I've just been looking up standard c programming techniques. It's just been much easier to find working examples on the Internet using ...
George Ou's 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 ...
Cliff B's user avatar
  • 69k

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