Question in C Segmentation fault (core dumped) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Question in C Segmentation fault (core dumped)

​#​include​ ​<​stdio.h​> ​#​include​ ​<​ctype.h​> ​#​include​ ​<​cs50.h​> ​#​include​ ​<​string.h​> ​int​ ​main​(​int​ argc, string argv[]) ​{ ​    ​char​ go[​100​]; ​        argv[​1​] = go; ​        ​printf​(​"​%s​\n​"​, go); ​    ​if​ (argc == ​2​ && ​isdigit​(go)) ​    { ​        ​printf​(​"​Done​\n​"​); ​    } ​    ​else ​    { ​        ​printf​(​"​Usage:./caesar key​\n​"​); ​    } ​} It's giving me Segmentation fault (core dumped) What does that mean and how to fix it Aslo what is the difference between String[ ] Char [ ]

17th Feb 2022, 12:44 PM
Abdallah Ashraf
4 Answers
+ 3
Hi Abdallah Ashraf, The code contains a lot of invalid characters which prevents the code from being processed (compiled). This usually happens after copy/paste from sources with different character encoding like copying text from a web page into a text or code editor. I'm not exactly sure, but is <argv> writeable? I mean, you assign <go> memory address to <argv>. Isn't <argc> and <argv> supposedly only readable? Also take care reading <argv>, make sure the indices you use are valid.
17th Feb 2022, 1:45 PM
Ipang
+ 2
Segfault is because you are reaching for a piece of memory you do not own in the program. I'll cover more of that later. string() does not exist it's a function developed with cs50.h so if you put this code in the playgroud it's not a normal helper file. The cs50 string function is equated to char *dynamic_size char *word is a pointer to the first char of word however if the size is unknown you can segfault quickly. char[size] is a block of char variables in a row such that if your word and the terminating char fit inside you are safe however the size will not keep you from going out of it you have to manage that access or segfault.
17th Feb 2022, 12:59 PM
William Owens
William Owens - avatar
+ 1
int main should be written one of two ways: int main(void){} or int main(int argc, char *argv[]){} argc is the number of arguments the command line has including the name of the program argv[] is an array of the arguments in string like formats. if your program is called test.o and you run it with: ./test.o first second argc == 3 argv[0] == "test.o" argv[1] == "first" argv[2] == "second" However it is still a bit more complicated then that since argv[] is really a list of pointers to the arguments which is why they can be different sizes. But lets move on. you cant just char[100] = argv[1] because if it is empty you reach for memory you do not own. Also you are checking the number of arguments in wrong order. There is more to say here hopefully the other will jump on soon and help you out. Command line arguments can be tricky.
17th Feb 2022, 1:55 PM
William Owens
William Owens - avatar
+ 1
William Owens Ipang Thanks for your help i really appreciate it all
17th Feb 2022, 4:36 PM
Abdallah Ashraf