Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1094377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:57:52+00:00 2026-05-16T23:57:52+00:00

As input I have a pointer to char pointer containing: {ab, cd} As output

  • 0

As input I have a pointer to char pointer containing:

{"ab", "cd"}

As output I need to create the following Cartesian product:

{"abab", "abcd", "cdab", "cdcd"}

I created a function that receives “ab, cd” and a pointer to char pointer that is meant to hold the resulting set. Although everything seems to be working fine inside the function, once it gets exited, my output remains empty. I suppose I’m doing something wrong during the concatenation but I’m not sure what.

This is how my code looks like:

#include <stdio.h>

void Permute(char**, int, char**);

main() {
    // my input
    int words = 2;
    char **input;
    input = malloc(sizeof(char*) * words);
    input[0] = "ab";
    input[1] = "cd";

    // compute how much memory we need
    char **output;
    output = malloc(sizeof(char*) * (words * 2));

    // start permutation
    Permute(input, words, output);

    // show output
    int i = 0;
    for(i = 0; i < (words * 2); ++i) {
        // should print: {abcd, abab, cdab, cdcd} 
        // but nothing gets printed
        printf("%s\n", output[i]); 
    }
    free(input);
    free(output);
}

void Permute(char **input, int words, char **output){
    int i = 0, j = 0, k = 0;
    char str[5];
    for(i = 0; i < words; ++i) {
        for(j = 0; j < words; ++j) {
            strcpy (str, input[i]);
            strcat (str, input[j]);
            output[k] = str;
            // at this point concatenation is printed correctly
            printf("%s\n", output[k]); correctly
            ++k;
        }
    }
}

Edit

Thanks to Goz’s comment I updated my function. Now, a pointer to char gets allocated, is pointed to the concatenation and is then stored inside output[k]. This way no data is lost when exciting the funcion:

void Permute(char **input, int words, char **output){
    int i = 0, j = 0, k = 0;
    char *p;
    for(i = 0; i < words; ++i) {
        for(j = 0; j < words; ++j) {
            p = malloc(sizeof(char*) * 5);
            strcpy(p, input[i]);
            strcat (p, input[j]);
            output[k] = p;
            printf("%d %s \n", k, output[k]); 
            ++k;
        }
    }
}

Edit

The buffer holding the result gets allocated before passing it over to the Permute function:

    // compute how much memory we need
    // allocate space for 4 pointers to char
    char **output = malloc(sizeof(char*) * 4); 
    int i = 0;
    // pre-allocate space for every pointer 
    for(i = 0; i < 4; i++)
       output[i] = malloc( sizeof( char ) * 5 ); 

Edit

Free all memory pointed to by char pointer before cleaning up the pointer to char pointer:

    // free memory
    for(i = 0; i < 4; i++ )
       free( output[i] );
    free(output);

    for(i = 0; i < 2; i++ )
       free(input[i]);
    free(input);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:57:52+00:00Added an answer on May 16, 2026 at 11:57 pm

    There are a couple of issues. Firstly you allocate a char*. You then assign it to a char** and expect it to have 2 dimensionality. It doesn’t. You’d need to malloc a set of char* pointers (4 * whatever your pointer size is … ie sizeof( char* )) then malloc 5 bytes for each of those pointers.

    Furthermore in Permute you overwrite the pointer value with the pointer to str (which doesn’t exist outside the function). You ought to be strcpy’ing the contents of str to output[k].

    In answer to the comment: Yes that will work but it would be advisable to allocate the buffer before you go into the loop.

    ie

    char** ptr = malloc( sizeof( char* ) * 4 );
    for( int i = 0; i++; i < 4 )
    {
       ptr[i] = malloc( sizeof( char ) * 4 ); // sizeof( char ) == 1 but its a good habit to get into.
    }
    

    Then as said before strcpy the temporary array into the relevant char* array.

    Furthermore remember that when you free the memory you need to do the opposite of the loop above. ie dealloc the 4 individual arrays and then dealloc the array of pointers. ie:

    for( int i = 0; i++; i < 4 )
    {
       free( ptr[i] );
    }
    free( ptr );
    

    ie all 5 occasions malloc is called are met with a corresponding free. If you free the array of ptr first you cannot guarantee that the memory is valid. Therefore the 4 pointers stored in that array may no longer be valid. So free them first then the array of pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that requires a const char pointer as input (not null
How do I allocate memory for a char variable (not a char pointer) inside
I'm developing a game and I need to find a way of getting the
I need to obfuscate or encrypt some plain text data in my php 5.2
I'm working on a texture management and animation solution for a small side project
I'm learning C right now and got a bit confused with character arrays -
I am trying to build a program to Tolkinize a string (i'm trying to
The C function myfunc operates on a larger chunk of data. The results are
I'm writing a string tokenization program for a homework assignment in C++, that uses
I've written a simple string tokenizing program using pointers for a recent school project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.