0

I am trying to implement DMA for char variable. But I am unable to take input. I tried with all the possible cases I know:

//gets(ptr_name);
//scanf("%[^\n]", &ptr_name);
//fgets(ptr_name, name, stdin);

But I can't even enter input data for the character variable ptr_name. I want to take input as "string with space" as input value. How to solve this problem? And then how to print the entered name in the screen?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
  
int main()
{
    char* ptr_name;
    int name, i;
      
    printf("Enter number of characters for Name: ");
    scanf("%d",&name);
    
    ptr_name = (char*)malloc(name);
    
    printf("Enter name: ");  
    
    //gets(ptr_name);
    //scanf("%[^\n]", &ptr_name);
    //fgets(ptr_name, name, stdin);
                    
    printf("\n Your name is: ");   
         
    puts(ptr_name);  
            
    free(ptr_name);
    return 0;
}
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
nischalinn
  • 1,133
  • 2
  • 12
  • 38
  • DMA tag is for "direct memory access". You probably mean "dynamic memory allocation". – Gerhardh May 15 '21 at 06:14
  • What happened when you tried to input and output the string? What did you expect and what did you get instead? Please show your input, output and expected output. – Gerhardh May 15 '21 at 06:15
  • Related: [Do not use gets](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) and [Input spaces in string](https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) – Gerhardh May 15 '21 at 06:17

2 Answers2

2

scanf("%d", ...) does not consume the enter so the next scanf() gets an empty string. you can use getchar() to consume the enter.

Also, you need to allocate additional byte for the zero at the end of the string / string terminator. See the + 1 in malloc().

As for your questions, your commented scanf() had & before argument 2 which isn't expected (char ** vs. char *) but other than that it will allow spaces in strings. puts() will print the entered name, alternatively you can modify the above printf() to print the name, e.g: printf("\n Your name is: %s", ptr_name);

Lastly, please consult Specifying the maximum string length to scanf dynamically in C (like "%*s" in printf) for dynamically limiting the input size, avoiding buffer overflow.

DISCLAIMER: The following is only "make it work" version of the program above and is not intended for real life use without appropriately checking return codes and limiting the input size:

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

int main()
{
    char* ptr_name;
    int name, i;

    printf("Enter number of characters for Name: ");
    scanf("%d",&name);
    getchar();

    ptr_name = (char*)malloc(name + 1);

    printf("Enter name: ");

    scanf("%[^\n]", ptr_name);

    printf("\n Your name is: ");

    puts(ptr_name);

    free(ptr_name);
    return 0;
}
niry
  • 3,238
  • 22
  • 34
  • Still I can't insert input for name. Screen doesn't wait to enter name, after taking input for name variable, it doesn't allow to take input – nischalinn May 16 '21 at 05:26
  • @nischalinn the `conio.h` you included gives me a hint. are you running on microsoft windows or msdos? try to `getchar()` twice. – niry May 16 '21 at 07:01
  • I am running on Microsoft Windows 10 and IDE is Dev C++. – nischalinn May 17 '21 at 04:35
  • thank you for your detail explanation. It solved my problem as well my concept is also clear. Thank you very much!!!! – nischalinn May 17 '21 at 05:00
0

if you want to get input with spaces you need to use getline():

getline(&buffer,&size,stdin);

here an example:

#include <stdio.h>
#include <stdlib.h>
    
int main()
{
    char* ptr_name;
    int len;
    printf("Enter number of characters for Name: ");
    scanf("%d",&len);
    ptr_name = (char*)malloc(len);
    printf("Enter name: ");
    getline(&ptr_name, &len, stdin);
    printf("\n Your name is: %s", ptr_name);
    
    free(ptr_name);
    return 0;
}
coderx64
  • 185
  • 1
  • 11