#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int main()
{
    int i=0,j=0,n=0;
    char *a,**b;
    a=(char *)malloc(sizeof(char)*50);
    printf("Enter your text\n");
    gets(a);
    while(a[i]!='\0')
    {
                     
                     *(b+n)=(char *)malloc(sizeof(char)*20);  //error
                     for(j=0;a[i]!=' ';i++,j++)
                     {
                                     *(*(b+n)+j)=*(a+i);
                     }
                     i++;
                     n++;
    }
    
    printf("Words in given text are \n");
    for(i=0;i<n;i++)
    {
                    puts(*(b+i));
                    printf("\n");
    }
    
    printf("Possibile combinations are \n\n");
    for(i=0;i<n;i++)
    {
                    for(j=0;j<n;j++)
                    {
                                    if(j!=i)
                                    {
                                            puts(b[i]);
                                            printf(" ");
                                            puts(b[j]);
                                            printf("\n");
                                    }
                    }
    }
    getch();
}

Above code is for separating words from a given line of text and to produce all possible two word combinations. When I execute this program in dev-cpp

*(b+n)=(char *)malloc(sizeof(char)*20);
produces an access violation.Actually what is wrong here. When i execute same program in turbo c, it does not produce expected output. Can you help me?

Your b pointer is completely uninitialized.

Your b pointer is completely uninitialized.

I'm using this code to initialize b pointer
*(b+n)=(char *)malloc(sizeof(char)*20);

what is wrong with this initialization?

*(b+n) equivalent to b[n];
*(*(b+n)+j) equivalent to b[n][j];

**b will store the address the address of any pointer or an array.
I am little confused with your code...!
Can you please explain me what all these thing means..??

I'm using this code to initialize b pointer
*(b+n)=(char *)malloc(sizeof(char)*20);

No, that looks more like you're assuming b is already initialized, otherwise you wouldn't be derefencing it.

what is wrong with this initialization?

It's an initialization of b[n] , not b .

No, that looks more like you're assuming b is already initialized, otherwise you wouldn't be derefencing it.


It's an initialization of b[n] , not b .

Do we have to initialize b and b[n]separately?

Can anyone give me the proper code for above program?

I'm using this code to initialize b pointer
*(b+n)=(char *)malloc(sizeof(char)*20);

That's not called initializing ,it's called dereferencing of b.Dereferencing a pointer always involves the value present at the address pointed to by that pointer.
for e.g.
If there's a pointer 'ptr' to char

char letter1='A'];
char *ptr;

Then you initialize it like this

ptr=&letter1;

and dereference like this:

char letter2=*(ptr);//dereferencing ptr
printf("%c\n",letter2);

Do we have to initialize b and b[n]separately?

Here b is a pointer to a pointer to a char.So b should contain an address of a pointer to a char.
for e.g.

char *letters1="hello",**ptr;
ptr=&letters1;//initializing ptr(letters1 is itself a pointer to a string of char)
char *letters2=*(ptr);//dereferencing ptr
printf("%c\n",*(letters2);//points to the first char of the string "hello"
printf("%c\n",*(letters2+3));//points to the fourth(array's index starts from 0) char of the string "hello"

Now, do you realize what you are missing from the code you posted above?

Do we have to initialize b and b[n]separately?

They're two separate pointers, so yes:

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

int main(void)
{
    char **b = malloc(10 * sizeof *b);
    
    if (b != NULL) {
        int i, j, n = 0;
        char a[50];
        
        printf("Enter your text: ");
        fflush(stdout);
        
        if (fgets(a, sizeof a, stdin) != NULL) {
            for (i = 0; a[i] != '\0'; n++) {
                b[n] = malloc(20);
                
                if (b[n] == NULL)
                    break;
                
                for (j = 0; a[i] != ' ' && a[i] != '\0'; i++, j++)
                    b[n][j] = a[i];
                    
                b[n][j] = '\0';
                
                if (a[i] != '\0')
                    ++i;
            }

            printf("Words in given text are:\n");
            
            for (i = 0; i < n; i++)
                printf(">%s<\n", b[i]);
        }
    }

    return 0;
}

Thanks for everyone
Here is my final code

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int main()
{
    char **b,*a;
    int i,j,n=0;
    a=(char *)malloc(100);
    b=(char **)malloc(sizeof(*b)*20);
    printf("Enter your text\n");
    gets(a);
    for(i=0;a[i]!='\0';n++)
    {
                           b[n]=(char *)malloc(20);
                           for(j=0;a[i]!=' '&&a[i]!='\0';i++,j++)
                           {
                                           b[n][j]=a[i];
                           }
                           b[n][j]='\0';
                           if(a[i]!='\0')
                           {
                                         i++;
                           }
    }
    printf("Words in given text are \n");
    for(i=0;i<n;i++)
    {
                    printf("%s\n",b[i]);
    }
    printf("Possible combinations are \n");
    for(i=0;i<n;i++)
    {
                    for(j=0;j<n;j++)
                    {
                                    if(i!=j)
                                    {
                                            printf("\n%s %s",b[i],b[j]);
                                    }
                    }
    }
    getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.