0

I am trying to make all possible combinations of alphabets using a number. Input NUM given by user. The combinatitions are created by splitting input numbers upto two digits. Input Obtained as char* I am Using C. I am getting output as Segmenation fault (core dumped), guessing because of the warning.
substr is my own function. sample input and output

input: 11112
output: 
AAAAB
AAAL
AAKB
AKAB
AKL
KAAB
KAL
KKB

My CODE

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

char* substr(char* str,int l,int n)
{
    char* c;
    int len = n-l;
    while(l<n)
    {
        *c = *(str+l);
        l++;
        c++;
    }
    *c='\0';
    return c-len;
}


int printAlpha(char* str, char* word)
{
    char *sub;
    char* ch;
    int n = strlen(str);
    if(n == 0)
    {
        printf("%s",word);
    }
    else
    {

     sub = substr(str,0,1);
     int num = atoi(sub);
     str = substr(str,1,n);
     ch = 'A'+(num-1); 
     printAlpha(str, strcat(word, ch));
     sub = substr(str,0,2);
     num = atoi(sub);
     if(strlen(str)>=2 && num <= 26)
     {
         str = substr(str,2,n);
         ch = 'A'+(num-1);
         printAlpha( str, strcat(word, ch) );
     }
    }
    return 0;
}


int main()
{
    char* str;
    char* word = '\0';
    scanf("%s",str);
    printAlpha(str,word);

    return 0;
}

thanks in advance.

Maddy
  • 3
  • 1
  • 1
    Well, you should allocate `str` and `word`. – CoderCharmander Feb 14 '20 at 10:26
  • can you please make the changes and show it clearly. As I am learning C, string concept in C always confusing. And gimme a simple explanation to aoid this in future, thans – Maddy Feb 14 '20 at 10:28
  • 1
    Also here `char* word = '\0';` assignment of `'\0'` to `word` is wrong, as `word` is character pointer not just a char. It should be `char* word = NULL;` – Achal Feb 14 '20 at 10:28
  • 1
    @Maddy Read [this](https://stackoverflow.com/questions/18217525/why-or-when-do-you-need-to-dynamically-allocate-memory-in-c) to know how to allocate memory dynamically & assign it to `word`. – Achal Feb 14 '20 at 10:31
  • Basically before attempting strings in C, you must already have a solid understanding of arrays and pointers. People coming from higher level programming languages to C always get this wrong. – Lundin Feb 14 '20 at 13:45

1 Answers1

0

As commenters have said you need to allocate memory in c dynamically.

In c if you need to store something like an array of characters you have 2 basic szenarios:

  1. You know how many elements your array will contain before you compile then you can use

    char word[numberOfLetters]

this will work as long as you dont need to store more letters, it becomes problematic in the other case

  1. you dont know how big your array has to be before compiling

    e.g when you are doing stuff with veriable lengths. imagine storing a user input into a char array. How should you make the array? if you make it 100 chars big and the user types 101 then you will get a segfault or loose everything he typed after the 100th char

you could also deal with this by making the array huge, but then with a short input youd be wasting a lot of memory and you still have the problem that if you need 1 char more than what you chose as size it wont work.

here is where you have to use dynamic memory allocation using functions like element_ptr* =malloc(numOfElements*sizeof(element)); to request memory during runtime.

what malloc does is it returns a pointer to the address, of the memory you requested

when you dont need the memory anymore you call free(element_ptr); this will free the memory again, otherwise it will stay blocked.

best you read up on malloc in the man pages

Community
  • 1
  • 1
NasaOK
  • 81
  • 5