Nikunj Banka
Nikunj Banka

Reputation: 11365

Taking string input in char pointer

#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?

Upvotes: 12

Views: 67037

Answers (9)

Ziaur Rehman
Ziaur Rehman

Reputation: 1

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.

Upvotes: -2

ganesh borkar
ganesh borkar

Reputation: 3

#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;
}

Upvotes: 0

Sai Ganesh Myneni
Sai Ganesh Myneni

Reputation: 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
 }

Upvotes: -1

P.P
P.P

Reputation: 121387

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;
}

Upvotes: 21

Rami Jarrar
Rami Jarrar

Reputation: 4643

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.

Upvotes: 8

m0skit0
m0skit0

Reputation: 25873

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.

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25705

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())

Upvotes: 0

oleg_g
oleg_g

Reputation: 532

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);

Upvotes: 1

akp
akp

Reputation: 1823

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);

Upvotes: 1

Related Questions