0

I am trying to read a string in a function and then return it to the main program and print it there. The problem is that when I print the string in the main program it appears that there is not data in the string array

The code I wrote is :

#include <stdio.h>

char *getString(){
    char buff[10]; //String Array to be used within the function
    char *pbuff = &buff; //Pointer to the address
    printf("Enter the name: ");
    scanf("%s", buff);
    return pbuff; //Returns the pointer to the array
}

int main(){
    char MyString[10]; //string in the main program
    char *pMyString = (char*)malloc(sizeof(char) * 10); // Allocation for the memory for the pointer


    pMyString = &MyString; //pointer to the array


    *pMyString = getString();

    printf("%s", pMyString);

}

0 Answers0