12
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
    char *s;
    printf("enter the string : ");
    scanf("%s", s);
    printf("you entered %s\n", s);
    return 0;
}

When I provide small inputs of length up to 17 characters (for example "aaaaaaaaaaaaaaaaa") the program works perfectly fine but on providing inputs of larger lengths, it gives me a runtime error saying "main.c has stopped working unexpectedly".

Is there some problem with my compiler (codeblocks) or my pc (windows 7)? Or is it somehow related to the input buffer of C?

4
  • It's called a buffer overflow... Don't use scanf() is you need to get large input values. Commented Feb 5, 2013 at 12:22
  • I don't think that the answers below mention that Kevin . Is it a buffer overflow ? Commented Feb 5, 2013 at 12:54
  • I don't think the answers below were there when I posted this comment, and yes it is a buffer overflow. Your input is larger than the boundaries of your buffer. Commented Feb 5, 2013 at 14:35
  • And to be clear: there's no buffer at all! (The pointer s doesn't point to a too-small buffer; it points nowhere at all.) Commented Feb 2, 2021 at 17:35

9 Answers 9

21

It's undefined behaviour as the pointer is uninitialized. There's no problem with your compiler but your code has problem :)

Make s point to valid memory before storing data in there.


To manage buffer overflow, you can specify the length in the format specifier:

scanf("%255s", s); // If s holds a memory of 256 bytes
// '255' should be modified as per the memory allocated.

GNU C supports an non-standard extension with which you don't have to allocate memory as allocation is done if %as is specified but a pointer to pointer should be passed:

#include<stdio.h>
#include<stdlib.h>  

int main() {
  char *s,*p;

  s = malloc(256);
  scanf("%255s", s); // Don't read more than 255 chars
  printf("%s", s);

  // No need to malloc `p` here
  scanf("%as", &p); // GNU C library supports this type of allocate and store.
  printf("%s", p);
  free(s);
  free(p); 
  return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Best answer so far because of overflow protection. One question if you don't mind: why use malloc() instead of static allocation?
OP asked why used pointer and asked why it's failing. It's minimal example. Static array would work as well for the particular case.
8

the char pointer is not initialized, you should dynamiclly allocate memory to it,

char *s = malloc(sizeof(char) * N);

where N is the maximum string size you can read, And its not safe to use scanf without specifying the maximum length for the input string, use it like this,

scanf("%Ns",s);

where N same as that for malloc.

3 Comments

thou shall not cast the return value from malloc()
malloc return's void *, it should be cast ?
@RamiJarrar malloc() returns void pointer so no need to type cast...its bad habbit as you are doing...refer stackoverflow.com/questions/605845/…
1

You are not allocating any memory to the character array so first try to get memory by calling malloc() or calloc(). then try to use it.

s = malloc(sizeof(char) * YOUR_ARRAY_SIZE);
...do your work...
free(s);

1 Comment

Why bother with malloc() and free()? Just reserve statically.
1

You need to allocate enough memory for buffer where your pointer will point to:

    s = malloc(sizeof(char) * BUF_LEN);

and then free this memory if you do not need it anymore:

    free(s);

2 Comments

Why bother with malloc() and free()? Just reserve statically.
Static reserve is better for this case, but question shows that TS is not familiar with pointers and memory allocation. Just helped him to understand this.
1

You're not allocating memory for your string, and thus, you're trying to write in a non-authorized memory address. Here

char *s;

You're just declaring a pointer. You're not specifying how much memory to reserve for your string. You can statically declare this like:

char s[100];

which will reserve 100 characters. If you go beyond 100, it will still crash as you mentionned for the same reason again.

Comments

0

The problem is with your code .. you never allocate memory for the char *. Since, there is no memory allocated(with malloc()) big enough to hold the string, this becomes an undefined behavior..

You must allocate memory for s and then use scanf()(I prefer fgets())

1 Comment

Why bother with malloc() and free()? Why not reserve statically?
0
#include"stdio.h"
#include"malloc.h"

int main(){

        char *str;

        str=(char*)malloc(sizeof(char)*30);

        printf("\nENTER THE STRING : ");
        fgets(str,30,stdin);

        printf("\nSTRING IS : %s",str);

        return 0;
}

Comments

-1

The code in C to read a character pointer

#include<stdio.h>
 #include<stdlib.h>
 void main()
 {
    char* str1;//a character pointer is created 
    str1 = (char*)malloc(sizeof(char)*100);//allocating memory to pointer
    scanf("%[^\n]s",str1);//hence the memory is allocated now we can store the characters in allocated memory space
    printf("%s",str1);
    free(str1);//free the memory allocated to the pointer
 }

Comments

-2

I was getting this problem. I tried this code below and it worked:

char *text; 
scanf("%s", *&text); 

I dont know how it worked. I just felt like doing it.

1 Comment

Try run the code and print the string.

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.