Thread: Function not splitting words from dynamically allocated array correctly

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    Function not splitting words from dynamically allocated array correctly

    I want to write a function that splits a string into words (strings with no whitespace), returning a (dynamically allocated) array of pointers to (dynamically allocated) copies of the words in the string.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void readText(char *s)
    {
        s = (char*)malloc(256*sizeof(char));
        if(s == NULL)
        {
            fprintf(stderr, "Memory could not be allocated.");
            exit(-1);
        }
        while(fgets(s, sizeof(s)/sizeof(s[0]), stdin))
        {
            char *ptr = strchr(s, '\n');
            if(ptr)
            {
                *ptr = '\0';
            }
            char *word = strtok(s, " ");
            while(word != NULL)
            {
                printf("Word : %s\n", word);
                word = strtok(NULL, " ");
            }
        }
        free(s);
    }
    int main()
    {
        char *s;
        readText(s);
        return 0;
    }
    I wrote this so far and I wanted to see if the function is getting each word from the input correctly and it's not.
    Input:
    anna has many red delicious apples
    Output:
    Word : anna
    Word : ha
    Word : s
    Word : many
    Word : red
    Word : del
    Word : icious
    Word : apples

    I have no idea why this is happening, my best guess would be that the second argument in fgets is wrong, I tried a lot of different things but nothing is working, I removed the trailing newline but no improvement.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > sizeof(s)/sizeof(s[0])
    This only works with real arrays.

    Code:
    char a[100];
    // sizeof(s)/sizeof(s[0]) will give you 100
    Neither of these will tell you how much memory you have to play with.
    Code:
    char a[100];
    char *pa = a;
    char *pb = malloc(100);
    // sizeof(pa)/sizeof(pa[0]) will NOT give you 100
    // sizeof(pb)/sizeof(pb[0]) will NOT give you 100
    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. Splitting dynamically allocated 2D arrays
    By CodeNovice in forum C Programming
    Replies: 1
    Last Post: 06-25-2014, 11:46 AM
  2. Replies: 18
    Last Post: 01-30-2014, 08:42 AM
  3. Replies: 1
    Last Post: 12-01-2011, 12:38 PM
  4. Using dynamically allocated strings to sort words
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-13-2009, 04:01 PM
  5. Using dynamically allocated strings to sort words
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-12-2009, 09:10 PM

Tags for this Thread