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?

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • It's called a buffer overflow... Don't use scanf() is you need to get large input values. – Kevin Feb 05 '13 at 12:22
  • I don't think that the answers below mention that Kevin . Is it a buffer overflow ? – Nikunj Banka Feb 05 '13 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. – Kevin Feb 05 '13 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.) – Steve Summit Feb 02 '21 at 17:35

9 Answers9

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;
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • +1 Best answer so far because of overflow protection. One question if you don't mind: why use `malloc()` instead of static allocation? – m0skit0 Feb 05 '13 at 14:49
  • 2
    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. – P.P Feb 05 '13 at 16:00
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.

Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52
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);
akp
  • 1,753
  • 3
  • 18
  • 26
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);
oleg_g
  • 512
  • 3
  • 7
  • Why bother with malloc() and free()? Just reserve statically. – m0skit0 Feb 05 '13 at 12:42
  • 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. – oleg_g Feb 05 '13 at 13:00
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.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
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())

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
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;
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-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
 }
-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.

cigien
  • 57,834
  • 11
  • 73
  • 112