Thread: Need help please with array string scan

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    21

    Need help please with array string scan

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE_TEXT 100
    char *textCopy;
    
    
    int textClone()
    {
        int count = 1;
        int i;
        puts("Please enter your text\n");
        char textOne[SIZE_TEXT];
        for (i = 0; i < SIZE_TEXT-1; i++) {  //Scanning to textOne
            textOne[i] = getchar();
            count++; //Counting memory for reserving in malloc 
            if (i == SIZE_TEXT - 1 || i == '\n') {
                textOne[i + 1] = '\0';  // Maybe problem here? When I press enter its not finishing scan process from first time 
                break;
            }
        }
        textCopy = (char *)malloc(count *sizeof(char));
        strcpy(textCopy, textOne); //Coping to dynamic memory the scanned text from textOne array for exercise purposes
        return (&textCopy); // Return address of textCopy pointer
    }
    
    
    int main()
    {
        int address;
        address=textClone();
        printf("Text copied to dynamic memory (%s) and adress of dynamic memory is %d\n",textCopy, address);
        free(textCopy); // Free dynamic memory 
    }
    The getchar function not working correctly can't figure out why if someone can help a lot of thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It seems to work here - excepting the warning.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘textClone’:
    foo.c:24:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
         return (&textCopy); // Return address of textCopy pointer
                ^
    $ ./a.out 
    Please enter your text
    
    hello world
    Text copied to dynamic memory (hello world) and adress of dynamic memory is 6295664
    What are you seeing when it "doesn't work"?

    FYI, forget about the returning of an integer. While you're messing about with a global variable, the address is printed with
    Code:
        printf("Text copied to dynamic memory (%s) and adress of dynamic memory is %p\n",textCopy, (void*)textCopy);
    Now redo textClone() so it returns a char* instead, and dispense with the global variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to scan a string without knowing where it ends?
    By janedoe in forum C Programming
    Replies: 22
    Last Post: 12-04-2013, 09:22 AM
  2. Compiler won't scan my string!
    By mccraryp in forum C Programming
    Replies: 1
    Last Post: 09-28-2013, 08:15 AM
  3. Best way to scan string for a number?
    By SneakySnake in forum C Programming
    Replies: 2
    Last Post: 12-07-2012, 09:22 AM
  4. Best way to scan in a string
    By Matt Hintzke in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 06:33 PM
  5. scan a string
    By Max in forum C Programming
    Replies: 4
    Last Post: 12-02-2002, 04:26 PM

Tags for this Thread