Home > database >  Storing a string in char pointer (using scanf)
Storing a string in char pointer (using scanf)

Time:04-06

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


int main(){

    char *s1;
    char *s2;

    printf("Enter the string : ");
    scanf("%s",s1);

    printf("Enter the string : ");
    scanf("%s",s2);

    return 0;
}

I'm facing problems in taking inputs in char pointer. Here I'm just taking 1 word as input but Scanf isn't taking any inputs at all when using char pointer, but works well with char arrays. Is it a code blocks bug?

CodePudding user response:

A pointer is a variable that holds a memory address.

An uninitialized variable contains (generally) some random garbage number.

When you pass some random garbage number to scanf and tell it to store a string at that address, it's no surprise that (usually) the program crashes.

It makes no sense to tell scanf to store a string at an address where you don't know what the address is. It's like telling your friend to come over to your house and when they ask which house is yours, you click somewhere random on Google Maps. It's probably going to be in the middle of the ocean or something and they'll drown.

What you need to do is make a space to put the string (such as by declaring an array) and then tell scanf to put the string in that space that you specifically made to hold the string.

A pointer to a string does not hold the string. A pointer to a string is just a signpost saying "the string is over there --->", and you can change it if you want it to point to a different place, maybe holding a different string, but it's never going to hold a string itself.

CodePudding user response:

This is a beginner-C-programmers' bug as old as the language itself.

There are two good solutions to your problem, either change the definitions of s1 and s2 to something like

char s1[BUFFER_LENGTH];
char s2[BUFFER_LENGTH];

...where BUFFER_LENGTH-1 is some sufficiently long string length, to tell the compiler to allocate enough memory on the stack, or manually allocate memory in the heap for them using malloc() like so:

char *s1 = malloc(BUFFER_LENGTH);
char *s2 = malloc(BUFFER_LENGTH);

But keep in mind that in both cases your code is unsafe because for any finite-length buffer, there is a string too long for it, which can cause your program to crash or overwrite something important in memory or give hackers access to stuff etc. etc.

A safe solution is to allocate memory for your strings in one of the two above ways, and only read strings that are small enough to fit into them. If you require the program to be able to handle a string of any length, your code will be much more complex, but there's still a safe way to do it.

#include <stdio.h>

#define BUFFER_LENGTH 20 //Arbitrary max string length   1

int main(){

  char s1[BUFFER_LENGTH]; //Can safely hold 19 chars for string and a null terminator
  char s2[BUFFER_LENGTH];

  printf("Enter the string : ");
  scanf("s",s1); //s tells the function to only read max 19 characters

  printf("Enter the string : ");
  scanf("s",s2);

{

In the above example a string longer than 19 characters will be truncated, for instance "012345678901234567890" will be stored in s1 as "0123456789012345678" and s2 will have "90" since scanf will just use the leftover characters in the input buffer for the next call.

  • Related