0
#include <iostream>

using namespace std;

int main() {
  char *s;

  printf("enter a word \n");

  scanf("%s", s);

  int i;

  for (i = 0; s[i] != '\0'; i++) {
  }

  printf("length is = %d", i);

  return 0;
}

When I compile this program, a segmentation fault occurs.

Can anyone plz tell me, what is the problem in this code?

3
  • 2
    You're trying to put chars in the memory locations pointed to by s. Where does s point? Commented Mar 12, 2020 at 16:55
  • If it has a compiler error please edit the question and add the exact text of the compiler error. I believe there may not actually be a compiler error but instead a runtime error because of the undefined behavior caused by not initializing s. With that said if this is c++ why are using char *, scanf(), and printf() it's much simpler and less error prone if you use modern c++ instead of c code compiled in a c++ compiler. Commented Mar 12, 2020 at 17:11
  • You are including the wrong header for the functions that you are using. For example printf is defined by cstdio Commented Mar 12, 2020 at 17:23

3 Answers 3

2
char *s;

Is unitialized, you can't assign an input stream to it until it is.

char s[100]; //space for 99 characters plus null terminator

or

char* s = malloc(100); //each char has the size of one byte

These are C tools, for C++, however, you can/should use std::string, with std::cin instead of scanf.

std::string s;
std::cin >> s;

If you must use C tools, scanf("%s", s); is not the most safe method, if you don't pass the size of the char array container, changing specifier %s to %100s or changing it altogether to a safer function like fgets(s, sizeof(s), stdin); is a better option.

Sign up to request clarification or add additional context in comments.

4 Comments

If the OP would keep scanf and tries to use std::string there, without resizing the string before, the same problem would exist. So it should be mentioned that if you use std::string you should also consider to switch to std::cin >>
@FireLancer, that is true,
@anastaciu I got corrected here that %ns with a width (e.g. %100s) is a thing so can technically be done, but should probably mention. Also playing with macro's (or worse variables) to insert the right size in the format string isn't going to be pretty.
@FireLancer, yes it's an option, it will limit the size, so I guess it's safe.
1
char *s;

You created a pointer, though it does not point to anything (it is uninitialized).

scanf("%s",s);

You try to read data into the string pointed-to by s, but no such thing exists.

💥!

Allocate some memory and have s point to it… or, better yet, use std::string and std::cin.

Comments

1
char *s;

printf("enter a word \n");

scanf("%s",s);

scanf with %s does not allocate memory, instead it reads into the buffer you provide it (e.g. s = malloc(100);. How big does that buffer need to be to avoid a buffer overflow? Unfortunately it can be however long until a whitespace character, so is inherently unsafe.

You can specify a max up front (not including null terminator!), but to handle variable size you then need to dynamically build the format string which gets complicated. As does what to do if the user enters a string too long.

char s[100];
scanf("%99s", s);

Since you tagged C++, you can use std::string and IO streams (e.g. std::cin for console input) which will handle all the memory allocation for you.

std::string s;
std::cout << "enter a word" << std::endl;
std::cin >> s;

In the case of C, you might use fgets(str, num, stream), this lets you specify the max length and avoid overflow in a simple manner.

char s[128];
fgetsf(s, 100, stdin);

Or with the POSIX 2008 scanf it can allocate the memory, but I don't believe this is universally supported, e.g. by Microsoft Visual Studio.

char *s = 0;
scanf("%ms", &s); // pass a pointer to a pointer!
free(s); // needs to be freed later!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.