2

I'm learning how to use pointers in C (with malloc and free), and I'm having some troubles with this exercise. I just want to make an array of pointers, where I want to save the direction of every word. Then I want to do a free() for a specific word, but this free makes my program to crash.

int main
{
    printf("Introduce how many words do you want. \n");
    scanf("%d", &numWords);
    getchar();

    char ***array = (char***)malloc(sizeof(char**) * numWords);

    if (array == nullptr)
    {
        exit(1);
    } 

    for (int i = 0; i < numWords; i++) array[i] = (char**)malloc(sizeof(char*)) ;

    for (int i = 0; i < numWords; i++)
    {
        printf("Enter your word number %d: \n", i + 1);
        scanf("%s", &(array[i]));
        getchar();
    }

    for (int i = 0; i < numWords; i++)
    {
        printf("%s \n", &(array[i]));
    }

    free(array[1]);

    printWord(array[2])
}

Also, I want to make this function because I want to print every character of the word with a space before. It makes my program crash aswell.

void printWord(char **array)
{
    for (int i = 0; i < strlen(*array); i++) printf("%c ", &((*array)[i]));
}

Don't know how to focus this. What do you recommend to me? Do you find any problems in my code? Thank you.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
Soutuyo
  • 106
  • 10

2 Answers2

0

You are getting your stars mixed up. This is how it works:

  • char*: string
  • char**: list< string >
  • char***: list< list< string > >

Go over your code again and check that every printf("%s" ...) corresponds to a char* and every printf("%c" ...) corresponds to a char. Also turn on all warnings in your compiler, if it is any good it should warn you when you pass the wrong types to printf().

Hint: Your array variable in main should be a char**, not char***.

jforberg
  • 6,537
  • 3
  • 29
  • 47
-1

You need char** and there are a lot of problems and errors which should be fixed:

  • int main{} should be at least int main(void){} you need (void)
  • no checking scanf for errors
  • nullptr which is c++ keyword, should be NULL
  • most important is that way you free what you malloced.
  • casting malloc is not always a good Idea, please read this

Your code should be like this:

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

int main(void){
    long unsigned int numWords,i;
    char **array;

    printf("Introduce how many words do you want:> ");
    if((scanf("%lu", &numWords)) != 1){
        printf("Error, Fix it!\n");
        exit(1);
    }

    array = malloc(sizeof(char*) * numWords * numWords);

    if (array == NULL)    {
        exit(2);
    }

    for (i = 0; i < numWords; i++){
         array[i] = malloc(sizeof(char*) * 100);
    }

    for (i = 0; i < numWords; i++){
        printf("Enter your word number %lu:> ", i + 1);
        if((scanf("%s", array[i])) != 1){
            printf("Error, Fix it!\n");
            exit(3);
        }
    }

    for (i = 0; i < numWords; i++){
        printf("%s \n", array[i]);
    }

    for (i = 0; i < numWords; i++){
         free(array[i]);
    }
    free(array);

    return 0;
}

Output:

Introduce how many words do you want:> 3
Enter your word number 1:> Michi
Enter your word number 2:> aloha
Enter your word number 3:> cool
Michi 
aloha 
cool
Community
  • 1
  • 1
Michi
  • 5,175
  • 7
  • 33
  • 58
  • I think the line ```array = malloc(sizeof(char*) * numWords * numWords);``` should be: ```array = malloc(sizeof(char*) * numWords);``` – Nicolas Gong Jun 11 '21 at 08:32