I need a bit of help for my following program which inputs a string from the user and also prompts from the user a character from that string so as to display the number of times that character is present in that string. My program is not cs50 IDE specific but does relate to my problem with pointers. I have finished my psets but this problem persists because the following program does give the required output using &*pointername which according to me doesn't hold any relevance. Any suggestions will be appreciated!

void char_array(char *i, char j);
int main()  
   char *a;
   char b;
   printf("choose the charachter");
   return 0;
void char_array(char *c, char d)
   int counter=0,i,len=strlen(&*c);

gets does not allocate memory, so you write to wherever a points to (might cause a segfault).

Citing https://reference.cs50.net/stdio/gets:


Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

So you could do like

char a[100];
fgets(a, sizeof(a), stdin);

You might have to deal with a newline at the end, which probably is not an issue in your case.

Your counting function would probably work.

&* does not make any sense to me, &*a should be same as a.

  • Thanks for the help! But there is one more doubt, get_string() doesn't exist for many IDE as they don't have it in their packages so, what can you suggest for its replacement in this case?And also the above program does work very unknowingly, are there any reasons for that because I don't see any relevance of &*pointername? – sk.76 Sep 5 '17 at 15:51
  • I'd assume that your programme works by accident then, as you don't provide any memory location for gets to store a string. Don't have experience in console input of larger strings (usually read character by character in most code), so no recommendations there. Last time I checked, GetString (now replaced with get_string) also read the console input character by character. – Blauelf Sep 6 '17 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .