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;
}
3
  • DMA tag is for "direct memory access". You probably mean "dynamic memory allocation". Commented May 15, 2021 at 6: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. Commented May 15, 2021 at 6:15
  • Related: Do not use gets and Input spaces in string Commented May 15, 2021 at 6:17

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 the conio.h you included gives me a hint. are you running on microsoft windows or msdos? try to getchar() twice.
I am running on Microsoft Windows 10 and IDE is Dev C++.
thank you for your detail explanation. It solved my problem as well my concept is also clear. Thank you very much!!!!
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;
}

1 Comment

You should note that getline() is a POSIX function.

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.