-3

I made a code but for ome reason when i compile it I can type in the strings but then the compiler breaks.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

main(){
    char s1[100]={};
    char s2[100]={};
    gets(s1);
    gets(s2);
    int zbr;
    funkcija(s1[100], s2[100], &zbr);
    printf("Zbroj duljina stringova je: %d", zbr);
}

void funkcija(char x1[100],char x2[100], int *pzbr){
    int n1=0, n2=0;
    n1=strlen(x1);
    n2=strlen(x2);
    *pzbr=n1+n2;
}

After typing in the strings it says the program has stopped workig and Windows is trying to fix it like when you foget to & put an adress in scanf.

Nuke
  • 1
  • 1
    When you call the `funkcija` function and pass `s1[100]`, what is the expression `s1[100]` *really* doing? Try to explain it to your [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). There's also some other problem that indicates you need to take a couple of steps back, invest in a couple of books and start reading them from the beginning. – Some programmer dude Dec 21 '21 at 09:38
  • 2
    Welcome to SO. You should turn up compiler warnings. In your function call `s1[100]` is a single element of that array. It is also one behind the last element. Both (accessing the array out of bounds and passing a wrong parameter type to your function) cause undefined behaviour – Gerhardh Dec 21 '21 at 09:39
  • 1
    Unrelated: **Never** use `gets`. It' was even removed from the language in C11. – Ted Lyngmo Dec 21 '21 at 09:40
  • 1
    Also, ***never ever*** use `gets` **!** It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) it has been obsolete since the 1990's, and was removed completely in the C11 standard ten years ago. – Some programmer dude Dec 21 '21 at 09:40
  • Thank you for the suggestion and for the help. – Nuke Dec 21 '21 at 09:43
  • Side note: Empty brace initialisers are only legal in C++, not in C... – Aconcagua Dec 21 '21 at 09:44
  • If you use GCC, you can turn up warning level with options `-Wall -Wextra`. You should get some hint "making pointer from integer" for your function call. – Gerhardh Dec 21 '21 at 09:44
  • That is such bad code that no wonder such things are happening. `gets` doesn't even exist. – Shambhav Dec 21 '21 at 09:45
  • Should I be using scanf for strings because i know they break sometimes when i don't put a & inside it. – Nuke Dec 21 '21 at 09:50
  • Good job on a short program. However, you didn't ask a question. Please read [how to ask good questions](//stackoverflow.com/help/how-to-ask). I haven't downvoted, but ["it's not working" is not helpful](http://idownvotedbecau.se/itsnotworking/) on "I Downvoted Because" explains the issue, including why it's an issue. – outis Dec 21 '21 at 09:54
  • Give your code descriptive names. Rather than "function" it could be called "add_two_pointers" – stark Dec 21 '21 at 11:51

1 Answers1

1
  • main() is an invalid form of main() in hosted systems since year 1999. Use int main (void) instead.
  • char s1[100]={}; is invalid C, you need to enter at least an initializer such as { 0 } or alternatively leave the array uninitialized.
  • s1[100] is invalid C for calling a function, you are passing a single element (and access the array out of bounds) instead . This should have been funkcija(s1, s2, &zbr);.
  • gets was removed from the C language in year 2011 and cannot be used. Study how to use fgets instead.
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Right now in highscool i am taught to use gets so this has helped. – Nuke Dec 21 '21 at 09:53
  • 2
    @Nuke Unfortunately that means that your teacher is very incompetent and shouldn't be teaching C. `gets` was flagged as dangerous and obsolete in the mid 1990s and finally completely removed from the language 10 years ago. – Lundin Dec 21 '21 at 09:57
  • I have actually asked him why don't we use it and he explained it so it's no problem. And he is a great teacher but he just has to do it by curicculum. – Nuke Dec 21 '21 at 13:57